sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/modul/host/apimail.go

165 lines
4.4 KiB
Go

package host
import (
"net/http"
"strconv"
"strings"
"goji.io/pat"
"golang.org/x/net/context"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
system "dev.sum7.eu/sum7/warehost/system"
)
func getMail(ctx context.Context, w http.ResponseWriter) (mail Mail, returnerr *libapi.ErrorResult) {
login := ctx.Value("login").(*system.Login)
profil := ctx.Value("profil").(*Profil)
id, err := strconv.ParseInt(pat.Param(ctx, "mailid"), 10, 64)
if err != nil {
returnerr = &libapi.ErrorResult{
Message: "Internal Request Error",
}
w.WriteHeader(http.StatusBadRequest)
return
}
if login.Superadmin {
dbconnection.Where("id = ?", id).Preload("Forwards").Find(&mail)
} else {
dbconnection.Where(map[string]int64{"ID": id, "domain.profil": profil.ID}).Preload("Forwards").Find(&mail)
}
if mail.ID <= 0 {
returnerr = &libapi.ErrorResult{Fields: []string{"mail"}, Message: "not found"}
w.WriteHeader(http.StatusNotFound)
}
return
}
func mailList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "maillist")
var mail []*Mail
domain, returnerr := getDomain(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
dbconnection.Where("domain = ?", domain.ID).Preload("Domain").Preload("Forwards").Find(&mail)
logger.Info("done")
returndata = mail
return
}
func mailAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "mailadd")
var mailRequest Mail
returnerr = libapi.JSONDecoder(r.Body, &mailRequest, w, logger)
if returnerr != nil {
return
}
domain, returnerr := getDomain(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
mail := &Mail{
DomainID: domain.ID,
Name: mailRequest.Name,
Forwards: mailRequest.Forwards,
LoginID: mailRequest.LoginID,
}
if err := dbconnection.Create(mail).Error; err != nil {
if strings.Contains(err.Error(), "duplicate key") {
logger.Warning("exists already")
return
}
logger.Error("database: during create host mail: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
logger.Info("done")
return
}
func mailEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "mailedit")
mail, returnerr := getMail(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
var mailRequest Mail
returnerr = libapi.JSONDecoder(r.Body, &mailRequest, w, logger)
if returnerr != nil {
return
}
mail.Name = mailRequest.Name
mail.LoginID = mailRequest.LoginID
idsStay := map[int64]struct{}{}
idsStay[0] = struct{}{}
for _, item := range mailRequest.Forwards {
if item.ID == 0 {
mail.Forwards = append(mail.Forwards, item)
} else {
idsStay[item.ID] = struct{}{}
}
}
idsDel := []int64{}
for _, item := range mail.Forwards {
if _, ok := idsStay[item.ID]; !ok {
idsDel = append(idsDel, item.ID)
}
}
if err := dbconnection.Unscoped().Delete(MailForward{}, "id in (?)", idsDel).Error; err != nil {
logger.Error("database: during delete host mail forwards: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
if err := dbconnection.Save(mail).Error; err != nil {
logger.Error("database: during modify host mail: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
logger.Info("done")
return
}
func mailDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
returndata = false
logger := log.GetLog(r, "maildelete")
mailRequest, returnerr := getMail(ctx, w)
if returnerr != nil {
logger.Info("not found")
return
}
if err := dbconnection.Unscoped().Delete(mailRequest).Error; err != nil {
logger.Error("database: during create host mail: ", err)
w.WriteHeader(http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
returndata = true
logger.Info("done")
return
}