207 lines
4.6 KiB
Go
207 lines
4.6 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.dynamicdiscord.de/malobeo/portal/openapi"
|
|
"git.dynamicdiscord.de/malobeo/portal/services"
|
|
)
|
|
|
|
type AccessAuthController struct{}
|
|
|
|
type GroupWrapper struct {
|
|
Name string
|
|
Id int32
|
|
}
|
|
|
|
type AccessAuthWithGroup struct {
|
|
AccessAuth openapi.AccessAuthorizationResponse
|
|
Groups []GroupWrapper
|
|
}
|
|
|
|
func (gc *AccessAuthController) AccessAuthView(c *gin.Context) {
|
|
accessAuths, err := services.AccessAuths.GetAll(c)
|
|
|
|
if err != nil {
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"data": gin.H{"error": err}})
|
|
}
|
|
|
|
aaWrapper := []AccessAuthWithGroup{}
|
|
for _, accessAuth := range accessAuths {
|
|
|
|
groupWrapper := []GroupWrapper{}
|
|
for _, group := range accessAuth.Groups {
|
|
groupWrapper = append(groupWrapper, GroupWrapper{
|
|
Name: group.Name,
|
|
Id: *group.Id.Get(),
|
|
})
|
|
|
|
}
|
|
aaWrapper = append(aaWrapper, AccessAuthWithGroup{
|
|
AccessAuth: accessAuth,
|
|
Groups: groupWrapper,
|
|
})
|
|
}
|
|
|
|
groups, err := services.Groups.GetAll(c)
|
|
|
|
if err != nil {
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"data": gin.H{"error": err}})
|
|
}
|
|
|
|
data := CreateSessionData(c, gin.H{
|
|
"accessAuths": aaWrapper,
|
|
"groups": groups,
|
|
})
|
|
|
|
if err != nil {
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", data)
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "accessauths.html", data)
|
|
|
|
}
|
|
|
|
func (gc *AccessAuthController) AddAccessAuthHandler(c *gin.Context) {
|
|
name := c.PostForm("name")
|
|
|
|
if len(name) == 0 {
|
|
fmt.Println("Adding accessAuth with empty name is forbidden")
|
|
c.HTML(http.StatusBadRequest, "accessAuths.html", gin.H{"error": "Cant create accessAuth without name"})
|
|
return
|
|
}
|
|
|
|
_, err := services.AccessAuths.Create(c, name)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
|
|
gc.AccessAuthView(c)
|
|
}
|
|
|
|
func (gc *AccessAuthController) AccessAuthHandler(c *gin.Context) {
|
|
action := c.PostForm("action")
|
|
idStr := c.Param("id")
|
|
fmt.Println("AA ID: ")
|
|
fmt.Println(idStr)
|
|
id, err := strconv.Atoi(idStr)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
|
|
if action == "createTimetable" {
|
|
fmt.Println(c)
|
|
weekdayStr := c.PostForm("weekday")
|
|
starttime := c.PostForm("starttime")
|
|
durationStr := c.PostForm("duration")
|
|
duration, err := strconv.Atoi(durationStr)
|
|
fmt.Printf("Creating timetable with %s, %s, %d", weekdayStr, starttime, duration)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
|
|
weekday, err := strconv.Atoi(weekdayStr)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
|
|
_, err = services.AccessAuths.AddTimetable(c, int32(id), int32(weekday), starttime, int32(duration))
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
}
|
|
|
|
if action == "addGroups" {
|
|
fmt.Println("ADD GROUp")
|
|
groupIds := c.PostFormArray("groups[]")
|
|
|
|
for _, groupIdStr := range groupIds {
|
|
groupId, err := strconv.Atoi(groupIdStr)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
|
|
err = services.AccessAuths.AddGroup(c, int32(id), int32(groupId))
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
if action == "removeGroup" {
|
|
fmt.Println("REMOVE GROUp")
|
|
fmt.Println(c)
|
|
groupIdStr := c.PostForm("groupId")
|
|
fmt.Println(groupIdStr)
|
|
groupId, err := strconv.Atoi(groupIdStr)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
|
|
err = services.AccessAuths.RemoveGroup(c, int32(id), int32(groupId))
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
}
|
|
|
|
if action == "activate" {
|
|
_, err := services.AccessAuths.SetActive(c, int32(id), true)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
}
|
|
|
|
if action == "deactivate" {
|
|
_, err := services.AccessAuths.SetActive(c, int32(id), false)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
}
|
|
|
|
if action == "delete" {
|
|
err := services.AccessAuths.Delete(c, int32(id))
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
c.HTML(http.StatusBadRequest, "accessauths.html", gin.H{"error": err})
|
|
return
|
|
}
|
|
}
|
|
|
|
gc.AccessAuthView(c)
|
|
}
|