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

182 lines
4.9 KiB
Go
Raw Permalink Normal View History

2016-10-21 21:32:30 +02:00
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"
)
2016-12-19 12:24:18 +01:00
func getMail(w http.ResponseWriter, r *http.Request) (mail Mail, returnerr *libapi.ErrorResult) {
ctx := r.Context()
2016-10-21 21:32:30 +02:00
login := ctx.Value("login").(*system.Login)
profil := ctx.Value("profil").(*Profil)
2016-12-19 12:24:18 +01:00
id, err := strconv.ParseInt(pat.Param(r, "mailid"), 10, 64)
2016-10-21 21:32:30 +02:00
if err != nil {
returnerr = &libapi.ErrorResult{
Message: "Internal Request Error",
}
w.WriteHeader(http.StatusBadRequest)
return
}
2016-12-19 12:52:43 +01:00
mail = Mail{}
2016-12-13 12:49:17 +01:00
2017-06-02 10:15:09 +02:00
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"}
2016-10-21 21:32:30 +02:00
w.WriteHeader(http.StatusNotFound)
}
2017-06-02 10:15:09 +02:00
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)
}
}
2016-10-21 21:32:30 +02:00
return
}
2016-12-19 12:24:18 +01:00
func mailList(w http.ResponseWriter, r *http.Request) {
2016-10-21 21:32:30 +02:00
logger := log.GetLog(r, "maillist")
var mail []*Mail
2017-06-02 10:15:09 +02:00
logger = setProfilLog(r, logger)
2016-12-19 12:24:18 +01:00
domain, returnerr := getDomain(w, r)
2016-10-21 21:32:30 +02:00
if returnerr != nil {
2017-06-02 10:15:09 +02:00
logger.Info(returnerr.Message)
2016-12-19 12:52:43 +01:00
libapi.JSONWrite(w, r, false, returnerr)
2016-10-21 21:32:30 +02:00
return
}
2016-12-13 11:35:44 +01:00
logger = logger.WithField("dID", domain.ID)
2016-11-15 20:50:57 +01:00
dbconnection.Where("domain = ?", domain.ID).Preload("Domain").Preload("Forwards").Find(&mail)
2016-10-21 21:32:30 +02:00
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, mail, nil)
2016-10-21 21:32:30 +02:00
return
}
2016-12-19 12:24:18 +01:00
func mailAdd(w http.ResponseWriter, r *http.Request) {
2016-10-21 21:32:30 +02:00
logger := log.GetLog(r, "mailadd")
2017-06-02 10:15:09 +02:00
logger = setProfilLog(r, logger)
2016-10-21 21:32:30 +02:00
var mailRequest Mail
2016-12-19 12:24:18 +01:00
returnerr := libapi.JSONDecoder(w, r, logger, &mailRequest)
2016-10-21 21:32:30 +02:00
if returnerr != nil {
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, returnerr)
2016-10-21 21:32:30 +02:00
return
}
2016-12-19 12:24:18 +01:00
domain, returnerr := getDomain(w, r)
2016-10-21 21:32:30 +02:00
if returnerr != nil {
2017-06-02 10:15:09 +02:00
logger.Info(returnerr.Message)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, returnerr)
2016-10-21 21:32:30 +02:00
return
}
2016-12-13 11:35:44 +01:00
logger = logger.WithField("dID", domain.ID)
2016-10-21 21:32:30 +02:00
mail := &Mail{
DomainID: domain.ID,
Name: mailRequest.Name,
2016-11-15 20:50:57 +01:00
Forwards: mailRequest.Forwards,
LoginID: mailRequest.LoginID,
2016-10-21 21:32:30 +02:00
}
if err := dbconnection.Create(mail).Error; err != nil {
2016-12-19 12:24:18 +01:00
w.WriteHeader(http.StatusInternalServerError)
2016-10-21 21:32:30 +02:00
if strings.Contains(err.Error(), "duplicate key") {
logger.Warning("exists already")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "already signup"})
2016-10-21 21:32:30 +02:00
return
}
logger.Error("database: during create host mail: ", err)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
2016-10-21 21:32:30 +02:00
return
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-10-21 21:32:30 +02:00
}
2016-12-19 12:24:18 +01:00
func mailEdit(w http.ResponseWriter, r *http.Request) {
2016-10-21 21:32:30 +02:00
logger := log.GetLog(r, "mailedit")
2017-06-02 10:15:09 +02:00
logger = setProfilLog(r, logger)
2016-10-21 21:32:30 +02:00
2016-12-19 12:24:18 +01:00
mail, returnerr := getMail(w, r)
2016-10-21 21:32:30 +02:00
if returnerr != nil {
2017-06-02 10:15:09 +02:00
logger.Info(returnerr.Message)
2016-12-19 12:52:43 +01:00
libapi.JSONWrite(w, r, false, returnerr)
2016-10-21 21:32:30 +02:00
return
}
2016-12-13 11:35:44 +01:00
logger = logger.WithField("mID", mail.ID)
2016-10-21 21:32:30 +02:00
var mailRequest Mail
2016-12-19 12:24:18 +01:00
returnerr = libapi.JSONDecoder(w, r, logger, &mailRequest)
2016-10-21 21:32:30 +02:00
if returnerr != nil {
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, returnerr)
2016-10-21 21:32:30 +02:00
return
}
mail.Name = mailRequest.Name
mail.LoginID = mailRequest.LoginID
2017-10-04 12:32:07 +02:00
mail.Domain = nil
2016-10-21 21:32:30 +02:00
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)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
return
}
2017-10-04 12:32:07 +02:00
2016-10-21 21:32:30 +02:00
if err := dbconnection.Save(mail).Error; err != nil {
logger.Error("database: during modify host mail: ", err)
w.WriteHeader(http.StatusInternalServerError)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
2016-10-21 21:32:30 +02:00
return
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-10-21 21:32:30 +02:00
}
2016-12-19 12:24:18 +01:00
func mailDelete(w http.ResponseWriter, r *http.Request) {
2016-10-21 21:32:30 +02:00
logger := log.GetLog(r, "maildelete")
2017-06-02 10:15:09 +02:00
logger = setProfilLog(r, logger)
2016-10-21 21:32:30 +02:00
2016-12-19 12:24:18 +01:00
mail, returnerr := getMail(w, r)
2016-10-21 21:32:30 +02:00
if returnerr != nil {
2017-06-02 10:15:09 +02:00
logger.Info(returnerr.Message)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, returnerr)
2016-10-21 21:32:30 +02:00
return
}
2016-12-13 11:35:44 +01:00
logger = logger.WithField("mID", mail.ID)
2016-10-21 21:32:30 +02:00
2016-12-13 11:35:44 +01:00
if err := dbconnection.Unscoped().Delete(mail).Error; err != nil {
2016-10-21 21:32:30 +02:00
logger.Error("database: during create host mail: ", err)
w.WriteHeader(http.StatusInternalServerError)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
2016-10-21 21:32:30 +02:00
return
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-10-21 21:32:30 +02:00
}