-
Notifications
You must be signed in to change notification settings - Fork 13
/
role.go
103 lines (81 loc) · 2.53 KB
/
role.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
package acl
import (
"fmt"
"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"
"github.com/coretrix/hitrix/service/component/crud"
)
func RolesColumns() []*crud.Column {
return []*crud.Column{
{
Key: "ID",
Label: "ID",
Searchable: false,
Sortable: false,
Visible: true,
},
{
Key: "Name",
Label: "Name",
Searchable: false,
Sortable: false,
Visible: true,
},
}
}
func ListRoles(c *gin.Context, request *crud.ListRequest) *acl.RolesResponseDTO {
cols := RolesColumns()
crudService := service.DI().Crud()
searchParams := crudService.ExtractListParams(cols, request)
query := crudService.GenerateListRedisSearchQuery(searchParams)
if len(searchParams.Sort) == 0 {
query.Sort("ID", false)
}
ormService := service.DI().OrmEngineForContext(c.Request.Context())
allRoleEntities := make([]*entity.RoleEntity, 0)
total := ormService.RedisSearch(&allRoleEntities, query, beeorm.NewPager(searchParams.Page, searchParams.PageSize))
result := &acl.RolesResponseDTO{
Total: int(total),
Columns: cols,
PageContext: ListResources(c),
}
for _, roleEntity := range allRoleEntities {
result.Rows = append(result.Rows, &acl.RoleResponseDTO{
ID: roleEntity.ID,
Name: roleEntity.Name,
})
}
return result
}
func GetRole(c *gin.Context, request *acl.RoleRequestDTO) (*acl.RoleResponseDTO, error) {
ormService := service.DI().OrmEngineForContext(c.Request.Context())
query := beeorm.NewRedisSearchQuery()
query.FilterUint("RoleID", request.ID)
allPrivilegeEntities := make([]*entity.PrivilegeEntity, 0)
ormService.RedisSearch(&allPrivilegeEntities, query, beeorm.NewPager(1, 4000), "RoleID", "ResourceID", "PermissionIDs")
if len(allPrivilegeEntities) == 0 {
return nil, fmt.Errorf("role with ID: %d not found", request.ID)
}
result := &acl.RoleResponseDTO{}
for i, privilegeEntity := range allPrivilegeEntities {
if i == 0 {
result.ID = privilegeEntity.RoleID.ID
result.Name = privilegeEntity.RoleID.Name
}
resource := &acl.ResourceResponseDTO{
ID: privilegeEntity.ResourceID.ID,
Name: privilegeEntity.ResourceID.Name,
}
for _, permission := range privilegeEntity.PermissionIDs {
resource.Permissions = append(resource.Permissions, &acl.PermissionResponseDTO{
ID: permission.ID,
Name: permission.Name,
})
}
result.Resources = append(result.Resources, resource)
}
return result, nil
}