/
permission.go
367 lines (306 loc) · 8.75 KB
/
permission.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Revision History:
* Initial: 2020/10/05 Abserari
*/
package controller
import (
"database/sql"
"log"
"net/http"
mysql "github.com/abserari/shower/pkgs/permission/model/mysql"
"github.com/gin-gonic/gin"
)
var (
admin = "userAuth"
intro = "use for test. Have any permission to API"
)
// Controller external service interface
type Controller struct {
db *sql.DB
getIDFunc func(c *gin.Context) (uint32, error)
}
// New create an external service interface
func New(db *sql.DB, getID func(c *gin.Context) (uint32, error)) *Controller {
return &Controller{
db: db,
getIDFunc: getID,
}
}
// todo: add configuration on userAuth-defined API
func (c *Controller) InitWithUserAPI() {
c.initWithRole()
}
func (c *Controller) initWithRole() {}
//RegisterRouter register router and from now on, every API would check if valid on current AdminID.
// Should init the permission to the API.
func (c *Controller) RegisterRouter(r gin.IRouter) {
err := mysql.CreateTable(c.db)
if err != nil {
log.Fatal(err)
}
// init with userAuth defined API.
c.InitWithUserAPI()
// choose if from now on, every API would check if valid on current AdminID.
// r.Use(c.CheckPermission())
// role table
r.POST("/addrole", c.createRole)
r.POST("/modifyrole", c.modifyRole)
r.POST("/activerole", c.modifyRoleActive)
r.POST("/getallrole", c.roleList)
r.POST("/idgetrole", c.getRoleByID)
// role2url table
r.POST("/addurl", c.addURLPermission)
r.POST("/removeurl", c.removeURLPermission)
r.POST("/urlgetrole", c.urlPermissions)
r.POST("/geturl", c.permissions)
// admin2role table
r.POST("/addrelation", c.addRelation)
r.POST("/removerelation", c.removeRelation)
r.POST("/admingetrole", c.adminGetRoleMap)
r.POST("/getalladmin", c.getAdminIDMap)
r.POST("/getallroleid", c.getRoleIDMap)
}
func (c *Controller) createRole(ctx *gin.Context) {
var (
role struct {
Name string `json:"name" binding:"required,alphanum,min=5,max=64"`
Intro string `json:"intro" binding:"required,alphanum,min=2,max=256"`
}
)
err := ctx.ShouldBind(&role)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
err = mysql.CreateRole(c.db, &role.Name, &role.Intro)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK})
}
func (c *Controller) modifyRole(ctx *gin.Context) {
var (
role struct {
RoleID uint32 `json:"role_id" binding:"required"`
Name string `json:"name" binding:"required,alphanum,min=5,max=64"`
Intro string `json:"intro" binding:"required,alphanum,min=2,max=256"`
}
)
err := ctx.ShouldBind(&role)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
err = mysql.ModifyRole(c.db, role.RoleID, &role.Name, &role.Intro)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK})
}
func (c *Controller) modifyRoleActive(ctx *gin.Context) {
var (
role struct {
RoleID uint32 `json:"role_id" binding:"required"`
Active bool `json:"active"`
}
)
err := ctx.ShouldBind(&role)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
err = mysql.ModifyRoleActive(c.db, role.RoleID, role.Active)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK})
}
func (c *Controller) roleList(ctx *gin.Context) {
result, err := mysql.RoleList(c.db)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "RoleList": result})
}
func (c *Controller) getRoleByID(ctx *gin.Context) {
var (
role struct {
RoleID uint32 `json:"role_id" binding:"required"`
}
)
err := ctx.ShouldBind(&role)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
result, err := mysql.GetRoleByID(c.db, role.RoleID)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "RoleByID": result})
}
func (c *Controller) addURLPermission(ctx *gin.Context) {
var (
url struct {
URL string `json:"url" binding:"required"`
RoleID uint32 `json:"role_id" binding:"required"`
}
)
err := ctx.ShouldBind(&url)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
err = mysql.AddURLPermission(c.db, url.RoleID, url.URL)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK})
}
func (c *Controller) removeURLPermission(ctx *gin.Context) {
var (
url struct {
URL string `json:"url" binding:"required"`
RoleID uint32 `json:"role_id" binding:"required"`
}
)
err := ctx.ShouldBind(&url)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
err = mysql.RemoveURLPermission(c.db, url.RoleID, url.URL)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK})
}
func (c *Controller) urlPermissions(ctx *gin.Context) {
var (
url struct {
URL string `json:"url" binding:"required"`
}
)
err := ctx.ShouldBind(&url)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
result, err := mysql.URLPermissions(c.db, &url.URL)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "URLPermissions": result})
}
func (c *Controller) permissions(ctx *gin.Context) {
result, err := mysql.Permissions(c.db)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "Permissions": result})
}
func (c *Controller) addRelation(ctx *gin.Context) {
var (
relation struct {
AdminID uint32 `json:"admin_id" binding:"required"`
RoleID uint32 `json:"role_id" binding:"required"`
}
)
err := ctx.ShouldBind(&relation)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
err = mysql.AddRelation(c.db, relation.AdminID, relation.RoleID)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK})
}
func (c *Controller) removeRelation(ctx *gin.Context) {
var (
relation struct {
AdminID uint32 `json:"admin_id" binding:"required"`
RoleID uint32 `json:"role_id" binding:"required"`
}
)
err := ctx.ShouldBind(&relation)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
err = mysql.RemoveRelation(c.db, relation.AdminID, relation.RoleID)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK})
}
func (c *Controller) adminGetRoleMap(ctx *gin.Context) {
var (
relation struct {
AdminID uint32 `json:"admin_id" binding:"required"`
}
)
err := ctx.ShouldBind(&relation)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest})
return
}
result, err := mysql.AdminGetRoleMap(c.db, relation.AdminID)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "RoleMap": result})
}
func (c *Controller) getAdminIDMap(ctx *gin.Context) {
result, err := mysql.GetAdminIDMap(c.db)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "AdminIDMap": result})
}
func (c *Controller) getRoleIDMap(ctx *gin.Context) {
result, err := mysql.GetRoleIDMap(c.db)
if err != nil {
ctx.Error(err)
ctx.JSON(http.StatusBadGateway, gin.H{"status": http.StatusBadGateway})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "RoleIDMap": result})
}