-
Notifications
You must be signed in to change notification settings - Fork 4
/
role.go
287 lines (238 loc) · 6.52 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
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
package apihandler
import (
"strconv"
"strings"
"github.com/uxff/flexdrive/pkg/app/admin/model/rbac"
"github.com/gin-gonic/gin"
"github.com/uxff/flexdrive/pkg/dao"
"github.com/uxff/flexdrive/pkg/dao/base"
"github.com/uxff/flexdrive/pkg/log"
)
type RoleListRequest struct {
CreateStart string `form:"createStart"`
CreateEnd string `form:"createEnd"`
Name string `form:"name"`
Page int `form:"page"`
PageSize int `form:"pagesize"`
}
func (r *RoleListRequest) ToCondition() (condition map[string]interface{}) {
condition = make(map[string]interface{})
if r.CreateStart != "" {
condition["created>=?"] = r.CreateStart
}
if r.CreateEnd != "" {
condition["created<=?"] = r.CreateEnd
}
if r.Name != "" {
condition["name like ?"] = "%" + r.Name + "%"
}
log.Debugf("r=%+v tocondition:%+v", r, condition)
return condition
}
// 接口返回的元素
type RoleItem struct {
Id int `json:"id"`
Name string `json:"name" form:"name"`
Created string `json:"created" form:"-"`
Updated string `json:"updated" form:"-"`
Status int `json:"status" form:"-"`
AccessRoute map[string]string `json:"accessRoute" form:"accessRoute"`
}
func RoleList(c *gin.Context) {
requestId := c.GetString(CtxKeyRequestId)
// 请求参数校验
req := &RoleListRequest{}
err := c.ShouldBindQuery(req)
if err != nil {
JsonErr(c, ErrInvalidParam)
return
}
// 列表查询
list := make([]*dao.Role, 0)
total, err := base.ListAndCountByCondition(&dao.Role{}, req.ToCondition(), req.Page, req.PageSize, "", &list)
if err != nil {
log.Trace(requestId).Errorf("list failed:%v", err)
JsonErr(c, ErrInternal)
return
}
// 从数据库结构转换成返回结构
//resItems := make([]*Role, 0)
// for _, roleItem := range list {
//resItems = append(resItems, NewManagerItemFromEnt(v))
// parse permit
// }
JsonOk(c, gin.H{
"total": total,
"page": req.Page,
"pagesize": req.PageSize,
"list": list,
"reqParam": req,
// "paginator": paginator.NewPaginator(c.Request, 10, int64(total)),
})
}
type RoleAddRequest struct {
RoleItem // 新增只用到里面的 Email,roleid,pwd
// Status int
}
func (r *RoleAddRequest) ToEnt() *dao.Role {
e := &dao.Role{
Name: r.Name,
//RoleId: r.RoleId,
// MgrLastLoginAt:time.Now(),
//Pwd: r.Pwd,
}
e.Permit = rbac.GetAllMenu()
for _, accessGroup := range e.Permit {
// 一级分组不用配置权限
for _, accessItem := range accessGroup.Sub {
accessItem.Access = false
for routeStr, isAccess := range r.AccessRoute {
if routeStr == accessItem.PermitRoute && isAccess == "1" {
accessItem.Access = true
}
}
}
}
//e.SetPwd(r.Pwd)
return e
}
// func RoleAdd(c *gin.Context) {
// allAccessItems := rbac.GetAllMenu()
// c.HTML(http.StatusOK, "role/add.tpl", gin.H{
// "LoginInfo": getLoginInfo(c),
// "IsLogin": isLoginIn(c),
// "allAccessItems": allAccessItems,
// })
// }
// func RoleEdit(c *gin.Context) {
// roleId, _ := strconv.Atoi(c.Param("id"))
// if roleId <= 0 {
// JsonErr(c, ErrInvalidParam)
// return
// }
// roleEnt, _ := dao.GetRoleById(roleId)
// if roleEnt == nil {
// JsonErr(c, ErrMgrNotExist)
// return
// }
// if roleEnt.Status == base.StatusDeleted {
// JsonErr(c, ErrMgrDisabled)
// return
// }
// log.Debugf("roleEnt:%+v", roleEnt)
// allAccessItems := rbac.GetAllAccessItems(roleEnt.Permit)
// c.HTML(http.StatusOK, "role/edit.tpl", gin.H{
// "LoginInfo": getLoginInfo(c),
// "IsLogin": isLoginIn(c),
// "roleId": roleId,
// "RoleEnt": roleEnt,
// "allAccessItems": allAccessItems,
// })
// }
// 新增和修改
func RoleAddForm(c *gin.Context) {
requestId := c.GetString(CtxKeyRequestId)
roleId, _ := strconv.ParseInt(c.Param("id"), 10, 64)
req := &RoleAddRequest{}
err := c.ShouldBind(req)
req.AccessRoute = c.PostFormMap("accessRoute")
if err != nil {
JsonErr(c, ErrInvalidParam)
return
}
if req.Name == "" { // 角色名不能为空
JsonErr(c, ErrInvalidParam)
return
}
// 去掉用户输入的字符串里开头结尾的不可见字符
req.Name = strings.TrimSpace(req.Name)
//req.Permit = strings.TrimSpace(req.Permit)
log.Trace(requestId).Debugf("%+v", req)
// 检验名称是否已经存在
existEnt, err := dao.GetRoleByName(req.Name)
if err != nil {
log.Errorf("db error:%v", err)
JsonErr(c, ErrInternal)
return
}
// 如果是添加 不能添加同名
if existEnt != nil && roleId == 0 {
JsonErr(c, ErrNameDuplicate)
return
}
// 如果是修改 不能修改和已存在的冲突
if existEnt != nil && roleId > 0 && int(roleId) != existEnt.Id {
JsonErr(c, ErrNameDuplicate)
return
}
// roleId := req.roleId // 如果有则是编辑
// roleEnt, err := dao.GetRoleById(req.RoleId)
// if err != nil {
// log.Trace(requestId).Errorf("查询角色信息失败:%v roleId:%d", err, req.RoleId)
// return
// }
ent := req.ToEnt()
ent.Status = base.StatusNormal
if roleId > 0 {
cols := []string{"name", "permit"}
_, err = base.UpdateByCol("id", roleId, ent, cols)
//base.CacheDelByEntity("mgrLoginName", req.Email, existEnt)
} else {
_, err = base.Insert(ent)
}
if err != nil {
log.Trace(requestId).Errorf("db error:%v", err)
JsonErr(c, ErrInternal)
return
}
JsonOk(c, gin.H{
"id": ent.Id,
})
// c.Redirect(http.StatusMovedPermanently, RouteRoleList)
}
func RoleEnable(c *gin.Context) {
roleId, _ := strconv.ParseInt(c.Param("id"), 10, 64)
if roleId <= 0 {
JsonErr(c, ErrInvalidParam)
return
}
enable, _ := strconv.ParseInt(c.Param("enable"), 10, 64)
loginInfo := getLoginInfo(c)
ent, err := dao.GetRoleById(int(roleId))
//_, err := base.GetByCol("id", roleId, roleEnt)
// exist, err := base.GetByCol("roleId", roleId, roleEnt)
if err != nil {
log.Errorf("db error:%v", err)
JsonErr(c, ErrInternal)
return
}
if ent == nil {
JsonErr(c, ErrRoleNotExist)
return
}
if enable == 1 {
// 启用
ent.Status = base.StatusNormal
} else if enable == 9 {
// 停用
if int(roleId) == loginInfo.RoleId {
JsonErrMsg(c, ErrInternal, "不能停用自己的账号")
return
}
// if ent.IsSuperRole() {
// StdErrMsgResponse(c, ErrInternal, "不能停用超级管理的账号", "")
// return
// }
ent.Status = base.StatusDeleted
}
_, err = base.UpdateByCol("id", roleId, ent, []string{"status"})
if err != nil {
log.Errorf("db error:%v", err)
JsonErr(c, ErrInternal)
return
}
JsonOk(c, gin.H{
"id": ent.Id,
})
// c.Redirect(http.StatusMovedPermanently, RouteRoleList)
}