-
Notifications
You must be signed in to change notification settings - Fork 0
/
auths.go
377 lines (327 loc) · 8.24 KB
/
auths.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
package rabbit
import (
"errors"
"gorm.io/gorm"
)
// group
func GetGroupByID(db *gorm.DB, gid uint) (*Group, error) {
return GetByID[Group](db, gid)
}
func GetGroupByName(db *gorm.DB, name string) (*Group, error) {
return Get(db, &Group{Name: name})
}
func GetGroupsByUser(db *gorm.DB, uid uint) ([]*Group, error) {
var user User
result := db.Where("id", uid).Preload("Groups").Take(&user)
if result.Error != nil {
return nil, result.Error
}
return user.Groups, nil
}
func GetFirstGroupByUser(db *gorm.DB, uid uint) (*Group, error) {
var member GroupMember
result := db.Where("user_id", uid).Preload("Group").Take(&member)
if result.Error != nil {
return nil, result.Error
}
return &member.Group, nil
}
func CreateGroupByUser(db *gorm.DB, uid uint, name string) (*Group, error) {
group := Group{
Name: name,
}
result := db.Create(&group)
if result.Error != nil {
return nil, result.Error
}
member := GroupMember{
UserID: uid,
GroupID: group.ID,
}
result = db.Create(&member)
if result.Error != nil {
return nil, result.Error
}
return &group, nil
}
func CheckGroupInUse(db *gorm.DB, gid uint) (bool, error) {
var count int64
if err := db.Model(&GroupMember{}).
Where("group_id", gid).
Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}
// role
func GetRoleByID(db *gorm.DB, rid uint) (*Role, error) {
return GetByID[Role](db, rid)
}
func GetRoleByName(db *gorm.DB, name string) (*Role, error) {
return Get(db, &Role{Name: name})
}
func GetRolesByUser(db *gorm.DB, uid uint) ([]*Role, error) {
var user User
result := db.Model(&User{}).Preload("Roles").Take(&user, uid)
if result.Error != nil {
return nil, result.Error
}
return user.Roles, nil
}
func CheckRoleInUse(db *gorm.DB, rid uint) (bool, error) {
var count int64
result := db.Model(&UserRole{}).Where("role_id", rid).Count(&count)
if result.Error != nil {
return false, result.Error
}
return count > 0, nil
}
func CheckRoleNameExist(db *gorm.DB, name string) (bool, error) {
var count int64
result := db.Model(&Role{}).Where("name", name).Count(&count)
if result.Error != nil {
return false, result.Error
}
return count > 0, nil
}
func CreateRole(db *gorm.DB, name, label string) (*Role, error) {
return AddRoleWithPermissions(db, name, label, nil)
}
func AddRoleWithPermissions(db *gorm.DB, name, label string, ps []uint) (*Role, error) {
role := Role{
Name: name,
Label: label,
}
result := db.Create(&role)
if result.Error != nil {
return nil, result.Error
}
// add new permissions related to this role
for _, pid := range ps {
rolePermission := RolePermission{
RoleID: role.ID,
PermissionID: pid,
}
result := db.Create(&rolePermission)
if result.Error != nil {
return nil, result.Error
}
}
return &role, nil
}
func UpdateRoleWithPermissions(db *gorm.DB, rid uint, name, label string, ps []uint) (*Role, error) {
role := Role{
ID: rid,
Name: name,
Label: label,
}
// update role, need to clear old permissions related to this role
result := db.Model(&role).Select("name", "label").Updates(role)
if result.Error != nil {
return nil, result.Error
}
result = db.Delete(&RolePermission{}, "role_id", role.ID)
if result.Error != nil {
return nil, result.Error
}
// add new permissions related to this role
for _, pid := range ps {
rolePermission := RolePermission{
RoleID: role.ID,
PermissionID: pid,
}
result := db.Create(&rolePermission)
if result.Error != nil {
return nil, result.Error
}
}
return &role, nil
}
func DeleteRole(db *gorm.DB, rid uint) error {
result := db.Delete(&RolePermission{}, "role_id", rid)
if result.Error != nil {
return result.Error
}
result = db.Delete(&Role{}, "id", rid)
if result.Error != nil {
return result.Error
}
return nil
}
// permission
func SavePermission(db *gorm.DB, id, pid uint, name, uri, method string, anonymous bool) (*Permission, error) {
permission := Permission{
ID: id,
Name: name,
ParentID: pid,
Uri: uri,
Method: method,
Anonymous: anonymous,
}
result := db.Save(&permission)
if result.Error != nil {
return nil, result.Error
}
return &permission, nil
}
func GetPermissionByID(db *gorm.DB, pid uint) (*Permission, error) {
return GetByID[Permission](db, pid)
}
func GetPermissionByName(db *gorm.DB, name string) (*Permission, error) {
return Get(db, &Permission{Name: name})
}
func GetPermission(db *gorm.DB, uri, method string) (*Permission, error) {
return Get(db, &Permission{Uri: uri, Method: method})
}
func GetPermissionsByRole(db *gorm.DB, rid uint) ([]*Permission, error) {
var role Role
result := db.Model(&Role{}).Preload("Permissions").Take(&role, rid)
if result.Error != nil {
return nil, result.Error
}
return role.Permissions, nil
}
func GetPermissionChildren(db *gorm.DB, pid uint) ([]*Permission, error) {
var permissions []*Permission
result := db.Model(&Permission{}).Where("parent_id", pid).Find(&permissions)
if result.Error != nil {
return nil, result.Error
}
return permissions, nil
}
func CheckPermissionInUse(db *gorm.DB, pid uint) (bool, error) {
var count int64
result := db.Model(&RolePermission{}).Where("permission_id", pid).Count(&count)
if result.Error != nil {
return false, result.Error
}
return count > 0, nil
}
func CheckPermissionNameExist(db *gorm.DB, name string) (bool, error) {
var count int64
result := db.Model(&Permission{}).Where("name", name).Count(&count)
if result.Error != nil {
return false, result.Error
}
return count > 0, nil
}
// if the permission is a parent permission, delete all its children
func DeletePermission(db *gorm.DB, pid uint) error {
p, err := GetPermissionByID(db, pid)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
if p.ParentID == 0 {
children, err := GetPermissionChildren(db, pid)
if err != nil {
return err
}
ids := []uint{pid}
for _, child := range children {
ids = append(ids, child.ID)
}
result := db.Delete(&Permission{}, "id", ids)
if result.Error != nil {
return result.Error
}
} else {
result := db.Delete(&Permission{}, "id", pid)
if result.Error != nil {
return result.Error
}
}
return nil
}
// user
func GetUsersByGroup(db *gorm.DB, gid uint) ([]*User, error) {
var group Group
result := db.Model(&Group{}).Preload("Users").Take(&group)
if result.Error != nil {
return nil, result.Error
}
return group.Users, nil
}
func GetUsersByRole(db *gorm.DB, rid uint) ([]*User, error) {
var role Role
result := db.Model(&Role{}).Preload("Users").Take(&role)
if result.Error != nil {
return nil, result.Error
}
return role.Users, nil
}
func AddRoleForUser(db *gorm.DB, uid uint, rid uint) error {
userRole := UserRole{
UserID: uid,
RoleID: rid,
}
return db.Model(&userRole).Create(userRole).Error
}
func UpdateRolesForUser(db *gorm.DB, uid uint, rids []uint) (*User, error) {
user := User{
ID: uid,
}
result := db.Delete(&UserRole{}, "user_id", user.ID)
if result.Error != nil {
return nil, result.Error
}
for _, roleID := range rids {
if err := AddRoleForUser(db, user.ID, roleID); err != nil {
return nil, err
}
}
return &user, nil
}
// check
func CheckRolePermission(db *gorm.DB, rid uint, uri, method string) (bool, error) {
ps, err := GetPermissionsByRole(db, rid)
if err != nil {
return false, err
}
for _, p := range ps {
if p.Anonymous || (p.Uri == uri && p.Method == method) {
return true, nil
}
}
return false, nil
}
func CheckUserPermission(db *gorm.DB, uid uint, uri, method string) (bool, error) {
p, err := GetPermission(db, uri, method)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, nil
}
return false, err
}
if p.Anonymous {
return true, nil
}
rs, err := GetRolesByUser(db, uid)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return false, nil
}
return false, err
}
for _, r := range rs {
if pass, _ := CheckRolePermission(db, r.ID, uri, method); pass {
return true, nil
}
}
return false, nil
}
// for test
func CreateRoleWithPermissions(db *gorm.DB, name, label string, ps []*Permission) (*Role, error) {
role := Role{
Name: name,
Label: label,
Permissions: ps,
}
result := db.Create(&role)
if result.Error != nil {
return nil, result.Error
}
return &role, nil
}