sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

[host] make domain and database admin accessable

This commit is contained in:
Martin Geno 2016-10-18 19:28:57 +02:00
parent 25a1f4f85b
commit fd6fb63371
6 changed files with 47 additions and 9 deletions

View File

@ -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

View File

@ -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
*/

View File

@ -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)

View File

@ -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
*/

View File

@ -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

View File

@ -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