From d3b8a5c5ae34908b8d9469bfcc4abc3d64d6bcdf Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Mon, 17 Oct 2016 11:54:35 +0200 Subject: [PATCH] [host] add some things --- build.sh | 2 +- modul/host/api.go | 38 ++++++++++++++++++------ modul/host/api_test.go | 64 +++++++++++++++++++++++++++++++++++++++++ modul/host/apidomain.go | 31 ++++++++++++++++++++ modul/host/lib.go | 4 +-- modul/host/models.go | 2 +- 6 files changed, 128 insertions(+), 13 deletions(-) create mode 100644 modul/host/apidomain.go diff --git a/build.sh b/build.sh index 07f4112..dd93c8f 100644 --- a/build.sh +++ b/build.sh @@ -3,7 +3,7 @@ P=$PWD; cd $GOPATH/src/dev.sum7.eu/sum7/warehost/cmd/warehost; -go install; +go install -tags all; cd $GOPATH/src/dev.sum7.eu/sum7/warehost/cmd/warehost-web; go install; diff --git a/modul/host/api.go b/modul/host/api.go index a41770a..6f8b8b7 100644 --- a/modul/host/api.go +++ b/modul/host/api.go @@ -26,39 +26,54 @@ func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) { log = liblog.NewModulLog(MODULNAME) router.HandleFuncC(pat.Post(prefix+"/signup"), libapi.SessionHandler(system.LoginHandler(signup))) + router.HandleFuncC(pat.Get(prefix+"/signup"), libapi.SessionHandler(system.LoginHandler(checkSignup))) router.HandleFuncC(pat.Delete(prefix+"/delete"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(delete)))) + router.HandleFuncC(pat.Get(prefix+"/profil"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(profil)))) + router.HandleFuncC(pat.Get(prefix+"/domain"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainList)))) + router.HandleFuncC(pat.Get(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseList)))) } -func signup(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { +func checkSignup(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, "signup") + logger := log.GetLog(r, "checksignup") run := login.Superadmin if !run { var profil Profil dbconnection.Joins("LEFT JOIN invite invite ON invite.login=host_profil.login").Where("invite.invited=?", login.ID).Find(&profil) run = profil.Reseller } + returndata = run if run { + logger.Info("done") + return + } + w.WriteHeader(http.StatusUnauthorized) + logger.Info("not allowed") + return +} + +func signup(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + login := ctx.Value("login").(*system.Login) + logger := log.GetLog(r, "signup") + returndata, returnerr = checkSignup(ctx, w, r) + if returndata.(bool) { profil := &Profil{LoginID: login.ID} if err := dbconnection.Create(profil).Error; err != nil { if strings.Contains(err.Error(), "duplicate key") { returndata = false logger.Warning("exists already") return - } else { - logger.Error("database: during create host profil: ", err) - returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} - w.WriteHeader(http.StatusInternalServerError) - return } + logger.Error("database: during create host profil: ", err) + returnerr = &libapi.ErrorResult{Message: "Internal Database Error"} + w.WriteHeader(http.StatusInternalServerError) + return } returndata = true logger.Info("done") return } - w.WriteHeader(http.StatusUnauthorized) - logger.Info("not allowed") return } func delete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { @@ -67,3 +82,8 @@ func delete(ctx context.Context, w http.ResponseWriter, r *http.Request) (return dbconnection.Unscoped().Delete(profil) return } +func profil(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + profil := ctx.Value("profil").(*Profil) + returndata = profil + return +} diff --git a/modul/host/api_test.go b/modul/host/api_test.go index 7d72892..fa76a36 100644 --- a/modul/host/api_test.go +++ b/modul/host/api_test.go @@ -48,6 +48,20 @@ func TestAPI(t *testing.T) { assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, false) + /* + * TEST checksignup + */ + session.Clean() + result, w = session.JSONRequest("GET", "/host/signup", nil) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("GET", "/host/signup", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, true) + /* * TEST delete */ @@ -62,4 +76,54 @@ func TestAPI(t *testing.T) { assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, true) + /* + * TEST profil + */ + session.Clean() + result, w = session.JSONRequest("GET", "/host/profil", nil) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("GET", "/host/profil", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.Equal(result.Data, false) + + // 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) + + result, w = session.JSONRequest("GET", "/host/profil", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.NotEqual(result.Data, false) + + /* + * TEST domainList + */ + session.Clean() + result, w = session.JSONRequest("GET", "/host/domain", nil) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("GET", "/host/domain", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.NotEqual(result.Data, false) + + /* + * TEST databaseList + */ + session.Clean() + result, w = session.JSONRequest("GET", "/host/database", nil) + assertion.Equal(w.StatusCode, http.StatusUnauthorized) + assertion.Equal(result.Data, false) + + loginTest(session, assertion) + + result, w = session.JSONRequest("GET", "/host/database", nil) + assertion.Equal(w.StatusCode, http.StatusOK) + assertion.NotEqual(result.Data, false) } diff --git a/modul/host/apidomain.go b/modul/host/apidomain.go new file mode 100644 index 0000000..7c33814 --- /dev/null +++ b/modul/host/apidomain.go @@ -0,0 +1,31 @@ +package host + +import ( + "net/http" + + "golang.org/x/net/context" + + libapi "dev.sum7.eu/sum7/warehost/lib/api" +) + +func domainList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + profil := ctx.Value("profil").(*Profil) + returndata = false + logger := log.GetLog(r, "domainlist") + var domain []*Domain + dbconnection.Where("profil = ?", profil.ID).Find(&domain) + logger.Info("done") + returndata = domain + return +} + +func databaseList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { + profil := ctx.Value("profil").(*Profil) + returndata = false + logger := log.GetLog(r, "databaselist") + var database []*Database + dbconnection.Where("profil = ?", profil.ID).Find(&database) + logger.Info("done") + returndata = database + return +} diff --git a/modul/host/lib.go b/modul/host/lib.go index 1637654..e907bc6 100644 --- a/modul/host/lib.go +++ b/modul/host/lib.go @@ -18,8 +18,8 @@ func ProfilHandler(h libapi.Handle) libapi.Handle { returndata = false profil := &Profil{LoginID: login.ID} - res := dbconnection.Find(profil) - if !res.RecordNotFound() { + dbconnection.Where("login = ?", login.ID).Find(profil) + if profil.ID > 0 { ctx = context.WithValue(ctx, "profil", profil) returndata, returnerr = h(ctx, w, r) return diff --git a/modul/host/models.go b/modul/host/models.go index 811db33..988c1b0 100644 --- a/modul/host/models.go +++ b/modul/host/models.go @@ -19,7 +19,7 @@ type Domain struct { ID int64 ProfilID int64 `sql:"type:bigint NOT NULL REFERENCES host_profil(id) ON UPDATE CASCADE ON DELETE CASCADE;column:profil" json:"profil"` FQDN string `sql:"type:varchar(255);column:fqdn" json:"fqdn"` - Code string `sql:"type:varchar(255);column:code" json:"code"` + Code string `sql:"type:varchar(255);column:code" json:"-"` Active bool `sql:"default:false;column:active" json:"active"` Mail bool `sql:"default:false;column:mail" json:"mail"` Web bool `sql:"default:false;column:web" json:"web"`