2016-09-01 22:19:39 +02:00
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-10-16 19:00:53 +02:00
|
|
|
"strings"
|
2016-09-01 22:19:39 +02:00
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2016-10-11 20:16:24 +02:00
|
|
|
"goji.io"
|
|
|
|
"goji.io/pat"
|
|
|
|
"golang.org/x/net/context"
|
2016-09-01 22:19:39 +02:00
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
|
|
|
liblog "dev.sum7.eu/sum7/warehost/lib/log"
|
2016-10-16 19:00:53 +02:00
|
|
|
system "dev.sum7.eu/sum7/warehost/system"
|
2016-09-01 22:19:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
//MODULNAME to get global name for the modul
|
|
|
|
const MODULNAME = "host"
|
|
|
|
|
2016-10-11 20:16:24 +02:00
|
|
|
var dbconnection *gorm.DB
|
|
|
|
var log *liblog.ModulLog
|
|
|
|
|
|
|
|
// BindAPI sets the routes to the api functions
|
|
|
|
func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) {
|
|
|
|
dbconnection = db
|
|
|
|
log = liblog.NewModulLog(MODULNAME)
|
2016-09-01 22:19:39 +02:00
|
|
|
|
2016-10-16 19:00:53 +02:00
|
|
|
router.HandleFuncC(pat.Post(prefix+"/signup"), libapi.SessionHandler(system.LoginHandler(signup)))
|
2016-10-17 11:54:35 +02:00
|
|
|
router.HandleFuncC(pat.Get(prefix+"/signup"), libapi.SessionHandler(system.LoginHandler(checkSignup)))
|
2016-10-16 19:00:53 +02:00
|
|
|
router.HandleFuncC(pat.Delete(prefix+"/delete"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(delete))))
|
2016-10-17 11:54:35 +02:00
|
|
|
router.HandleFuncC(pat.Get(prefix+"/profil"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(profil))))
|
2016-10-17 19:21:17 +02:00
|
|
|
|
2016-10-17 11:54:35 +02:00
|
|
|
router.HandleFuncC(pat.Get(prefix+"/domain"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainList))))
|
2016-10-17 14:07:17 +02:00
|
|
|
router.HandleFuncC(pat.Post(prefix+"/domain"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainAdd))))
|
2016-10-17 19:21:17 +02:00
|
|
|
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))))
|
|
|
|
|
2016-10-17 11:54:35 +02:00
|
|
|
router.HandleFuncC(pat.Get(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseList))))
|
2016-10-17 14:07:17 +02:00
|
|
|
router.HandleFuncC(pat.Post(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseAdd))))
|
2016-10-17 19:21:17 +02:00
|
|
|
router.HandleFuncC(pat.Patch(prefix+"/database/:databaseid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseEdit))))
|
|
|
|
router.HandleFuncC(pat.Delete(prefix+"/database/:databaseid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseDelete))))
|
2016-09-01 22:19:39 +02:00
|
|
|
}
|
|
|
|
|
2016-10-17 11:54:35 +02:00
|
|
|
func checkSignup(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-10-16 19:00:53 +02:00
|
|
|
login := ctx.Value("login").(*system.Login)
|
|
|
|
returndata = false
|
2016-10-17 11:54:35 +02:00
|
|
|
logger := log.GetLog(r, "checksignup")
|
2016-10-16 19:00:53 +02:00
|
|
|
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
|
|
|
|
}
|
2016-10-17 11:54:35 +02:00
|
|
|
returndata = run
|
2016-10-16 19:00:53 +02:00
|
|
|
if run {
|
2016-10-17 11:54:35 +02:00
|
|
|
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) {
|
2016-10-16 19:00:53 +02:00
|
|
|
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
|
|
|
|
}
|
2016-10-17 11:54:35 +02:00
|
|
|
logger.Error("database: during create host profil: ", err)
|
|
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
2016-10-16 19:00:53 +02:00
|
|
|
}
|
|
|
|
returndata = true
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
func delete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
|
|
profil := ctx.Value("profil").(*Profil)
|
2016-09-01 22:19:39 +02:00
|
|
|
returndata = true
|
2016-10-16 19:00:53 +02:00
|
|
|
dbconnection.Unscoped().Delete(profil)
|
2016-09-01 22:19:39 +02:00
|
|
|
return
|
|
|
|
}
|
2016-10-17 11:54:35 +02:00
|
|
|
func profil(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
|
|
profil := ctx.Value("profil").(*Profil)
|
|
|
|
returndata = profil
|
|
|
|
return
|
|
|
|
}
|