diff --git a/modul/host/api.go b/modul/host/api.go index 3cc310f..5b53189 100644 --- a/modul/host/api.go +++ b/modul/host/api.go @@ -35,6 +35,16 @@ func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) { router.HandleFuncC(pat.Patch(prefix+"/domain/:domainid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainEdit)))) router.HandleFuncC(pat.Delete(prefix+"/domain/:domainid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainDelete)))) + router.HandleFuncC(pat.Get(prefix+"/domain/:domainid/web"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(webList)))) + router.HandleFuncC(pat.Post(prefix+"/domain/:domainid/web"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(webAdd)))) + router.HandleFuncC(pat.Patch(prefix+"/domain/:domainid/web/:webid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(webEdit)))) + router.HandleFuncC(pat.Delete(prefix+"/domain/:domainid/web/:webid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(webDelete)))) + + router.HandleFuncC(pat.Get(prefix+"/domain/:domainid/mail"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(mailList)))) + router.HandleFuncC(pat.Post(prefix+"/domain/:domainid/mail"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(mailAdd)))) + router.HandleFuncC(pat.Patch(prefix+"/domain/:domainid/mail/:mailid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(mailEdit)))) + router.HandleFuncC(pat.Delete(prefix+"/domain/:domainid/mail/:mailid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(mailDelete)))) + router.HandleFuncC(pat.Get(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseList)))) router.HandleFuncC(pat.Post(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseAdd)))) router.HandleFuncC(pat.Patch(prefix+"/database/:databaseid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseEdit)))) diff --git a/modul/host/apidatabase.go b/modul/host/apidatabase.go index 4d9c4b5..a723da3 100644 --- a/modul/host/apidatabase.go +++ b/modul/host/apidatabase.go @@ -31,6 +31,7 @@ func getDatabase(ctx context.Context, w http.ResponseWriter) (database Database, if database.ID <= 0 { returnerr = &libapi.ErrorResult{Fields: []string{"database"}, Message: "not found"} + w.WriteHeader(http.StatusNotFound) } return } diff --git a/modul/host/apidatabase_test.go b/modul/host/apidatabase_test.go index 242ecd8..90730e0 100644 --- a/modul/host/apidatabase_test.go +++ b/modul/host/apidatabase_test.go @@ -92,6 +92,12 @@ func TestAPIDatabase(t *testing.T) { assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, true) + result, w = session.JSONRequest("PATCH", "/host/database/"+strconv.Itoa(-1), Database{ + Comment: "test-bug", + }) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + result, w = session.JSONRequest("PATCH", "/host/database/"+strconv.Itoa(database), []byte{2, 3}) assertion.Equal(w.StatusCode, http.StatusBadRequest) assertion.Equal(result.Data, false) @@ -112,7 +118,7 @@ func TestAPIDatabase(t *testing.T) { assertion.Equal(result.Data, true) result, w = session.JSONRequest("DELETE", "/host/database/"+strconv.Itoa(database), nil) - assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(w.StatusCode, http.StatusNotFound) assertion.Equal(result.Data, false) } diff --git a/modul/host/apidomain.go b/modul/host/apidomain.go index ce1fa05..b5af7e1 100644 --- a/modul/host/apidomain.go +++ b/modul/host/apidomain.go @@ -31,6 +31,7 @@ func getDomain(ctx context.Context, w http.ResponseWriter) (domain Domain, retur if domain.ID <= 0 { returnerr = &libapi.ErrorResult{Fields: []string{"domain"}, Message: "not found"} + w.WriteHeader(http.StatusNotFound) } return } diff --git a/modul/host/apidomain_test.go b/modul/host/apidomain_test.go index 197c9b9..915d128 100644 --- a/modul/host/apidomain_test.go +++ b/modul/host/apidomain_test.go @@ -110,6 +110,12 @@ func TestAPIDomain(t *testing.T) { assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, true) + result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(-1), Domain{ + Mail: true, + }) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain), []byte{2, 3}) assertion.Equal(w.StatusCode, http.StatusBadRequest) assertion.Equal(result.Data, false) @@ -125,11 +131,18 @@ func TestAPIDomain(t *testing.T) { loginTest(session, assertion) + result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(-1), nil) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain), nil) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, true) result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain), nil) - assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(w.StatusCode, http.StatusNotFound) assertion.Equal(result.Data, false) + + // CLEANUP + } diff --git a/modul/host/apimail.go b/modul/host/apimail.go new file mode 100644 index 0000000..8de7d07 --- /dev/null +++ b/modul/host/apimail.go @@ -0,0 +1,141 @@ +package host + +import ( + "net/http" + "strconv" + "strings" + + "goji.io/pat" + "golang.org/x/net/context" + + libapi "dev.sum7.eu/sum7/warehost/lib/api" + system "dev.sum7.eu/sum7/warehost/system" +) + +func getMail(ctx context.Context, w http.ResponseWriter) (mail Mail, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*system.Login) + profil := ctx.Value("profil").(*Profil) + id, err := strconv.ParseInt(pat.Param(ctx, "mailid"), 10, 64) + if err != nil { + returnerr = &libapi.ErrorResult{ + Message: "Internal Request Error", + } + w.WriteHeader(http.StatusBadRequest) + return + } + if login.Superadmin { + dbconnection.Where("id = ?", id).Find(&mail) + } else { + dbconnection.Where(map[string]int64{"ID": id, "domain.profil": profil.ID}).Find(&mail) + } + if mail.ID <= 0 { + returnerr = &libapi.ErrorResult{Fields: []string{"mail"}, Message: "not found"} + w.WriteHeader(http.StatusNotFound) + } + return +} + +func mailList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + returndata = false + logger := log.GetLog(r, "maillist") + var mail []*Mail + domain, returnerr := getDomain(ctx, w) + if returnerr != nil { + logger.Info("not found") + return + } + dbconnection.Where("domain = ?", domain.ID).Find(&mail) + logger.Info("done") + returndata = mail + return +} + +func mailAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + returndata = false + logger := log.GetLog(r, "mailadd") + + var mailRequest Mail + returnerr = libapi.JSONDecoder(r.Body, &mailRequest, w, logger) + if returnerr != nil { + return + } + + domain, returnerr := getDomain(ctx, w) + if returnerr != nil { + logger.Info("not found") + return + } + + mail := &Mail{ + DomainID: domain.ID, + Name: mailRequest.Name, + //Forward: mailRequest.Forward, + LoginID: mailRequest.LoginID, + } + + if err := dbconnection.Create(mail).Error; err != nil { + if strings.Contains(err.Error(), "duplicate key") { + logger.Warning("exists already") + return + } + logger.Error("database: during create host mail: ", err) + w.WriteHeader(http.StatusInternalServerError) + returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"} + return + } + returndata = true + logger.Info("done") + return +} + +func mailEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + returndata = false + logger := log.GetLog(r, "mailedit") + + mail, returnerr := getMail(ctx, w) + if returnerr != nil { + logger.Info("not found") + return + } + + var mailRequest Mail + returnerr = libapi.JSONDecoder(r.Body, &mailRequest, w, logger) + if returnerr != nil { + return + } + + mail.Name = mailRequest.Name + //mail.Forward = mailRequest.Forward + mail.LoginID = mailRequest.LoginID + + if err := dbconnection.Save(mail).Error; err != nil { + logger.Error("database: during modify host mail: ", err) + w.WriteHeader(http.StatusInternalServerError) + returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"} + return + } + returndata = true + logger.Info("done") + return +} + +func mailDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + returndata = false + logger := log.GetLog(r, "maildelete") + + mailRequest, returnerr := getMail(ctx, w) + if returnerr != nil { + logger.Info("not found") + return + } + + if err := dbconnection.Unscoped().Delete(mailRequest).Error; err != nil { + logger.Error("database: during create host mail: ", err) + w.WriteHeader(http.StatusInternalServerError) + returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"} + return + } + returndata = true + logger.Info("done") + return +} diff --git a/modul/host/apimail_test.go b/modul/host/apimail_test.go new file mode 100644 index 0000000..e6431e1 --- /dev/null +++ b/modul/host/apimail_test.go @@ -0,0 +1,173 @@ +package host + +import ( + "net/http" + "strconv" + "testing" + + "dev.sum7.eu/sum7/warehost/system" + "dev.sum7.eu/sum7/warehost/test" +) + +func TestAPIMail(t *testing.T) { + + assertion, db, router := test.Init(t) + defer db.Close() + + //load system Models to database + system.SyncModels(db) + db.Unscoped().Delete(Profil{}) + SyncModels(db) + + // Bind API + system.BindAPI(db, router, "") + BindAPI(db, router, "/host") + session := test.NewSession(router) + + loginTest(session, assertion) + + result, w := session.JSONRequest("DELETE", "/host/delete", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + + // Need a Profile for Next tests + result, w = session.JSONRequest("POST", "/host/signup", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, true) + + // Need a Domain for next tests + result, w = session.JSONRequest("POST", "/host/domain", Domain{ + FQDN: "example.de", + }) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, true) + + // Get id von domain + result, w = session.JSONRequest("GET", "/host/domain", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.NotEqual(result.Data, false) + var domain int + for _, obj := range result.Data.([]interface{}) { + item := obj.(map[string]interface{}) + if item["fqdn"] == "example.de" { + domain = int(item["ID"].(float64)) + break + } + } + /* + * TEST mailList + */ + session.Clean() + + result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/mail", nil) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(-1)+"/mail", nil) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/mail", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.NotEqual(result.Data, false) + + /* + * TEST mailAdd + */ + session.Clean() + + result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/mail", Mail{ + Name: "test-bug", + }) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/mail", []byte{2, 3}) + assertion.Equal(w.StatusCode, http.StatusBadRequest) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(-1)+"/mail", Mail{ + Name: "test-bug", + }) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/mail", Mail{ + Name: "test", + }) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, true) + + result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/mail", Mail{ + Name: "test", + }) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, false) + + /* + * TEST mailEdit + */ + result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/mail", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.NotEqual(result.Data, false) + var mail int + for _, obj := range result.Data.([]interface{}) { + item := obj.(map[string]interface{}) + if item["name"] == "test" { + mail = int(item["ID"].(float64)) + break + } + } + session.Clean() + + result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), Mail{ + Name: "test-bug-auth", + }) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), []byte{2, 3}) + assertion.Equal(w.StatusCode, http.StatusBadRequest) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(-1), Mail{ + Name: "test-bug", + }) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), Mail{ + Name: "test", + }) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, true) + + /* + * TEST domainDelete + */ + session.Clean() + + result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), nil) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(-1), nil) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, true) + + result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/mail/"+strconv.Itoa(mail), nil) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + +} diff --git a/modul/host/apiweb.go b/modul/host/apiweb.go new file mode 100644 index 0000000..134ec4f --- /dev/null +++ b/modul/host/apiweb.go @@ -0,0 +1,151 @@ +package host + +import ( + "net/http" + "strconv" + "strings" + + "goji.io/pat" + "golang.org/x/net/context" + + libapi "dev.sum7.eu/sum7/warehost/lib/api" + system "dev.sum7.eu/sum7/warehost/system" +) + +func getWeb(ctx context.Context, w http.ResponseWriter) (web Web, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*system.Login) + profil := ctx.Value("profil").(*Profil) + id, err := strconv.ParseInt(pat.Param(ctx, "webid"), 10, 64) + if err != nil { + returnerr = &libapi.ErrorResult{ + Message: "Internal Request Error", + } + w.WriteHeader(http.StatusBadRequest) + return + } + if login.Superadmin { + dbconnection.Where("id = ?", id).Find(&web) + } else { + dbconnection.Where(map[string]int64{"ID": id, "domain.profil": profil.ID}).Find(&web) + } + if web.ID <= 0 { + returnerr = &libapi.ErrorResult{Fields: []string{"web"}, Message: "not found"} + w.WriteHeader(http.StatusNotFound) + } + return +} + +func webList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + returndata = false + logger := log.GetLog(r, "weblist") + var web []*Web + domain, returnerr := getDomain(ctx, w) + if returnerr != nil { + logger.Info("not found") + return + } + dbconnection.Where("domain = ?", domain.ID).Find(&web) + logger.Info("done") + returndata = web + return +} + +func webAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + returndata = false + logger := log.GetLog(r, "webadd") + + var webRequest Web + returnerr = libapi.JSONDecoder(r.Body, &webRequest, w, logger) + if returnerr != nil { + return + } + + domain, returnerr := getDomain(ctx, w) + if returnerr != nil { + logger.Info("not found") + return + } + + web := &Web{ + DomainID: domain.ID, + Subdomain: webRequest.Subdomain, + PHP: webRequest.PHP, + SSL: webRequest.SSL, + SSLRedirect: webRequest.SSLRedirect, + Redirect: webRequest.Redirect, + Proxy: webRequest.Proxy, + //FTP: webRequest.FTP, + //HTTPAccess: webRequest.HTTPAccess, + } + + if err := dbconnection.Create(web).Error; err != nil { + if strings.Contains(err.Error(), "duplicate key") { + logger.Warning("exists already") + return + } + logger.Error("database: during create host web: ", err) + w.WriteHeader(http.StatusInternalServerError) + returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"} + return + } + returndata = true + logger.Info("done") + return +} + +func webEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + returndata = false + logger := log.GetLog(r, "webedit") + + web, returnerr := getWeb(ctx, w) + if returnerr != nil { + logger.Info("not found") + return + } + + var webRequest Web + returnerr = libapi.JSONDecoder(r.Body, &webRequest, w, logger) + if returnerr != nil { + 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 + //web.FTP = webRequest.FTP + //web.HTTPAccess = webRequest.HTTPAccess + + if err := dbconnection.Save(web).Error; err != nil { + logger.Error("database: during modify host web: ", err) + w.WriteHeader(http.StatusInternalServerError) + returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"} + return + } + returndata = true + logger.Info("done") + return +} + +func webDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + returndata = false + logger := log.GetLog(r, "webdelete") + + webRequest, returnerr := getWeb(ctx, w) + if returnerr != nil { + logger.Info("not found") + return + } + + if err := dbconnection.Unscoped().Delete(webRequest).Error; err != nil { + logger.Error("database: during create host web: ", err) + w.WriteHeader(http.StatusInternalServerError) + returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"} + return + } + returndata = true + logger.Info("done") + return +} diff --git a/modul/host/apiweb_test.go b/modul/host/apiweb_test.go new file mode 100644 index 0000000..68d1062 --- /dev/null +++ b/modul/host/apiweb_test.go @@ -0,0 +1,173 @@ +package host + +import ( + "net/http" + "strconv" + "testing" + + "dev.sum7.eu/sum7/warehost/system" + "dev.sum7.eu/sum7/warehost/test" +) + +func TestAPIWeb(t *testing.T) { + + assertion, db, router := test.Init(t) + defer db.Close() + + //load system Models to database + system.SyncModels(db) + db.Unscoped().Delete(Profil{}) + SyncModels(db) + + // Bind API + system.BindAPI(db, router, "") + BindAPI(db, router, "/host") + session := test.NewSession(router) + + loginTest(session, assertion) + + result, w := session.JSONRequest("DELETE", "/host/delete", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + + // Need a Profile for Next tests + result, w = session.JSONRequest("POST", "/host/signup", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, true) + + // Need a Domain for next tests + result, w = session.JSONRequest("POST", "/host/domain", Domain{ + FQDN: "example.de", + }) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, true) + + // Get id von domain + result, w = session.JSONRequest("GET", "/host/domain", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.NotEqual(result.Data, false) + var domain int + for _, obj := range result.Data.([]interface{}) { + item := obj.(map[string]interface{}) + if item["fqdn"] == "example.de" { + domain = int(item["ID"].(float64)) + break + } + } + /* + * TEST webList + */ + session.Clean() + + result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/web", nil) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(-1)+"/web", nil) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/web", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.NotEqual(result.Data, false) + + /* + * TEST webAdd + */ + session.Clean() + + result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", Web{ + Subdomain: "", + }) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", []byte{2, 3}) + assertion.Equal(w.StatusCode, http.StatusBadRequest) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(-1)+"/web", Web{ + Subdomain: "", + }) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", Web{ + Subdomain: "", + }) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, true) + + result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", Web{ + Subdomain: "", + }) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, false) + + /* + * TEST webEdit + */ + result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/web", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.NotEqual(result.Data, false) + var web int + for _, obj := range result.Data.([]interface{}) { + item := obj.(map[string]interface{}) + if item["subdomain"] == "" { + web = int(item["ID"].(float64)) + break + } + } + session.Clean() + + result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), Web{ + Subdomain: "test-bug-auth", + }) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), []byte{2, 3}) + assertion.Equal(w.StatusCode, http.StatusBadRequest) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(-1), Web{ + Subdomain: "test-bug", + }) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), Web{ + Subdomain: "test", + }) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, true) + + /* + * TEST domainDelete + */ + session.Clean() + + result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), nil) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(-1), nil) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + + result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, true) + + result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), nil) + assertion.Equal(w.StatusCode, http.StatusNotFound) + assertion.Equal(result.Data, false) + +} diff --git a/modul/host/models.go b/modul/host/models.go index 551f7b8..994d0b6 100644 --- a/modul/host/models.go +++ b/modul/host/models.go @@ -1,6 +1,8 @@ package host import ( + "database/sql" + "github.com/jinzhu/gorm" "dev.sum7.eu/sum7/warehost/system" @@ -36,15 +38,15 @@ func (Domain) TableName() string { return "host_domain" } type Web struct { ID int64 DomainID int64 `sql:"type:bigint NOT NULL REFERENCES host_domain(id) ON UPDATE CASCADE ON DELETE CASCADE;column:domain" json:"-"` - Domain *Domain `gorm:"foreignkey:Domain;" json:"domain"` - Subdomain string `sql:"type:varchar(255);column:subdomain" json:"subdomain"` + Domain *Domain `gorm:"foreignkey:Domain;unique_index:idx_host_domain_web" json:"domain"` + Subdomain string `sql:"type:varchar(255);column:subdomain" gorm:"unique_index:idx_host_domain_web" json:"subdomain"` PHP bool `sql:"default:false;column:php" json:"php"` SSL bool `sql:"default:true;column:ssl" json:"ssl"` SSLRedirect bool `sql:"default:false;column:sslredirect" json:"sslredirect"` Redirect string `sql:"type:varchar(255);column:redirect" json:"redirect"` Proxy string `sql:"type:varchar(255);column:proxy" json:"proxy"` - FTP []int64 `sql:"type:bigint[];column:ftp" json:"ftp"` - HTTPAccess []int64 `sql:"type:bigint[];column:httpaccess" json:"httpaccess"` + //FTP []int64 `sql:"type:bigint[];column:ftp" json:"ftp"` + //HTTPAccess []int64 `sql:"type:bigint[];column:httpaccess" json:"httpaccess"` } // TableName of struct @@ -53,11 +55,11 @@ func (Web) TableName() string { return "host_web" } // Mail struct type Mail struct { ID int64 - DomainID int64 `sql:"type:bigint NOT NULL REFERENCES host_domain(id) ON UPDATE CASCADE ON DELETE CASCADE;column:domain" json:"-"` - Domain *Domain `gorm:"foreignkey:Domain;" json:"domain"` - Name string `sql:"type:varchar(255);column:name" json:"name"` - Forward []string `sql:"type:varchar(255)[];column:forward" json:"forward"` - LoginID int64 `sql:"type:bigint NOT NULL REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login" json:"login"` + DomainID int64 `sql:"type:bigint NOT NULL REFERENCES host_domain(id) ON UPDATE CASCADE ON DELETE CASCADE;column:domain" json:"-"` + Domain *Domain `gorm:"foreignkey:Domain;unique_index:idx_host_domain_mail" json:"domain"` + Name string `sql:"type:varchar(255);column:name" gorm:"unique_index:idx_host_domain_mail" json:"name"` + //Forward []string `sql:"type:varchar(255)[];column:forward" json:"forward"` + LoginID sql.NullInt64 `sql:"type:bigint NULL REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login" json:"login"` } // TableName of struct diff --git a/system/api.go b/system/api.go index cb0aab6..4daea6f 100644 --- a/system/api.go +++ b/system/api.go @@ -291,6 +291,7 @@ func loginEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (ret if login.Superadmin { invitedLogin.Username = changeLogin.Username invitedLogin.Superadmin = changeLogin.Superadmin + invitedLogin.Active = changeLogin.Active } if err := dbconnection.Save(invitedLogin).Error; err != nil { logger.Warn("sql edit login") diff --git a/system/models.go b/system/models.go index 9ebf4df..accb19e 100644 --- a/system/models.go +++ b/system/models.go @@ -21,6 +21,7 @@ type RequestLogin struct { Username string `json:"username"` Password string `json:"password"` Superadmin bool `json:"superadmin,oemitempty"` + Active bool `json:"active,oemitempty"` } // ChangePasswordRequest for api request of a new password