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/apiprofil.go

57 lines
1.7 KiB
Go
Raw Normal View History

package host
import (
"net/http"
"strconv"
"goji.io/pat"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
system "dev.sum7.eu/sum7/warehost/system"
)
2016-12-19 12:24:18 +01:00
func profilList(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
login := ctx.Value("login").(*system.Login)
logger := log.GetLog(r, "toggleReseller")
2017-06-02 10:15:09 +02:00
if !login.Superadmin {
logger.Warn("not a superadmin")
2016-12-19 12:24:18 +01:00
w.WriteHeader(http.StatusUnauthorized)
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"session"}, Message: "not a superadmin"})
return
}
var profils []*Profil
dbconnection.Preload("Login").Find(&profils)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, profils, nil)
}
2016-12-19 12:24:18 +01:00
func toggleReseller(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
login := ctx.Value("login").(*system.Login)
logger := log.GetLog(r, "toggleReseller")
if !login.Superadmin {
logger.Warn("not a superadmin")
2016-12-19 12:24:18 +01:00
w.WriteHeader(http.StatusUnauthorized)
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Fields: []string{"session"}, Message: "not a superadmin"})
return
}
2016-12-19 12:24:18 +01:00
id, err := strconv.ParseInt(pat.Param(r, "id"), 10, 64)
if err != nil {
logger.Warn("invalid userinput, no integer")
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Error invalid input"})
return
}
logger = logger.WithField("id", id)
var profil = Profil{ID: id}
dbconnection.Where("id = ?", profil.ID).First(&profil)
profil.Reseller = !profil.Reseller
if err := dbconnection.Save(profil).Error; err != nil {
logger.Error("database: during modify host profil: ", err)
w.WriteHeader(http.StatusInternalServerError)
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, false, &libapi.ErrorResult{Message: "Internal Database Error with Database"})
return
}
2016-12-19 12:24:18 +01:00
libapi.JSONWrite(w, r, true, nil)
}