169 lines
4.6 KiB
Go
169 lines
4.6 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 getDomain(w http.ResponseWriter, r *http.Request) (domain Domain, returnerr *libapi.ErrorResult) {
|
|
ctx := r.Context()
|
|
login := ctx.Value("login").(*system.Login)
|
|
profil := ctx.Value("profil").(*Profil)
|
|
id, err := strconv.ParseInt(pat.Param(r, "domainid"), 10, 64)
|
|
if err != nil {
|
|
returnerr = &libapi.ErrorResult{
|
|
Message: "Internal Request Error",
|
|
}
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
domain = Domain{ID: id}
|
|
db := dbconnection.First(&domain)
|
|
|
|
if db.Error != nil || db.RecordNotFound() {
|
|
returnerr = &libapi.ErrorResult{Fields: []string{"domain"}, Message: "domain not found"}
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
|
|
if !login.Superadmin {
|
|
if profil.ID != domain.ProfilID {
|
|
returnerr = &libapi.ErrorResult{Fields: []string{"profil"}, Message: "not allowed to get domain"}
|
|
w.WriteHeader(http.StatusForbidden)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func domainList(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
login := ctx.Value("login").(*system.Login)
|
|
profil := ctx.Value("profil").(*Profil)
|
|
logger := log.GetLog(r, "domainlist")
|
|
logger = logger.WithField("pID", profil.ID)
|
|
var domain []*Domain
|
|
if login.Superadmin && r.URL.Query().Get("filter") == "all" {
|
|
dbconnection.Preload("Profil").Preload("Profil.Login").Find(&domain)
|
|
} else {
|
|
dbconnection.Where("profil = ?", profil.ID).Find(&domain)
|
|
}
|
|
logger.Info("done")
|
|
libapi.JSONWrite(w, r, domain, nil)
|
|
}
|
|
|
|
func domainShow(w http.ResponseWriter, r *http.Request) {
|
|
logger := log.GetLog(r, "domainshow")
|
|
logger = setProfilLog(r, logger)
|
|
domain, returnerr := getDomain(w, r)
|
|
logger = logger.WithField("dID", domain.ID)
|
|
if returnerr != nil {
|
|
logger.Info(returnerr.Message)
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
|
return
|
|
}
|
|
logger.Info("done")
|
|
libapi.JSONWrite(w, r, domain, nil)
|
|
}
|
|
|
|
func domainAdd(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
profil := ctx.Value("profil").(*Profil)
|
|
logger := log.GetLog(r, "domainadd")
|
|
logger = setProfilLog(r, logger)
|
|
|
|
var domainRequest Domain
|
|
returnerr := libapi.JSONDecoder(w, r, logger, &domainRequest)
|
|
if returnerr != nil {
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
|
return
|
|
}
|
|
|
|
domain := &Domain{
|
|
ProfilID: profil.ID,
|
|
FQDN: domainRequest.FQDN,
|
|
Mail: domainRequest.Mail,
|
|
Web: domainRequest.Web,
|
|
}
|
|
|
|
if err := dbconnection.Create(domain).Error; err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
if strings.Contains(err.Error(), "licate key") {
|
|
logger.Warning("exists already")
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "already signup"})
|
|
return
|
|
}
|
|
logger.Error("database: during create host domain: ", 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 domainEdit(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
login := ctx.Value("login").(*system.Login)
|
|
logger := log.GetLog(r, "domainedit")
|
|
logger = setProfilLog(r, logger)
|
|
|
|
domain, returnerr := getDomain(w, r)
|
|
logger = logger.WithField("dID", domain.ID)
|
|
if returnerr != nil {
|
|
logger.Info(returnerr.Message)
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
|
return
|
|
}
|
|
|
|
var domainRequest Domain
|
|
returnerr = libapi.JSONDecoder(w, r, logger, &domainRequest)
|
|
if returnerr != nil {
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
|
return
|
|
}
|
|
|
|
domain.FQDN = domainRequest.FQDN
|
|
domain.Mail = domainRequest.Mail
|
|
domain.Web = domainRequest.Web
|
|
|
|
if login.Superadmin {
|
|
domain.Active = domainRequest.Active
|
|
}
|
|
|
|
if err := dbconnection.Save(domain).Error; err != nil {
|
|
logger.Error("database: during modify host domain: ", 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 domainDelete(w http.ResponseWriter, r *http.Request) {
|
|
logger := log.GetLog(r, "domaindelete")
|
|
logger = setProfilLog(r, logger)
|
|
|
|
domain, returnerr := getDomain(w, r)
|
|
logger = logger.WithField("dID", domain.ID)
|
|
if returnerr != nil {
|
|
logger.Info(returnerr.Message)
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
|
return
|
|
}
|
|
|
|
if err := dbconnection.Unscoped().Delete(domain).Error; err != nil {
|
|
logger.Error("database: during create host domain: ", 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)
|
|
}
|