-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource.go
111 lines (82 loc) · 3.1 KB
/
resource.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
package acl
import (
"sort"
"github.com/gin-gonic/gin"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix/pkg/dto/acl"
"github.com/coretrix/hitrix/pkg/entity"
"github.com/coretrix/hitrix/service"
)
func ListResources(c *gin.Context) *acl.ResourcesResponseDTO {
ormService := service.DI().OrmEngineForContext(c.Request.Context())
query := beeorm.NewRedisSearchQuery()
query.Sort("ID", false)
allPermissionEntities := make([]*entity.PermissionEntity, 0)
ormService.RedisSearch(&allPermissionEntities, query, beeorm.NewPager(1, 4000), "ResourceID")
resourceDTOsMapping := resourceDTOsMapping{}
for _, permissionEntity := range allPermissionEntities {
dto, ok := resourceDTOsMapping[permissionEntity.ResourceID.ID]
if !ok {
dto = &acl.ResourceResponseDTO{
ID: permissionEntity.ResourceID.ID,
Name: permissionEntity.ResourceID.Name,
Permissions: make([]*acl.PermissionResponseDTO, 0),
}
}
dto.Permissions = append(dto.Permissions, &acl.PermissionResponseDTO{
ID: permissionEntity.ID,
Name: permissionEntity.Name,
})
resourceDTOsMapping[permissionEntity.ResourceID.ID] = dto
}
resultDTOs := make([]*acl.ResourceResponseDTO, 0)
for _, resourceDTO := range resourceDTOsMapping {
resultDTOs = append(resultDTOs, resourceDTO)
}
sort.Slice(resultDTOs, func(i, j int) bool {
return resultDTOs[i].ID < resultDTOs[j].ID
})
return &acl.ResourcesResponseDTO{Resources: resultDTOs}
}
type UserRoleGetter interface {
GetRole() *entity.RoleEntity
}
func ListUserResources(c *gin.Context, getUserFunc func(c *gin.Context) beeorm.Entity) *acl.ResourcesResponseDTO {
ormService := service.DI().OrmEngineForContext(c.Request.Context())
userEntity := getUserFunc(c)
userWithGettableRole, ok := userEntity.(UserRoleGetter)
if !ok {
panic("user entity does not implement UserRoleSetter interface")
}
query := beeorm.NewRedisSearchQuery()
query.FilterUint("RoleID", userWithGettableRole.GetRole().ID)
privilegeEntities := make([]*entity.PrivilegeEntity, 0)
ormService.RedisSearch(&privilegeEntities, query, beeorm.NewPager(1, 4000), "ResourceID", "PermissionIDs")
resourceDTOsMapping := resourceDTOsMapping{}
for _, privilegeEntity := range privilegeEntities {
dto, ok := resourceDTOsMapping[privilegeEntity.ResourceID.ID]
if !ok {
dto = &acl.ResourceResponseDTO{
ID: privilegeEntity.ResourceID.ID,
Name: privilegeEntity.ResourceID.Name,
Permissions: make([]*acl.PermissionResponseDTO, 0),
}
}
for _, permissionEntity := range privilegeEntity.PermissionIDs {
dto.Permissions = append(dto.Permissions, &acl.PermissionResponseDTO{
ID: permissionEntity.ID,
Name: permissionEntity.Name,
})
}
resourceDTOsMapping[privilegeEntity.ResourceID.ID] = dto
}
resultDTOs := make([]*acl.ResourceResponseDTO, 0)
for _, resourceDTO := range resourceDTOsMapping {
resultDTOs = append(resultDTOs, resourceDTO)
}
sort.Slice(resultDTOs, func(i, j int) bool {
return resultDTOs[i].ID < resultDTOs[j].ID
})
return &acl.ResourcesResponseDTO{Resources: resultDTOs}
}
type resourceDTOsMapping map[uint64]*acl.ResourceResponseDTO