-
Notifications
You must be signed in to change notification settings - Fork 14
/
acl.go
43 lines (32 loc) · 1.37 KB
/
acl.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
package acl
import (
redisearch "github.com/coretrix/beeorm-redisearch-plugin"
"github.com/latolukasz/beeorm/v2"
"github.com/coretrix/hitrix/datalayer"
"github.com/coretrix/hitrix/pkg/entity"
)
func ACL(ormService *datalayer.ORM, roleEntity *entity.RoleEntity, resource string, permissions ...string) bool {
resourceQuery := redisearch.NewRedisSearchQuery()
resourceQuery.FilterString("Name", resource)
resourceEntity := &entity.ResourceEntity{}
if !ormService.RedisSearchOne(resourceEntity, resourceQuery) {
return false
}
permissionQuery := redisearch.NewRedisSearchQuery()
permissionQuery.FilterUint("ResourceID", resourceEntity.ID)
permissionQuery.FilterString("Name", permissions...)
permissionEntities := make([]*entity.PermissionEntity, 0)
ormService.RedisSearch(permissionQuery, beeorm.NewPager(1, 1000), &permissionEntities)
if len(permissions) != len(permissionEntities) {
return false
}
permissionIDs := make([]uint64, len(permissionEntities))
for i, permissionEntity := range permissionEntities {
permissionIDs[i] = permissionEntity.ID
}
privilegeQuery := redisearch.NewRedisSearchQuery()
privilegeQuery.FilterUint("RoleID", roleEntity.ID)
privilegeQuery.FilterUint("ResourceID", resourceEntity.ID)
privilegeQuery.FilterManyReferenceIn("PermissionIDs", permissionIDs...)
return ormService.RedisSearchOne(&entity.PrivilegeEntity{}, privilegeQuery)
}