2016-10-17 11:54:35 +02:00
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-10-17 19:21:17 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-10-17 11:54:35 +02:00
|
|
|
|
2016-10-17 19:21:17 +02:00
|
|
|
"goji.io/pat"
|
2016-10-17 11:54:35 +02:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
2016-10-18 19:28:57 +02:00
|
|
|
system "dev.sum7.eu/sum7/warehost/system"
|
2016-10-17 11:54:35 +02:00
|
|
|
)
|
|
|
|
|
2016-10-17 19:21:17 +02:00
|
|
|
func getDomain(ctx context.Context, w http.ResponseWriter) (domain Domain, returnerr *libapi.ErrorResult) {
|
2016-10-18 19:28:57 +02:00
|
|
|
login := ctx.Value("login").(*system.Login)
|
2016-10-17 19:21:17 +02:00
|
|
|
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
|
|
|
|
}
|
2016-10-18 19:28:57 +02:00
|
|
|
if login.Superadmin {
|
|
|
|
dbconnection.Where("id = ?", id).Find(&domain)
|
|
|
|
} else {
|
|
|
|
dbconnection.Where(map[string]int64{"id": id, "profil": profil.ID}).Find(&domain)
|
|
|
|
}
|
|
|
|
|
2016-10-17 19:21:17 +02:00
|
|
|
if domain.ID <= 0 {
|
|
|
|
returnerr = &libapi.ErrorResult{Fields: []string{"domain"}, Message: "not found"}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-17 11:54:35 +02:00
|
|
|
func domainList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-10-18 19:28:57 +02:00
|
|
|
login := ctx.Value("login").(*system.Login)
|
2016-10-17 11:54:35 +02:00
|
|
|
profil := ctx.Value("profil").(*Profil)
|
|
|
|
returndata = false
|
|
|
|
logger := log.GetLog(r, "domainlist")
|
|
|
|
var domain []*Domain
|
2016-10-18 19:28:57 +02:00
|
|
|
if login.Superadmin && r.URL.Query().Get("filter") == "all" {
|
2016-10-18 20:26:17 +02:00
|
|
|
dbconnection.Preload("Profil").Preload("Profil.Login").Find(&domain)
|
2016-10-18 19:28:57 +02:00
|
|
|
} else {
|
|
|
|
dbconnection.Where("profil = ?", profil.ID).Find(&domain)
|
|
|
|
}
|
2016-10-17 11:54:35 +02:00
|
|
|
logger.Info("done")
|
|
|
|
returndata = domain
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-17 14:07:17 +02:00
|
|
|
func domainAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-10-17 11:54:35 +02:00
|
|
|
profil := ctx.Value("profil").(*Profil)
|
|
|
|
returndata = false
|
2016-10-17 14:07:17 +02:00
|
|
|
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 {
|
2016-10-17 19:21:17 +02:00
|
|
|
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) {
|
2016-10-18 19:28:57 +02:00
|
|
|
login := ctx.Value("login").(*system.Login)
|
2016-10-17 19:21:17 +02:00
|
|
|
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
|
|
|
|
|
2016-10-18 19:28:57 +02:00
|
|
|
if login.Superadmin {
|
|
|
|
domain.Active = domainRequest.Active
|
|
|
|
}
|
|
|
|
|
2016-10-17 19:21:17 +02:00
|
|
|
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 {
|
2016-10-17 14:07:17 +02:00
|
|
|
logger.Error("database: during create host domain: ", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returndata = true
|
2016-10-17 11:54:35 +02:00
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|