/
endpoints-groups.go
98 lines (66 loc) · 2.54 KB
/
endpoints-groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"net/http"
"github.com/caTUstrophy/backend/db"
"github.com/gin-gonic/gin"
)
// Functions
func (app *App) GetGroupObject(groupID string) db.Group {
var Group db.Group
app.DB.Preload("Users").First(&Group, "id = ?", groupID)
app.DB.Model(&Group).Related(&Group.Region)
return Group
}
func (app *App) GetGroups(c *gin.Context) {
// Check authorization for this function.
ok, User, message := app.Authorize(c.Request)
if !ok {
// Signal client an error and expect authorization.
c.Header("WWW-Authenticate", fmt.Sprintf("Bearer realm=\"CaTUstrophy\", error=\"invalid_token\", error_description=\"%s\"", message))
c.Status(http.StatusUnauthorized)
return
}
// Check if user permissions are sufficient (user is admin).
if ok = app.CheckScope(User, db.Region{}, "superadmin"); !ok {
// Signal client that the provided authorization was not sufficient.
c.Header("WWW-Authenticate", "Bearer realm=\"CaTUstrophy\", error=\"authentication_failed\", error_description=\"Could not authenticate the request\"")
c.Status(http.StatusUnauthorized)
return
}
var Groups []db.Group
app.DB.Preload("Users").Find(&Groups)
models := make([]map[string]interface{}, len(Groups))
// Iterate over all groups in database return and marshal it.
for i, group := range Groups {
// TODO: Resolve region in smarter way.
if group.RegionId != "" {
var Region db.Region
app.DB.First(&Region, "id = ?", group.RegionId)
group.Region = Region
}
models[i] = CopyNestedModel(group, fieldsGroup).(map[string]interface{})
}
c.JSON(http.StatusOK, models)
}
func (app *App) ListSystemAdmins(c *gin.Context) {
// Check authorization for this function.
ok, User, message := app.Authorize(c.Request)
if !ok {
// Signal client an error and expect authorization.
c.Header("WWW-Authenticate", fmt.Sprintf("Bearer realm=\"CaTUstrophy\", error=\"invalid_token\", error_description=\"%s\"", message))
c.Status(http.StatusUnauthorized)
return
}
// Check if user permissions are sufficient (user is admin).
if ok := app.CheckScope(User, db.Region{}, "superadmin"); !ok {
// Signal client that the provided authorization was not sufficient.
c.Header("WWW-Authenticate", "Bearer realm=\"CaTUstrophy\", error=\"authentication_failed\", error_description=\"Could not authenticate the request\"")
c.Status(http.StatusUnauthorized)
return
}
var group db.Group
app.DB.Preload("Users").First(&group, "access_right = ?", "superadmin")
model := CopyNestedModel(group.Users, fieldsSystemAdmin)
c.JSON(http.StatusOK, model)
}