sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

[host] add some things

This commit is contained in:
Martin Geno 2016-10-17 11:54:35 +02:00
parent b2ccc6c987
commit d3b8a5c5ae
6 changed files with 128 additions and 13 deletions

View File

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

View File

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

View File

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

31
modul/host/apidomain.go Normal file
View File

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

View File

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

View File

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