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
|
|
|
|
|
|
|
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-12-19 12:24:18 +01:00
|
|
|
func getDomain(w http.ResponseWriter, r *http.Request) (domain Domain, returnerr *libapi.ErrorResult) {
|
|
|
|
ctx := r.Context()
|
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)
|
2016-12-19 12:24:18 +01:00
|
|
|
id, err := strconv.ParseInt(pat.Param(r, "domainid"), 10, 64)
|
2016-10-17 19:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
returnerr = &libapi.ErrorResult{
|
|
|
|
Message: "Internal Request Error",
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2016-12-19 12:24:18 +01:00
|
|
|
domain = Domain{}
|
2016-12-13 12:43:51 +01:00
|
|
|
|
2016-10-18 19:28:57 +02:00
|
|
|
if login.Superadmin {
|
2016-12-19 12:24:18 +01:00
|
|
|
dbconnection.Where("ID = ?", id).Find(&domain)
|
2016-10-18 19:28:57 +02:00
|
|
|
} else {
|
2016-12-19 12:24:18 +01:00
|
|
|
dbconnection.Where(map[string]int64{"ID": id, "profil": profil.ID}).Find(&domain)
|
2016-10-18 19:28:57 +02:00
|
|
|
}
|
|
|
|
|
2016-10-17 19:21:17 +02:00
|
|
|
if domain.ID <= 0 {
|
|
|
|
returnerr = &libapi.ErrorResult{Fields: []string{"domain"}, Message: "not found"}
|
2016-10-21 21:32:30 +02:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2016-10-17 19:21:17 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-19 12:24:18 +01:00
|
|
|
func domainList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
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)
|
|
|
|
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")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, domain, nil)
|
2016-10-17 11:54:35 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 12:24:18 +01:00
|
|
|
func domainShow(w http.ResponseWriter, r *http.Request) {
|
2016-12-13 12:43:51 +01:00
|
|
|
logger := log.GetLog(r, "domainshow")
|
2016-12-19 12:24:18 +01:00
|
|
|
domain, returnerr := getDomain(w, r)
|
2016-10-22 21:20:21 +02:00
|
|
|
if returnerr != nil {
|
|
|
|
logger.Info("not found")
|
2016-12-19 12:52:43 +01:00
|
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
2016-10-22 21:20:21 +02:00
|
|
|
return
|
|
|
|
}
|
2016-12-13 11:35:44 +01:00
|
|
|
logger = logger.WithField("dID", domain.ID)
|
2016-10-22 21:20:21 +02:00
|
|
|
logger.Info("done")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, domain, nil)
|
2016-10-22 21:20:21 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 12:24:18 +01:00
|
|
|
func domainAdd(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2016-10-17 11:54:35 +02:00
|
|
|
profil := ctx.Value("profil").(*Profil)
|
2016-10-17 14:07:17 +02:00
|
|
|
logger := log.GetLog(r, "domainadd")
|
|
|
|
|
|
|
|
var domainRequest Domain
|
2016-12-19 12:24:18 +01:00
|
|
|
returnerr := libapi.JSONDecoder(w, r, logger, &domainRequest)
|
2016-10-17 14:07:17 +02:00
|
|
|
if returnerr != nil {
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
2016-10-17 14:07:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
domain := &Domain{
|
|
|
|
ProfilID: profil.ID,
|
|
|
|
FQDN: domainRequest.FQDN,
|
|
|
|
Mail: domainRequest.Mail,
|
|
|
|
Web: domainRequest.Web,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dbconnection.Create(domain).Error; err != nil {
|
2016-12-19 12:24:18 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
if strings.Contains(err.Error(), "licate key") {
|
2016-10-17 19:21:17 +02:00
|
|
|
logger.Warning("exists already")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "already signup"})
|
2016-10-17 19:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Error("database: during create host domain: ", err)
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
|
2016-10-17 19:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Info("done")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, true, nil)
|
2016-10-17 19:21:17 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 12:24:18 +01:00
|
|
|
func domainEdit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2016-10-18 19:28:57 +02:00
|
|
|
login := ctx.Value("login").(*system.Login)
|
2016-10-17 19:21:17 +02:00
|
|
|
logger := log.GetLog(r, "domainedit")
|
|
|
|
|
2016-12-19 12:24:18 +01:00
|
|
|
domain, returnerr := getDomain(w, r)
|
2016-10-17 19:21:17 +02:00
|
|
|
if returnerr != nil {
|
|
|
|
logger.Info("not found")
|
2016-12-19 12:52:43 +01:00
|
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
2016-10-17 19:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
2016-12-13 11:35:44 +01:00
|
|
|
logger = logger.WithField("dID", domain.ID)
|
2016-10-17 19:21:17 +02:00
|
|
|
|
|
|
|
var domainRequest Domain
|
2016-12-19 12:24:18 +01:00
|
|
|
returnerr = libapi.JSONDecoder(w, r, logger, &domainRequest)
|
2016-10-17 19:21:17 +02:00
|
|
|
if returnerr != nil {
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
2016-10-17 19:21:17 +02:00
|
|
|
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)
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
|
2016-10-17 19:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Info("done")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, true, nil)
|
2016-10-17 19:21:17 +02:00
|
|
|
}
|
|
|
|
|
2016-12-19 12:24:18 +01:00
|
|
|
func domainDelete(w http.ResponseWriter, r *http.Request) {
|
2016-10-17 19:21:17 +02:00
|
|
|
logger := log.GetLog(r, "domaindelete")
|
|
|
|
|
2016-12-19 12:24:18 +01:00
|
|
|
domain, returnerr := getDomain(w, r)
|
2016-10-17 19:21:17 +02:00
|
|
|
if returnerr != nil {
|
|
|
|
logger.Info("not found")
|
2016-12-19 12:52:43 +01:00
|
|
|
libapi.JSONWrite(w, r, false, returnerr)
|
2016-10-17 19:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
2016-12-13 11:35:44 +01:00
|
|
|
logger = logger.WithField("dID", domain.ID)
|
2016-10-17 19:21:17 +02:00
|
|
|
|
2016-12-13 11:35:44 +01:00
|
|
|
if err := dbconnection.Unscoped().Delete(domain).Error; err != nil {
|
2016-10-17 14:07:17 +02:00
|
|
|
logger.Error("database: during create host domain: ", 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-17 14:07:17 +02:00
|
|
|
return
|
|
|
|
}
|
2016-10-17 11:54:35 +02:00
|
|
|
logger.Info("done")
|
2016-12-19 12:24:18 +01:00
|
|
|
libapi.JSONWrite(w, r, true, nil)
|
2016-10-17 11:54:35 +02:00
|
|
|
}
|