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
Raw Permalink Normal View History

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