From fd6fb63371cdbf071aba59ec297a1d3b66bac8c4 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Tue, 18 Oct 2016 19:28:57 +0200 Subject: [PATCH] [host] make domain and database admin accessable --- modul/host/apidatabase.go | 17 +++++++++++++++-- modul/host/apidatabase_test.go | 4 ++++ modul/host/apidomain.go | 21 +++++++++++++++++++-- modul/host/apidomain_test.go | 4 ++++ modul/host/models.go | 8 ++++---- modul/web/lib.go | 2 +- 6 files changed, 47 insertions(+), 9 deletions(-) diff --git a/modul/host/apidatabase.go b/modul/host/apidatabase.go index 8dd1ee8..37937d3 100644 --- a/modul/host/apidatabase.go +++ b/modul/host/apidatabase.go @@ -8,9 +8,11 @@ import ( "golang.org/x/net/context" libapi "dev.sum7.eu/sum7/warehost/lib/api" + system "dev.sum7.eu/sum7/warehost/system" ) func getDatabase(ctx context.Context, w http.ResponseWriter) (database Database, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*system.Login) profil := ctx.Value("profil").(*Profil) id, err := strconv.ParseInt(pat.Param(ctx, "databaseid"), 10, 64) if err != nil { @@ -20,7 +22,13 @@ func getDatabase(ctx context.Context, w http.ResponseWriter) (database Database, w.WriteHeader(http.StatusBadRequest) return } - dbconnection.Where(map[string]int64{"id": id, "profil": profil.ID}).Find(&database) + + if login.Superadmin { + dbconnection.Where("id = ?", id).Find(&database) + } else { + dbconnection.Where(map[string]int64{"id": id, "profil": profil.ID}).Find(&database) + } + if database.ID <= 0 { returnerr = &libapi.ErrorResult{Fields: []string{"database"}, Message: "not found"} } @@ -28,11 +36,16 @@ func getDatabase(ctx context.Context, w http.ResponseWriter) (database Database, } func databaseList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*system.Login) profil := ctx.Value("profil").(*Profil) returndata = false logger := log.GetLog(r, "databaselist") var database []*Database - dbconnection.Where("profil = ?", profil.ID).Find(&database) + if login.Superadmin && r.URL.Query().Get("filter") == "all" { + dbconnection.Find(&database) + } else { + dbconnection.Where("profil = ?", profil.ID).Find(&database) + } logger.Info("done") returndata = database return diff --git a/modul/host/apidatabase_test.go b/modul/host/apidatabase_test.go index 8086b1f..242ecd8 100644 --- a/modul/host/apidatabase_test.go +++ b/modul/host/apidatabase_test.go @@ -48,6 +48,10 @@ func TestAPIDatabase(t *testing.T) { assertion.Equal(w.StatusCode, http.StatusOK) assertion.NotEqual(result.Data, false) + result, w = session.JSONRequest("GET", "/host/database?filter=all", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.NotEqual(result.Data, false) + /* * TEST databaseAdd */ diff --git a/modul/host/apidomain.go b/modul/host/apidomain.go index 1ea3161..eb4e637 100644 --- a/modul/host/apidomain.go +++ b/modul/host/apidomain.go @@ -9,9 +9,11 @@ import ( "golang.org/x/net/context" libapi "dev.sum7.eu/sum7/warehost/lib/api" + system "dev.sum7.eu/sum7/warehost/system" ) func getDomain(ctx context.Context, w http.ResponseWriter) (domain Domain, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*system.Login) profil := ctx.Value("profil").(*Profil) id, err := strconv.ParseInt(pat.Param(ctx, "domainid"), 10, 64) if err != nil { @@ -21,7 +23,12 @@ func getDomain(ctx context.Context, w http.ResponseWriter) (domain Domain, retur w.WriteHeader(http.StatusBadRequest) return } - dbconnection.Where(map[string]int64{"id": id, "profil": profil.ID}).Find(&domain) + if login.Superadmin { + dbconnection.Where("id = ?", id).Find(&domain) + } else { + dbconnection.Where(map[string]int64{"id": id, "profil": profil.ID}).Find(&domain) + } + if domain.ID <= 0 { returnerr = &libapi.ErrorResult{Fields: []string{"domain"}, Message: "not found"} } @@ -29,11 +36,16 @@ func getDomain(ctx context.Context, w http.ResponseWriter) (domain Domain, retur } func domainList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*system.Login) profil := ctx.Value("profil").(*Profil) returndata = false logger := log.GetLog(r, "domainlist") var domain []*Domain - dbconnection.Where("profil = ?", profil.ID).Find(&domain) + if login.Superadmin && r.URL.Query().Get("filter") == "all" { + dbconnection.Find(&domain) + } else { + dbconnection.Where("profil = ?", profil.ID).Find(&domain) + } logger.Info("done") returndata = domain return @@ -73,6 +85,7 @@ func domainAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (ret } func domainEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*system.Login) returndata = false logger := log.GetLog(r, "domainedit") @@ -92,6 +105,10 @@ func domainEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (re domain.Mail = domainRequest.Mail domain.Web = domainRequest.Web + if login.Superadmin { + domain.Active = domainRequest.Active + } + if err := dbconnection.Save(domain).Error; err != nil { logger.Error("database: during modify host domain: ", err) w.WriteHeader(http.StatusInternalServerError) diff --git a/modul/host/apidomain_test.go b/modul/host/apidomain_test.go index d3434b6..197c9b9 100644 --- a/modul/host/apidomain_test.go +++ b/modul/host/apidomain_test.go @@ -48,6 +48,10 @@ func TestAPIDomain(t *testing.T) { assertion.Equal(w.StatusCode, http.StatusOK) assertion.NotEqual(result.Data, false) + result, w = session.JSONRequest("GET", "/host/domain?filter=all", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.NotEqual(result.Data, false) + /* * TEST domainAdd */ diff --git a/modul/host/models.go b/modul/host/models.go index de38698..7eb8292 100644 --- a/modul/host/models.go +++ b/modul/host/models.go @@ -48,10 +48,10 @@ 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"` - 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"` + 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"` } // TableName of struct diff --git a/modul/web/lib.go b/modul/web/lib.go index 45c68c1..b928c76 100644 --- a/modul/web/lib.go +++ b/modul/web/lib.go @@ -15,7 +15,7 @@ import ( //InvolveWebsiteHandler for api function to Verifie User ist loggedin func InvolveWebsiteHandler(h libapi.Handle) libapi.Handle { return func(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { - login := ctx.Value("login").(libsystem.Login) + login := ctx.Value("login").(*libsystem.Login) returnerr = &libapi.ErrorResult{Fields: []string{"session"}, Message: "Not logged in"} returndata = false