144 lines
3.9 KiB
Go
144 lines
3.9 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 getDomain(ctx context.Context, w http.ResponseWriter) (domain Domain, returnerr *libapi.ErrorResult) {
|
|
login := ctx.Value("login").(*system.Login)
|
|
profil := ctx.Value("profil").(*Profil)
|
|
id, err := strconv.ParseInt(pat.Param(ctx, "domainid"), 10, 64)
|
|
if err != nil {
|
|
returnerr = &libapi.ErrorResult{
|
|
Message: "Internal Request Error",
|
|
}
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
if login.Superadmin {
|
|
dbconnection.Where("id = ?", id).Find(&domain)
|
|
} else {
|
|
dbconnection.Where(map[string]int64{"id": id, "profil": profil.ID}).Find(&domain)
|
|
}
|
|
|
|
if domain.ID <= 0 {
|
|
returnerr = &libapi.ErrorResult{Fields: []string{"domain"}, Message: "not found"}
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
return
|
|
}
|
|
|
|
func domainList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
login := ctx.Value("login").(*system.Login)
|
|
profil := ctx.Value("profil").(*Profil)
|
|
returndata = false
|
|
logger := log.GetLog(r, "domainlist")
|
|
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")
|
|
returndata = domain
|
|
return
|
|
}
|
|
|
|
func domainAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
profil := ctx.Value("profil").(*Profil)
|
|
returndata = false
|
|
logger := log.GetLog(r, "domainadd")
|
|
|
|
var domainRequest Domain
|
|
returnerr = libapi.JSONDecoder(r.Body, &domainRequest, w, logger)
|
|
if returnerr != nil {
|
|
return
|
|
}
|
|
|
|
domain := &Domain{
|
|
ProfilID: profil.ID,
|
|
FQDN: domainRequest.FQDN,
|
|
Mail: domainRequest.Mail,
|
|
Web: domainRequest.Web,
|
|
}
|
|
|
|
if err := dbconnection.Create(domain).Error; err != nil {
|
|
if strings.Contains(err.Error(), "duplicate key") {
|
|
logger.Warning("exists already")
|
|
return
|
|
}
|
|
logger.Error("database: during create host domain: ", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
|
|
return
|
|
}
|
|
returndata = true
|
|
logger.Info("done")
|
|
return
|
|
}
|
|
|
|
func domainEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
login := ctx.Value("login").(*system.Login)
|
|
returndata = false
|
|
logger := log.GetLog(r, "domainedit")
|
|
|
|
domain, returnerr := getDomain(ctx, w)
|
|
if returnerr != nil {
|
|
logger.Info("not found")
|
|
return
|
|
}
|
|
|
|
var domainRequest Domain
|
|
returnerr = libapi.JSONDecoder(r.Body, &domainRequest, w, logger)
|
|
if returnerr != nil {
|
|
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)
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
|
|
return
|
|
}
|
|
returndata = true
|
|
logger.Info("done")
|
|
return
|
|
}
|
|
|
|
func domainDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
returndata = false
|
|
logger := log.GetLog(r, "domaindelete")
|
|
|
|
domainRequest, returnerr := getDomain(ctx, w)
|
|
if returnerr != nil {
|
|
logger.Info("not found")
|
|
return
|
|
}
|
|
|
|
if err := dbconnection.Unscoped().Delete(domainRequest).Error; err != nil {
|
|
logger.Error("database: during create host domain: ", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
|
|
return
|
|
}
|
|
returndata = true
|
|
logger.Info("done")
|
|
return
|
|
}
|