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 } } func getWeb(w http.ResponseWriter, r *http.Request) (web Web, returnerr *libapi.ErrorResult) { ctx := r.Context() login := ctx.Value("login").(*system.Login) profil := ctx.Value("profil").(*Profil) id, err := strconv.ParseInt(pat.Param(r, "webid"), 10, 64) if err != nil { returnerr = &libapi.ErrorResult{ Message: "Internal Request Error", } w.WriteHeader(http.StatusBadRequest) return } web = Web{} if login.Superadmin { dbconnection.Where("ID = ?", id).Preload("HTTPAccess.Login").Preload("FTPAccess.Login").Find(&web) } else { dbconnection.Where(map[string]int64{"ID": id, "domain.profil": profil.ID}).Preload("HTTPAccess.Login").Preload("FTPAccess.Login").Find(&web) } if web.ID <= 0 { returnerr = &libapi.ErrorResult{Fields: []string{"web"}, Message: "not found"} w.WriteHeader(http.StatusNotFound) } return } func webList(w http.ResponseWriter, r *http.Request) { logger := log.GetLog(r, "weblist") var web []*Web domain, returnerr := getDomain(w, r) if returnerr != nil { logger.Info("not found") libapi.JSONWrite(w, r, false, returnerr) return } logger = logger.WithField("dID", domain.ID) dbconnection.Where("domain = ?", domain.ID).Preload("Domain").Preload("HTTPAccess.Login").Preload("FTPAccess.Login").Find(&web) logger.Info("done") libapi.JSONWrite(w, r, web, nil) } func webAdd(w http.ResponseWriter, r *http.Request) { logger := log.GetLog(r, "webadd") var webRequest Web returnerr := libapi.JSONDecoder(w, r, logger, &webRequest) if returnerr != nil { libapi.JSONWrite(w, r, false, returnerr) return } domain, returnerr := getDomain(w, r) if returnerr != nil { logger.Info("not found") libapi.JSONWrite(w, r, false, returnerr) return } logger = logger.WithField("dID", domain.ID) cleanLoginFTPAccess(webRequest.FTPAccess) cleanLoginHTTPAccess(webRequest.HTTPAccess) 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, } if err := dbconnection.Create(web).Error; err != nil { w.WriteHeader(http.StatusInternalServerError) if strings.Contains(err.Error(), "duplicate key") { logger.Warning("exists already") libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "already signup"}) return } logger.Error("database: during create host web: ", err) libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"}) return } logger.Info("done") libapi.JSONWrite(w, r, true, nil) } func webEdit(w http.ResponseWriter, r *http.Request) { logger := log.GetLog(r, "webedit") web, returnerr := getWeb(w, r) if returnerr != nil { logger.Info("not found") libapi.JSONWrite(w, r, false, returnerr) return } logger = logger.WithField("wID", web.ID) var webRequest Web returnerr = libapi.JSONDecoder(w, r, logger, &webRequest) if returnerr != nil { libapi.JSONWrite(w, r, false, returnerr) 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) 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) libapi.JSONWrite(w, r, false, &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) w.WriteHeader(http.StatusInternalServerError) libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"}) return } logger.Info("done") libapi.JSONWrite(w, r, true, nil) } func webDelete(w http.ResponseWriter, r *http.Request) { logger := log.GetLog(r, "webdelete") web, returnerr := getWeb(w, r) if returnerr != nil { logger.Info("not found") libapi.JSONWrite(w, r, false, returnerr) return } logger = logger.WithField("wID", web.ID) if err := dbconnection.Unscoped().Delete(web).Error; err != nil { logger.Error("database: during create host web: ", err) w.WriteHeader(http.StatusInternalServerError) libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"}) return } logger.Info("done") libapi.JSONWrite(w, r, true, nil) }