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/web/apipermission.go

67 lines
2.0 KiB
Go

package web
import (
"net/http"
"strconv"
"goji.io/pat"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
)
// PermissionList to add permissions
func permissionList(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := log.GetLog(r, "permissionlist")
var involved []*Manager
dbconnection.Where("website = ?", ctx.Value("websiteid").(int64)).Preload("Login").Find(&involved)
logger.Info("done")
libapi.JSONWrite(w, r, involved, nil)
}
// PermissionAdd to add permissions
func permissionAdd(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := log.GetLog(r, "permissionadd")
loginid, err := strconv.ParseInt(pat.Param(r, "loginid"), 10, 64)
if err != nil {
logger.Warn("invalid loginid, no integer")
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"loginid"}, Message: "Not a valid loginid"})
return
}
manager := &Manager{
WebsiteID: ctx.Value("websiteid").(int64),
LoginID: loginid,
}
if err := dbconnection.Create(manager).Error; err != nil {
logger.Error("database: during create website permission")
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
return
}
logger.Info("done")
libapi.JSONWrite(w, r, true, nil)
}
// PermissionDelete to delete permissions
func permissionDelete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := log.GetLog(r, "permissiondelete")
loginid, err := strconv.ParseInt(pat.Param(r, "loginid"), 10, 64)
if err != nil {
logger.Warn("invalid loginid, no integer")
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"loginid"}, Message: "Not a valid loginid"})
return
}
manager := &Manager{
WebsiteID: ctx.Value("websiteid").(int64),
LoginID: loginid,
}
if err := dbconnection.Unscoped().Delete(manager).Error; err != nil {
logger.Error("database: during delete website permission")
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error"})
return
}
logger.Info("done")
libapi.JSONWrite(w, r, true, nil)
}