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/apiweb.go

229 lines
6.3 KiB
Go
Raw 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"
)
func cleanLoginFTPAccess(access []*FTPAccess) {
for _, item := range access {
item.LoginID = item.Login.ID
item.Login = nil
}
}
func cleanLoginHTTPAccess(access []*HTTPAccess) {
for _, item := range access {
item.LoginID = item.Login.ID
item.Login = nil
}
}
2016-12-19 12:24:18 +01:00
func getWeb(w http.ResponseWriter, r *http.Request) (web Web, 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, "webid"), 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
web = Web{}
2016-10-21 21:32:30 +02:00
if login.Superadmin {
2016-12-19 12:52:43 +01:00
dbconnection.Where("ID = ?", id).Preload("HTTPAccess.Login").Preload("FTPAccess.Login").Find(&web)
2016-10-21 21:32:30 +02:00
} else {
2016-12-19 12:52:43 +01:00
dbconnection.Where(map[string]int64{"ID": id, "domain.profil": profil.ID}).Preload("HTTPAccess.Login").Preload("FTPAccess.Login").Find(&web)
2016-10-21 21:32:30 +02:00
}
if web.ID <= 0 {
returnerr = &libapi.ErrorResult{Fields: []string{"web"}, Message: "not found"}
w.WriteHeader(http.StatusNotFound)
}
return
}
2016-12-19 12:24:18 +01:00
func webList(w http.ResponseWriter, r *http.Request) {
2016-10-21 21:32:30 +02:00
logger := log.GetLog(r, "weblist")
var web []*Web
2016-12-19 12:24:18 +01:00
domain, returnerr := getDomain(w, r)
2016-10-21 21:32:30 +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-21 21:32:30 +02:00
return
}
2016-12-13 11:35:44 +01:00
logger = logger.WithField("dID", domain.ID)
dbconnection.Where("domain = ?", domain.ID).Preload("Domain").Preload("HTTPAccess.Login").Preload("FTPAccess.Login").Find(&web)
2016-10-21 21:32:30 +02:00
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, web, nil)
2016-10-21 21:32:30 +02:00
}
2016-12-19 12:24:18 +01:00
func webAdd(w http.ResponseWriter, r *http.Request) {
2016-10-21 21:32:30 +02:00
logger := log.GetLog(r, "webadd")
var webRequest Web
2016-12-19 12:24:18 +01:00
returnerr := libapi.JSONDecoder(w, r, logger, &webRequest)
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 {
logger.Info("not found")
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)
cleanLoginFTPAccess(webRequest.FTPAccess)
cleanLoginHTTPAccess(webRequest.HTTPAccess)
2016-10-21 21:32:30 +02:00
web := &Web{
DomainID: domain.ID,
Subdomain: webRequest.Subdomain,
PHP: webRequest.PHP,
SSL: webRequest.SSL,
SSLRedirect: webRequest.SSLRedirect,
Redirect: webRequest.Redirect,
Proxy: webRequest.Proxy,
FTPAccess: webRequest.FTPAccess,
HTTPAccess: webRequest.HTTPAccess,
2016-10-21 21:32:30 +02:00
}
if err := dbconnection.Create(web).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 web: ", 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 webEdit(w http.ResponseWriter, r *http.Request) {
2016-10-21 21:32:30 +02:00
logger := log.GetLog(r, "webedit")
2016-12-19 12:24:18 +01:00
web, returnerr := getWeb(w, r)
2016-10-21 21:32:30 +02:00
if returnerr != nil {
logger.Info("not found")
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("wID", web.ID)
2016-10-21 21:32:30 +02:00
var webRequest Web
2016-12-19 12:24:18 +01:00
returnerr = libapi.JSONDecoder(w, r, logger, &webRequest)
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
}
web.Subdomain = webRequest.Subdomain
web.PHP = webRequest.PHP
web.SSL = webRequest.SSL
web.SSLRedirect = webRequest.SSLRedirect
web.Redirect = webRequest.Redirect
web.Proxy = webRequest.Proxy
idsOld := map[int64]struct{}{}
for _, item := range web.HTTPAccess {
idsOld[item.Login.ID] = struct{}{}
}
web.HTTPAccess = nil
idsNew := map[int64]struct{}{}
for _, item := range webRequest.HTTPAccess {
if _, ok := idsOld[item.Login.ID]; !ok {
web.HTTPAccess = append(web.HTTPAccess, item)
}
idsNew[item.Login.ID] = struct{}{}
}
idsDel := []int64{}
for id := range idsOld {
if _, ok := idsNew[id]; !ok {
idsDel = append(idsDel, id)
}
}
if err := dbconnection.Unscoped().Where("web = ?", web.ID).Delete(HTTPAccess{}, "login in (?)", idsDel).Error; err != nil {
logger.Error("database: during delete host web httpaccess: ", 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
}
idsOld = map[int64]struct{}{}
for _, item := range web.FTPAccess {
idsOld[item.Login.ID] = struct{}{}
}
web.FTPAccess = nil
idsNew = map[int64]struct{}{}
for _, item := range webRequest.FTPAccess {
if _, ok := idsOld[item.Login.ID]; !ok {
web.FTPAccess = append(web.FTPAccess, item)
}
idsNew[item.Login.ID] = struct{}{}
}
idsDel = []int64{}
for id := range idsOld {
if _, ok := idsNew[id]; !ok {
idsDel = append(idsDel, id)
}
}
if err := dbconnection.Unscoped().Where("web = ?", web.ID).Delete(FTPAccess{}, "login in (?)", idsDel).Error; err != nil {
logger.Error("database: during delete host web ftpaccess: ", 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
}
cleanLoginFTPAccess(web.FTPAccess)
cleanLoginHTTPAccess(web.HTTPAccess)
2016-10-21 21:32:30 +02:00
if err := dbconnection.Save(web).Error; err != nil {
logger.Error("database: during modify host web: ", 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 webDelete(w http.ResponseWriter, r *http.Request) {
2016-10-21 21:32:30 +02:00
logger := log.GetLog(r, "webdelete")
2016-12-19 12:24:18 +01:00
web, returnerr := getWeb(w, r)
2016-10-21 21:32:30 +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-21 21:32:30 +02:00
return
}
2016-12-13 11:35:44 +01:00
logger = logger.WithField("wID", web.ID)
2016-10-21 21:32:30 +02:00
2016-12-13 11:35:44 +01:00
if err := dbconnection.Unscoped().Delete(web).Error; err != nil {
2016-10-21 21:32:30 +02:00
logger.Error("database: during create host web: ", 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
}