sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

fix #13 web (http and ftp) access adding and editing

This commit is contained in:
Martin Geno 2016-11-20 12:04:24 +01:00
parent f10964d27a
commit 0d2556d491
1 changed files with 75 additions and 4 deletions

View File

@ -12,6 +12,19 @@ import (
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
}
}
func getWeb(ctx context.Context, w http.ResponseWriter) (web Web, returnerr *libapi.ErrorResult) {
login := ctx.Value("login").(*system.Login)
profil := ctx.Value("profil").(*Profil)
@ -65,6 +78,8 @@ func webAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (return
logger.Info("not found")
return
}
cleanLoginFTPAccess(webRequest.FTPAccess)
cleanLoginHTTPAccess(webRequest.HTTPAccess)
web := &Web{
DomainID: domain.ID,
@ -74,8 +89,8 @@ func webAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (return
SSLRedirect: webRequest.SSLRedirect,
Redirect: webRequest.Redirect,
Proxy: webRequest.Proxy,
//FTPAccess: webRequest.FTPAccess,
//HTTPAccess: webRequest.HTTPAccess,
FTPAccess: webRequest.FTPAccess,
HTTPAccess: webRequest.HTTPAccess,
}
if err := dbconnection.Create(web).Error; err != nil {
@ -115,8 +130,64 @@ func webEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (retur
web.SSLRedirect = webRequest.SSLRedirect
web.Redirect = webRequest.Redirect
web.Proxy = webRequest.Proxy
//web.FTPAccess = webRequest.FTPAccess
//web.HTTPAccess = webRequest.HTTPAccess
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)
returnerr = &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)
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
return
}
cleanLoginFTPAccess(web.FTPAccess)
cleanLoginHTTPAccess(web.HTTPAccess)
if err := dbconnection.Save(web).Error; err != nil {
logger.Error("database: during modify host web: ", err)