sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/modul/host/api.go

120 lines
5.2 KiB
Go
Raw Permalink Normal View History

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"
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-12-19 12:24:18 +01:00
router.HandleFunc(pat.Post(prefix+"/signup"), libapi.SessionHandler(system.LoginHandler(signup)))
router.HandleFunc(pat.Get(prefix+"/signup"), libapi.SessionHandler(system.LoginHandler(checkSignup)))
router.HandleFunc(pat.Delete(prefix+"/delete"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(delete))))
router.HandleFunc(pat.Get(prefix+"/profil"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(profil))))
2016-12-19 12:24:18 +01:00
router.HandleFunc(pat.Get(prefix+"/domain"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainList))))
router.HandleFunc(pat.Get(prefix+"/domain/:domainid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainShow))))
router.HandleFunc(pat.Post(prefix+"/domain"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainAdd))))
router.HandleFunc(pat.Patch(prefix+"/domain/:domainid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainEdit))))
router.HandleFunc(pat.Delete(prefix+"/domain/:domainid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainDelete))))
2016-12-19 12:24:18 +01:00
router.HandleFunc(pat.Get(prefix+"/domain/:domainid/web"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(webList))))
router.HandleFunc(pat.Post(prefix+"/domain/:domainid/web"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(webAdd))))
router.HandleFunc(pat.Patch(prefix+"/domain/:domainid/web/:webid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(webEdit))))
router.HandleFunc(pat.Delete(prefix+"/domain/:domainid/web/:webid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(webDelete))))
2016-10-21 21:32:30 +02:00
2016-12-19 12:24:18 +01:00
router.HandleFunc(pat.Get(prefix+"/domain/:domainid/mail"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(mailList))))
router.HandleFunc(pat.Post(prefix+"/domain/:domainid/mail"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(mailAdd))))
router.HandleFunc(pat.Patch(prefix+"/domain/:domainid/mail/:mailid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(mailEdit))))
router.HandleFunc(pat.Delete(prefix+"/domain/:domainid/mail/:mailid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(mailDelete))))
2016-10-21 21:32:30 +02:00
2016-12-19 12:24:18 +01:00
router.HandleFunc(pat.Get(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseList))))
router.HandleFunc(pat.Post(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseAdd))))
router.HandleFunc(pat.Patch(prefix+"/database/:databaseid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseEdit))))
router.HandleFunc(pat.Delete(prefix+"/database/:databaseid"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseDelete))))
// ADMIN APIS
2016-12-19 12:24:18 +01:00
router.HandleFunc(pat.Get(prefix+"/profils"), libapi.SessionHandler(system.LoginHandler(profilList)))
router.HandleFunc(pat.Patch(prefix+"/profil/:id"), libapi.SessionHandler(system.LoginHandler(toggleReseller)))
2016-09-01 22:19:39 +02:00
}
2016-12-19 12:24:18 +01:00
func checkSignupAllow(login *system.Login) bool {
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-12-19 12:24:18 +01:00
return run
}
func checkSignup(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
login := ctx.Value("login").(*system.Login)
returndata := checkSignupAllow(login)
logger := log.GetLog(r, "checksignup")
if returndata {
2016-10-17 11:54:35 +02:00
logger.Info("done")
2016-12-19 12:24:18 +01:00
} else {
logger.Info("not allowed")
2016-10-17 11:54:35 +02:00
}
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, returndata, nil)
2016-10-17 11:54:35 +02:00
}
2016-12-19 12:24:18 +01:00
func signup(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
2016-10-17 11:54:35 +02:00
login := ctx.Value("login").(*system.Login)
logger := log.GetLog(r, "signup")
2016-12-19 12:24:18 +01:00
if checkSignupAllow(login) {
2016-10-16 19:00:53 +02:00
profil := &Profil{LoginID: login.ID}
if err := dbconnection.Create(profil).Error; err != nil {
2016-12-19 12:24:18 +01:00
w.WriteHeader(http.StatusInternalServerError)
2016-10-16 19:00:53 +02:00
if strings.Contains(err.Error(), "duplicate key") {
logger.Warning("exists already")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "already signup"})
2016-10-16 19:00:53 +02:00
return
}
2016-10-17 11:54:35 +02:00
logger.Error("database: during create host profil: ", err)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
2016-10-17 11:54:35 +02:00
return
2016-10-16 19:00:53 +02:00
}
logger.Info("done")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
2016-10-16 19:00:53 +02:00
return
}
2016-12-19 12:24:18 +01:00
w.WriteHeader(http.StatusUnauthorized)
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "You are not allowed to signup"})
2016-10-16 19:00:53 +02:00
}
2016-12-19 12:24:18 +01:00
func delete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := log.GetLog(r, "delete")
2016-10-16 19:00:53 +02:00
profil := ctx.Value("profil").(*Profil)
dbconnection.Unscoped().Delete(profil)
2016-12-19 12:24:18 +01:00
logger.Info("done")
libapi.JSONWrite(w, r, true, nil)
2016-09-01 22:19:39 +02:00
}
2016-12-19 12:24:18 +01:00
func profil(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := log.GetLog(r, "profil")
2016-10-17 11:54:35 +02:00
profil := ctx.Value("profil").(*Profil)
2016-12-19 12:24:18 +01:00
logger.Info("done")
libapi.JSONWrite(w, r, profil, nil)
2016-10-17 11:54:35 +02:00
}