/
user_bll_role.go
174 lines (158 loc) · 4.2 KB
/
user_bll_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
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
package qf
import (
"fmt"
"github.com/UritMedical/qf/util"
"strings"
)
func (b *userBll) regRoleApi(api ApiMap) {
//角色
api.Reg(EApiKindSave, "role", b.saveRole) //创建、修改角色
api.Reg(EApiKindDelete, "role", b.deleteRole) //删除角色
api.Reg(EApiKindGetList, "roles", b.getAllRoles) //获取所有角色
//用户-角色
api.Reg(EApiKindSave, "role/users", b.setUserRoleRelation) //给角色删除或者添加用户
api.Reg(EApiKindGetList, "role/users", b.getRoleUsers) //获取指定角色下的用户
//角色-API
api.Reg(EApiKindSave, "role/apis", b.setRoleApi) //设置角色所拥有的api
api.Reg(EApiKindGetList, "role/apis", b.getRoleApi) //获取角色拥有的API
}
//
// saveRole
// @Description: 增改角色
// @param ctx
// @return interface{}
// @return error
//
func (b *userBll) saveRole(ctx *Context) (interface{}, IError) {
role := &Role{}
if err := ctx.Bind(role); err != nil {
return nil, err
}
if role.Id == 0 {
role.Id = ctx.NewId(role)
}
return nil, b.roleDal.Save(role)
}
//
// deleteRole
// @Description: 删除角色
// @param ctx
// @return interface{}
// @return error
//
func (b *userBll) deleteRole(ctx *Context) (interface{}, IError) {
uId := ctx.GetId()
return nil, b.roleDal.Delete(uId)
}
//
// getAllRoles
// @Description: 获取所有的角色
// @param ctx
// @return interface{}
// @return error
//
func (b *userBll) getAllRoles(_ *Context) (interface{}, IError) {
roles := make([]Role, 0)
err := b.roleDal.GetList(0, 100, &roles)
result := make([]map[string]interface{}, 0)
for _, role := range roles {
//获取此角色拥有的用户
userIds, _ := b.userRoleDal.GetUsersByRoleId(role.Id)
users, _ := b.userDal.GetUsersByIds(userIds)
roleDict := make(map[string]interface{})
roleDict["RoleInfo"] = util.ToMap(role)
roleDict["Users"] = util.ToMaps(users)
result = append(result, roleDict)
}
return result, err
}
//
// setUserRoleRelation
// @Description: 设置角色-用户关系
// @param roleId 角色ID
// @param userId 用户Id
// @return error
//
func (b *userBll) setUserRoleRelation(ctx *Context) (interface{}, IError) {
var params = struct {
RoleId uint64
UserIds []uint64
}{}
if err := ctx.Bind(¶ms); err != nil {
return nil, err
}
return nil, b.userRoleDal.SetRoleUsers(params.RoleId, params.UserIds)
}
//
// getRoleUsers
// @Description: 获取此角色下的用户
// @param ctx
// @return interface{}
// @return error
//
func (b *userBll) getRoleUsers(ctx *Context) (interface{}, IError) {
roleId := ctx.GetId()
userIds, _ := b.userRoleDal.GetUsersByRoleId(roleId)
users, err := b.userDal.GetUsersByIds(userIds)
return util.ToMaps(users), err
}
//
// getRolesByUserId
// @Description: 获取用户所拥有的角色
// @receiver b
//
func (b *userBll) getRolesByUserId(userId uint64) ([]Role, IError) {
roleIds, _ := b.userRoleDal.GetRolesByUserId(userId)
return b.roleDal.GetRolesByIds(roleIds)
}
//
// getUserAllApis
// @Description: 获取全部用户可以访问的Api列表
// @param roleIds
//
func (b *userBll) getUserAllApis(roles []RoleInfo) map[string]byte {
//apis := map[string]byte{}
rids := make([]uint64, 0)
for _, r := range roles {
rids = append(rids, r.Id)
}
apis, err := b.roleApiDal.GetApisByRoleId(rids)
if err != nil {
return map[string]byte{}
}
finals := map[string]byte{}
for _, api := range apis {
url := strings.TrimLeft(api.Url, "/")
for _, p := range api.Permission {
switch string(p) {
case "r":
finals[fmt.Sprintf("GET:/%s", url)] = 1
case "w":
finals[fmt.Sprintf("POST:/%s", url)] = 1
finals[fmt.Sprintf("PATCH:/%s", url)] = 1
finals[fmt.Sprintf("DELETE:/%s", url)] = 1
}
}
}
return finals
}
//设置角色所拥有的api
func (b *userBll) setRoleApi(ctx *Context) (interface{}, IError) {
params := struct {
RoleId uint64
Apis []string
}{}
if err := ctx.Bind(¶ms); err != nil {
return nil, err
}
return nil, b.roleApiDal.SetRoleApis(params.RoleId, params.Apis)
}
//获取角色所拥有的api
func (b *userBll) getRoleApi(ctx *Context) (interface{}, IError) {
roleId := ctx.GetId()
ret, err := b.roleApiDal.GetApisByRoleId([]uint64{roleId})
if err != nil {
return nil, err
}
return util.ToMaps(ret), nil
}