/
role.go
206 lines (174 loc) · 5.34 KB
/
role.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright ADH Partnership
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package user
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/adh-partnership/api/pkg/auth"
"github.com/adh-partnership/api/pkg/database"
models "github.com/adh-partnership/api/pkg/database/models"
"github.com/adh-partnership/api/pkg/discord"
"github.com/adh-partnership/api/pkg/gin/response"
"github.com/adh-partnership/api/pkg/memcache"
)
// Get User Information
// @Summary Get User Information
// @Tags user
// @Param cid path string false "CID, if not provided, defaults to logged in user"
// @Success 200 {object} []string
// @Failure 403 {object} response.R
// @Failure 500 {object} response.R
// @Router /v1/user/:cid/roles [GET]
func getUserRoles(c *gin.Context) {
var err error
user := c.MustGet("x-user").(*models.User)
if c.Param("cid") != "" {
user, err = database.FindUserByCID(c.Param("cid"))
if err != nil {
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
if user == nil {
response.RespondError(c, http.StatusNotFound, "User not found")
return
}
}
roles := []string{}
for _, role := range user.Roles {
roles = append(roles, role.Name)
}
response.Respond(c, http.StatusOK, roles)
}
// Add User Role
// @Summary Add User Role
// @Tags user
// @Param role path string true "Role"
// @Param cid path string true "CID"
// @Success 204
// @Failure 309 {object} response.R
// @Failure 403 {object} response.R
// @Failure 404 {object} response.R
// @Failure 500 {object} response.R
// @Router /v1/user/:cid/roles/:role [PUT]
func putUserRoles(c *gin.Context) {
reqUser := c.MustGet("x-user").(*models.User)
user, err := database.FindUserByCID(c.Param("cid"))
if err != nil {
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
if user == nil {
response.RespondError(c, http.StatusNotFound, "User not found")
return
}
role := c.Param("role")
if _, ok := auth.Roles[role]; !ok {
response.RespondError(c, http.StatusNotFound, "Role not found")
return
}
if auth.HasRole(user, role) {
response.RespondError(c, http.StatusConflict, "Conflict")
return
}
if !auth.CanUserModifyRole(reqUser, role) {
response.RespondError(c, http.StatusForbidden, "Forbidden")
return
}
dbrole, err := database.FindRole(role)
if err != nil {
log.Errorf("Error finding role %s: %s", role, err)
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
if err = database.AddRoleToUser(user, dbrole); err != nil {
log.Errorf("Error adding role %s to %d: %s", role, user.CID, err)
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
_ = discord.NewMessage().
SetContent(
fmt.Sprintf("%s %s has added role %s to %s %s (%d)",
reqUser.FirstName,
reqUser.LastName,
role,
user.FirstName,
user.LastName,
user.CID)).
Send("role")
// If roles change, invalidate staff cache
memcache.Cache.Delete("staff")
response.RespondBlank(c, http.StatusNoContent)
}
// Remove User Role
// @Summary Remove User Role
// @Tags user
// @Param role path string true "Role"
// @Param cid path string true "CID"
// @Success 204
// @Failure 403 {object} response.R
// @Failure 404 {object} response.R
// @Failure 500 {object} response.R
// @Router /v1/user/:cid/roles/:role [DELETE]
func deleteUserRoles(c *gin.Context) {
reqUser := c.MustGet("x-user").(*models.User)
user, err := database.FindUserByCID(c.Param("cid"))
if err != nil {
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
if user == nil {
response.RespondError(c, http.StatusNotFound, "User not found")
return
}
role := c.Param("role")
if _, ok := auth.Roles[role]; !ok {
response.RespondError(c, http.StatusNotFound, "Role not found")
return
}
if !auth.HasRole(user, role) {
response.RespondError(c, http.StatusNotFound, "Role not found")
return
}
if !auth.CanUserModifyRole(reqUser, role) {
response.RespondError(c, http.StatusForbidden, "Forbidden")
return
}
dbrole, err := database.FindRole(role)
if err != nil {
log.Errorf("Error finding role %s: %s", role, err)
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
if err = database.RemoveRoleFromUser(user, dbrole); err != nil {
log.Errorf("Error removing role %s from %d: %s", role, user.CID, err)
response.RespondError(c, http.StatusInternalServerError, "Internal Server Error")
return
}
_ = discord.NewMessage().
SetContent(
fmt.Sprintf("%s %s has removed role %s from %s %s (%d)",
reqUser.FirstName,
reqUser.LastName,
role,
user.FirstName,
user.LastName,
user.CID)).
Send("role")
// If roles change, invalidate staff cache
memcache.Cache.Delete("staff")
response.RespondBlank(c, http.StatusNoContent)
}