package host import ( "net/http" "strings" "github.com/jinzhu/gorm" "goji.io" "goji.io/pat" "golang.org/x/net/context" libapi "dev.sum7.eu/sum7/warehost/lib/api" liblog "dev.sum7.eu/sum7/warehost/lib/log" system "dev.sum7.eu/sum7/warehost/system" ) //MODULNAME to get global name for the modul const MODULNAME = "host" 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) 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.Post(prefix+"/domain"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainAdd)))) 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)))) router.HandleFuncC(pat.Get(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseList)))) router.HandleFuncC(pat.Post(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseAdd)))) 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)))) } 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, "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 } 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 } return } func delete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) { profil := ctx.Value("profil").(*Profil) returndata = true 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 }