forked from harranali/authority
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthority.go
453 lines (373 loc) · 11.6 KB
/
authority.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package authority
import (
"errors"
"gorm.io/gorm"
)
// Authority helps deal with permissions
type Authority struct {
DB *gorm.DB
}
// Options has the options for initiating the package
type Options struct {
TablesPrefix string
DB *gorm.DB
}
var (
ErrPermissionInUse = errors.New("cannot delete assigned permission")
ErrPermissionNotFound = errors.New("permission not found")
ErrRoleAlreadyAssigned = errors.New("this role is already assigned to the user")
ErrRoleInUse = errors.New("cannot delete assigned role")
ErrRoleNotFound = errors.New("role not found")
)
var tablePrefix string
var auth *Authority
// New initiates authority
func New(opts Options) *Authority {
tablePrefix = opts.TablesPrefix
auth = &Authority{
DB: opts.DB,
}
migrateTables(opts.DB)
return auth
}
// Resolve returns the initiated instance
func Resolve() *Authority {
return auth
}
// CreateRole stores a role in the database
// it accepts the role name. it returns an error
// in case of any
func (a *Authority) CreateRole(roleName string) error {
var dbRole Role
res := a.DB.Where("name = ?", roleName).First(&dbRole)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
// create
a.DB.Create(&Role{Name: roleName})
return nil
}
}
return res.Error
}
// CreatePermission stores a permission in the database
// it accepts the permission name. it returns an error
// in case of any
func (a *Authority) CreatePermission(permName string) error {
var dbPerm Permission
res := a.DB.Where("name = ?", permName).First(&dbPerm)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
// create
a.DB.Create(&Permission{Name: permName})
return nil
}
}
return res.Error
}
// AssignPermissions assigns a group of permissions to a given role
// it accepts in the first parameter the role name, it returns an error if there is not matching record
// of the role name in the database.
// the second parameter is a slice of strings which represents a group of permissions to be assigned to the role
// if any of these permissions doesn't have a matching record in the database the operations stops, changes reverted
// and error is returned
// in case of success nothing is returned
func (a *Authority) AssignPermissions(roleName string, permNames []string) error {
// get the role id
var role Role
rRes := a.DB.Where("name = ?", roleName).First(&role)
if rRes.Error != nil {
if errors.Is(rRes.Error, gorm.ErrRecordNotFound) {
return ErrRoleNotFound
}
}
var perms []Permission
// get the permissions ids
for _, permName := range permNames {
var perm Permission
pRes := a.DB.Where("name = ?", permName).First(&perm)
if pRes.Error != nil {
if errors.Is(pRes.Error, gorm.ErrRecordNotFound) {
return ErrPermissionNotFound
}
}
perms = append(perms, perm)
}
// insert data into RolePermissions table
for _, perm := range perms {
// ignore any assigned permission
var rolePerm RolePermission
res := a.DB.Where("role_id = ?", role.ID).Where("permission_id =?", perm.ID).First(&rolePerm)
if res.Error != nil {
// assign the record
cRes := a.DB.Create(&RolePermission{RoleID: role.ID, PermissionID: perm.ID})
if cRes.Error != nil {
return cRes.Error
}
}
}
return nil
}
// AssignRole assigns a given role to a user
// the first parameter is the user id, the second parameter is the role name
// if the role name doesn't have a matching record in the data base an error is returned
// if the user have already a role assigned to him an error is returned
func (a *Authority) AssignRole(userID uint, roleName string) error {
// make sure the role exist
var role Role
res := a.DB.Where("name = ?", roleName).First(&role)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return ErrRoleNotFound
}
}
// check if the role is already assigned
var userRole UserRole
res = a.DB.Where("user_id = ?", userID).Where("role_id = ?", role.ID).First(&userRole)
if res.Error == nil {
//found a record, this role is already assigned to the same user
return ErrRoleAlreadyAssigned
}
// assign the role
a.DB.Create(&UserRole{UserID: userID, RoleID: role.ID})
return nil
}
// CheckRole checks if a role is assigned to a user
// it accepts the user id as the first parameter
// the role as the second parameter
// it returns an error if the role is not present in database
func (a *Authority) CheckRole(userID uint, roleName string) (bool, error) {
// find the role
var role Role
res := a.DB.Where("name = ?", roleName).First(&role)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return false, ErrRoleNotFound
}
}
// check if the role is a assigned
var userRole UserRole
res = a.DB.Where("user_id = ?", userID).Where("role_id = ?", role.ID).First(&userRole)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return false, nil
}
}
return true, nil
}
// CheckPermission checks if a permission is assigned to the role that's assigned to the user.
// it accepts the user id as the first parameter
// the permission as the second parameter
// it returns an error if the permission is not present in the database
func (a *Authority) CheckPermission(userID uint, permName string) (bool, error) {
// the user role
var userRoles []UserRole
res := a.DB.Where("user_id = ?", userID).Find(&userRoles)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return false, nil
}
}
//prepare an array of role ids
var roleIDs []uint
for _, r := range userRoles {
roleIDs = append(roleIDs, r.RoleID)
}
// find the permission
var perm Permission
res = a.DB.Where("name = ?", permName).First(&perm)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return false, ErrPermissionNotFound
}
}
// find the role permission
var rolePermission RolePermission
res = a.DB.Where("role_id IN (?)", roleIDs).Where("permission_id = ?", perm.ID).First(&rolePermission)
if res.Error != nil {
return false, nil
}
return true, nil
}
// CheckRolePermission checks if a role has the permission assigned
// it accepts the role as the first parameter
// it accepts the permission as the second parameter
// it returns an error if the role is not present in database
// it returns an error if the permission is not present in database
func (a *Authority) CheckRolePermission(roleName string, permName string) (bool, error) {
// find the role
var role Role
res := a.DB.Where("name = ?", roleName).First(&role)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return false, ErrRoleNotFound
}
}
// find the permission
var perm Permission
res = a.DB.Where("name = ?", permName).First(&perm)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return false, ErrPermissionNotFound
}
}
// find the rolePermission
var rolePermission RolePermission
res = a.DB.Where("role_id = ?", role.ID).Where("permission_id = ?", perm.ID).First(&rolePermission)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return false, nil
}
}
return true, nil
}
// RevokeRole revokes a user's role
// it returns a error in case of any
func (a *Authority) RevokeRole(userID uint, roleName string) error {
// find the role
var role Role
res := a.DB.Where("name = ?", roleName).First(&role)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return ErrRoleNotFound
}
}
// revoke the role
a.DB.Where("user_id = ?", userID).Where("role_id = ?", role.ID).Delete(UserRole{})
return nil
}
// RevokePermission revokes a permission from the user's assigned role
// it returns an error in case of any
func (a *Authority) RevokePermission(userID uint, permName string) error {
// revoke the permission from all roles of the user
// find the user roles
var userRoles []UserRole
res := a.DB.Where("user_id = ?", userID).Find(&userRoles)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return nil
}
}
// find the permission
var perm Permission
res = a.DB.Where("name = ?", permName).First(&perm)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return ErrPermissionNotFound
}
}
for _, r := range userRoles {
// revoke the permission
a.DB.Where("role_id = ?", r.RoleID).Where("permission_id = ?", perm.ID).Delete(RolePermission{})
}
return nil
}
// RevokeRolePermission revokes a permission from a given role
// it returns an error in case of any
func (a *Authority) RevokeRolePermission(roleName string, permName string) error {
// find the role
var role Role
res := a.DB.Where("name = ?", roleName).First(&role)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return ErrRoleNotFound
}
}
// find the permission
var perm Permission
res = a.DB.Where("name = ?", permName).First(&perm)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return ErrPermissionNotFound
}
}
// revoke the permission
a.DB.Where("role_id = ?", role.ID).Where("permission_id = ?", perm.ID).Delete(RolePermission{})
return nil
}
// GetRoles returns all stored roles
func (a *Authority) GetRoles() ([]string, error) {
var result []string
var roles []Role
a.DB.Find(&roles)
for _, role := range roles {
result = append(result, role.Name)
}
return result, nil
}
// GetUserRoles returns all user assigned roles
func (a *Authority) GetUserRoles(userID uint) ([]string, error) {
var result []string
var userRoles []UserRole
a.DB.Where("user_id = ?", userID).Find(&userRoles)
for _, r := range userRoles {
var role Role
// for every user role get the role name
res := a.DB.Where("id = ?", r.RoleID).Find(&role)
if res.Error == nil {
result = append(result, role.Name)
}
}
return result, nil
}
// GetPermissions returns all stored permissions
func (a *Authority) GetPermissions() ([]string, error) {
var result []string
var perms []Permission
a.DB.Find(&perms)
for _, perm := range perms {
result = append(result, perm.Name)
}
return result, nil
}
// DeleteRole deletes a given role
// if the role is assigned to a user it returns an error
func (a *Authority) DeleteRole(roleName string) error {
// find the role
var role Role
res := a.DB.Where("name = ?", roleName).First(&role)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return ErrRoleNotFound
}
}
// check if the role is assigned to a user
var userRole UserRole
res = a.DB.Where("role_id = ?", role.ID).First(&userRole)
if res.Error == nil {
// role is assigned
return ErrRoleInUse
}
// revoke the assignment of permissions before deleting the role
a.DB.Where("role_id = ?", role.ID).Delete(RolePermission{})
// delete the role
a.DB.Where("name = ?", roleName).Delete(Role{})
return nil
}
// DeletePermission deletes a given permission
// if the permission is assigned to a role it returns an error
func (a *Authority) DeletePermission(permName string) error {
// find the permission
var perm Permission
res := a.DB.Where("name = ?", permName).First(&perm)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
return ErrPermissionNotFound
}
}
// check if the permission is assigned to a role
var rolePermission RolePermission
res = a.DB.Where("permission_id = ?", perm.ID).First(&rolePermission)
if res.Error == nil {
// role is assigned
return ErrPermissionInUse
}
// delete the permission
a.DB.Where("name = ?", permName).Delete(Permission{})
return nil
}
func migrateTables(db *gorm.DB) {
db.AutoMigrate(&Role{})
db.AutoMigrate(&Permission{})
db.AutoMigrate(&RolePermission{})
db.AutoMigrate(&UserRole{})
}