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

180 lines
4.9 KiB
Go

package host
import (
"net/http"
"strconv"
"strings"
"goji.io/pat"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
system "dev.sum7.eu/sum7/warehost/system"
)
func getMail(w http.ResponseWriter, r *http.Request) (mail Mail, returnerr *libapi.ErrorResult) {
ctx := r.Context()
login := ctx.Value("login").(*system.Login)
profil := ctx.Value("profil").(*Profil)
id, err := strconv.ParseInt(pat.Param(r, "mailid"), 10, 64)
if err != nil {
returnerr = &libapi.ErrorResult{
Message: "Internal Request Error",
}
w.WriteHeader(http.StatusBadRequest)
return
}
mail = Mail{}
db := dbconnection.Where("ID = ?", id).Preload("Domain").Preload("Forwards").First(&mail)
if db.Error != nil || db.RecordNotFound() {
returnerr = &libapi.ErrorResult{Fields: []string{"mail"}, Message: "mail not found"}
w.WriteHeader(http.StatusNotFound)
}
if !login.Superadmin {
if mail.Domain.ProfilID != profil.ID {
returnerr = &libapi.ErrorResult{Fields: []string{"profil"}, Message: "not allowed to get mail"}
w.WriteHeader(http.StatusForbidden)
}
}
return
}
func mailList(w http.ResponseWriter, r *http.Request) {
logger := log.GetLog(r, "maillist")
var mail []*Mail
logger = setProfilLog(r, logger)
domain, returnerr := getDomain(w, r)
if returnerr != nil {
logger.Info(returnerr.Message)
libapi.JSONWrite(w, r, false, returnerr)
return
}
logger = logger.WithField("dID", domain.ID)
dbconnection.Where("domain = ?", domain.ID).Preload("Domain").Preload("Forwards").Find(&mail)
logger.Info("done")
libapi.JSONWrite(w, r, mail, nil)
return
}
func mailAdd(w http.ResponseWriter, r *http.Request) {
logger := log.GetLog(r, "mailadd")
logger = setProfilLog(r, logger)
var mailRequest Mail
returnerr := libapi.JSONDecoder(w, r, logger, &mailRequest)
if returnerr != nil {
libapi.JSONWrite(w, r, false, returnerr)
return
}
domain, returnerr := getDomain(w, r)
if returnerr != nil {
logger.Info(returnerr.Message)
libapi.JSONWrite(w, r, false, returnerr)
return
}
logger = logger.WithField("dID", domain.ID)
mail := &Mail{
DomainID: domain.ID,
Name: mailRequest.Name,
Forwards: mailRequest.Forwards,
LoginID: mailRequest.LoginID,
}
if err := dbconnection.Create(mail).Error; err != nil {
w.WriteHeader(http.StatusInternalServerError)
if strings.Contains(err.Error(), "duplicate key") {
logger.Warning("exists already")
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "already signup"})
return
}
logger.Error("database: during create host mail: ", err)
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
return
}
logger.Info("done")
libapi.JSONWrite(w, r, true, nil)
}
func mailEdit(w http.ResponseWriter, r *http.Request) {
logger := log.GetLog(r, "mailedit")
logger = setProfilLog(r, logger)
mail, returnerr := getMail(w, r)
if returnerr != nil {
logger.Info(returnerr.Message)
libapi.JSONWrite(w, r, false, returnerr)
return
}
logger = logger.WithField("mID", mail.ID)
var mailRequest Mail
returnerr = libapi.JSONDecoder(w, r, logger, &mailRequest)
if returnerr != nil {
libapi.JSONWrite(w, r, false, returnerr)
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)
libapi.JSONWrite(w, r, false, &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)
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
return
}
logger.Info("done")
libapi.JSONWrite(w, r, true, nil)
}
func mailDelete(w http.ResponseWriter, r *http.Request) {
logger := log.GetLog(r, "maildelete")
logger = setProfilLog(r, logger)
mail, returnerr := getMail(w, r)
if returnerr != nil {
logger.Info(returnerr.Message)
libapi.JSONWrite(w, r, false, returnerr)
return
}
logger = logger.WithField("mID", mail.ID)
if err := dbconnection.Unscoped().Delete(mail).Error; err != nil {
logger.Error("database: during create host mail: ", err)
w.WriteHeader(http.StatusInternalServerError)
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
return
}
logger.Info("done")
libapi.JSONWrite(w, r, true, nil)
}