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

71 lines
2.2 KiB
Go
Raw 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"
"golang.org/x/net/context"
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-10-11 20:16:24 +02:00
func permissionList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
2016-09-11 18:40:33 +02:00
returndata = false
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")
returndata = involved
return
}
// PermissionAdd to add permissions
2016-10-11 20:16:24 +02:00
func permissionAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
2016-09-11 18:40:33 +02:00
returndata = false
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "permissionadd")
loginid, err := strconv.ParseInt(pat.Param(ctx, "loginid"), 10, 64)
2016-09-11 18:40:33 +02:00
if err != nil {
returnerr = &libapi.ErrorResult{Fields: []string{"loginid"}, Message: "Not a valid loginid"}
logger.Warn("invalid loginid, no integer")
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")
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
return
}
returndata = true
logger.Info("done")
return
}
// PermissionDelete to delete permissions
2016-10-11 20:16:24 +02:00
func permissionDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
2016-09-11 18:40:33 +02:00
returndata = false
2016-10-11 20:16:24 +02:00
logger := log.GetLog(r, "permissiondelete")
loginid, err := strconv.ParseInt(pat.Param(ctx, "loginid"), 10, 64)
2016-09-11 18:40:33 +02:00
if err != nil {
returnerr = &libapi.ErrorResult{Fields: []string{"loginid"}, Message: "Not a valid loginid"}
logger.Warn("invalid loginid, no integer")
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")
returnerr = &libapi.ErrorResult{Message: "Internal Database Error"}
return
}
returndata = true
logger.Info("done")
return
}