-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
395 lines (356 loc) · 11.4 KB
/
user.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package controllers
import (
"fmt"
"github.com/astaxie/beego/validation"
"github.com/dombine/mm-wiki/app/models"
"github.com/dombine/mm-wiki/app/utils"
"strings"
)
type UserController struct {
BaseController
}
func (this *UserController) Add() {
roles := []map[string]string{}
var err error
if this.IsRoot() {
roles, err = models.RoleModel.GetRoles()
} else {
roles, err = models.RoleModel.GetRolesNotContainRoot()
}
if err != nil {
this.ErrorLog("获取用户角色失败:" + err.Error())
this.ViewError("获取用户角色失败!")
}
this.Data["roles"] = roles
this.viewLayout("user/form", "user")
}
func (this *UserController) Save() {
if !this.IsPost() {
this.ViewError("请求方式有误!", "/system/user/add")
}
username := strings.TrimSpace(this.GetString("username", ""))
givenName := strings.TrimSpace(this.GetString("given_name", ""))
password := strings.TrimSpace(this.GetString("password", ""))
email := strings.TrimSpace(this.GetString("email", ""))
mobile := strings.TrimSpace(this.GetString("mobile", ""))
roleId := strings.TrimSpace(this.GetString("role_id", ""))
phone := strings.TrimSpace(this.GetString("phone", ""))
department := strings.TrimSpace(this.GetString("department", ""))
position := strings.TrimSpace(this.GetString("position", ""))
location := strings.TrimSpace(this.GetString("location", ""))
im := strings.TrimSpace(this.GetString("im", ""))
this.Ctx.Request.PostForm.Del("password")
v := validation.Validation{}
if username == "" {
this.jsonError("用户名不能为空!")
}
if !v.AlphaNumeric(username, "username").Ok {
this.jsonError("用户名格式不正确!")
}
if givenName == "" {
this.jsonError("姓名不能为空!")
}
if password == "" {
this.jsonError("密码不能为空!")
}
if email == "" {
this.jsonError("邮箱不能为空!")
}
if !v.Email(email, "email").Ok {
this.jsonError("邮箱格式不正确!")
}
if mobile == "" {
this.jsonError("手机号不能为空!")
}
//if !v.Mobile(mobile, "mobile").Ok {
// this.jsonError("手机号格式不正确!")
//}
if roleId == "" {
this.jsonError("没有选择角色!")
}
//if phone != "" && !v.Phone(phone, "phone").Ok {
// this.jsonError("电话格式不正确!")
//}
ok, err := models.UserModel.HasUsername(username)
if err != nil {
this.ErrorLog("添加用户失败:" + err.Error())
this.jsonError("添加用户失败!")
}
if ok {
this.jsonError("用户名已经存在!")
}
if !this.IsRoot() && roleId == fmt.Sprintf("%d", models.Role_Root_Id) {
this.jsonError("没有权限添加超级管理员!")
}
userId, err := models.UserModel.Insert(map[string]interface{}{
"username": username,
"given_name": givenName,
"password": models.UserModel.EncodePassword(password),
"email": email,
"mobile": mobile,
"phone": phone,
"department": department,
"position": position,
"location": location,
"im": im,
"role_id": roleId,
})
if err != nil {
this.ErrorLog("添加用户失败:" + err.Error())
this.jsonError("添加用户失败")
}
this.InfoLog("添加用户 " + utils.Convert.IntToString(userId, 10) + " 成功")
this.jsonSuccess("添加用户成功", nil, "/system/user/list")
}
func (this *UserController) List() {
keywords := map[string]string{}
page, _ := this.GetInt("page", 1)
username := strings.TrimSpace(this.GetString("username", ""))
roleId := strings.TrimSpace(this.GetString("role_id", ""))
number, _ := this.GetRangeInt("number", 20, 10, 100)
if username != "" {
keywords["username"] = username
}
if roleId != "" {
keywords["role_id"] = roleId
}
limit := (page - 1) * number
var err error
var count int64
var users []map[string]string
if len(keywords) != 0 {
count, err = models.UserModel.CountUsersByKeywords(keywords)
users, err = models.UserModel.GetUsersByKeywordsAndLimit(keywords, limit, number)
} else {
count, err = models.UserModel.CountUsers()
users, err = models.UserModel.GetUsersByLimit(limit, number)
}
if err != nil {
this.ErrorLog("获取用户列表失败: " + err.Error())
this.ViewError("获取用户列表失败", "/system/main/index")
}
var roleIds = []string{}
if roleId != "" {
roleIds = append(roleIds, roleId)
} else {
for _, user := range users {
roleIds = append(roleIds, user["role_id"])
}
}
roles, err := models.RoleModel.GetRoleByRoleIds(roleIds)
if err != nil {
this.ErrorLog("获取用户列表失败: " + err.Error())
this.ViewError("获取用户列表失败!", "/system/main/index")
}
var roleUsers = []map[string]string{}
for _, user := range users {
roleUser := user
for _, role := range roles {
if role["role_id"] == user["role_id"] {
roleUser["role_name"] = role["name"]
break
}
}
roleUsers = append(roleUsers, roleUser)
}
allRoles, err := models.RoleModel.GetRoles()
if err != nil {
this.ErrorLog("获取用户列表失败: " + err.Error())
this.ViewError("获取用户列表失败!", "/system/main/index")
}
this.Data["users"] = roleUsers
this.Data["username"] = username
this.Data["roleId"] = roleId
this.Data["roles"] = allRoles
this.SetPaginator(number, count)
this.viewLayout("user/list", "user")
}
func (this *UserController) Edit() {
userId := this.GetString("user_id", "")
if userId == "" {
this.ViewError("用户不存在!", "/system/user/list")
}
user, err := models.UserModel.GetUserByUserId(userId)
if err != nil {
this.ErrorLog("查找用户出错:" + err.Error())
this.ViewError("查找用户出错!", "/system/user/list")
}
if len(user) == 0 {
this.ViewError("用户不存在!", "/system/user/list")
}
// 登录非 root 用户不能修改 root 用户信息
if user["role_id"] == fmt.Sprintf("%d", models.Role_Root_Id) && !this.IsRoot() {
this.ViewError("没有权限修改!", "/system/user/list")
}
roles := []map[string]string{}
if this.IsRoot() {
roles, err = models.RoleModel.GetRoles()
} else {
roles, err = models.RoleModel.GetRolesNotContainRoot()
}
if err != nil {
this.ErrorLog("获取用户角色失败:" + err.Error())
this.ViewError("获取用户角色失败!")
}
this.Data["user"] = user
this.Data["roles"] = roles
this.viewLayout("user/edit", "user")
}
func (this *UserController) Modify() {
if !this.IsPost() {
this.ViewError("请求方式有误!", "/system/user/list")
}
userId := strings.TrimSpace(this.GetString("user_id", ""))
givenName := strings.TrimSpace(this.GetString("given_name", ""))
email := strings.TrimSpace(this.GetString("email", ""))
mobile := strings.TrimSpace(this.GetString("mobile", ""))
roleId := strings.TrimSpace(this.GetString("role_id", ""))
phone := strings.TrimSpace(this.GetString("phone", ""))
department := strings.TrimSpace(this.GetString("department", ""))
position := strings.TrimSpace(this.GetString("position", ""))
location := strings.TrimSpace(this.GetString("location", ""))
im := strings.TrimSpace(this.GetString("im", ""))
password := strings.TrimSpace(this.GetString("password", ""))
this.Ctx.Request.PostForm.Del("password")
v := validation.Validation{}
if givenName == "" {
this.jsonError("姓名不能为空!")
}
if email == "" {
this.jsonError("邮箱不能为空!")
}
if !v.Email(email, "email").Ok {
this.jsonError("邮箱格式不正确!")
}
if mobile == "" {
this.jsonError("手机号不能为空!")
}
//if !v.Mobile(mobile, "mobile").Ok {
// this.jsonError("手机号格式不正确!")
//}
//if roleId == "" {
// this.jsonError("没有选择角色!")
//}
//if phone != "" && !v.Phone(phone, "phone").Ok {
// this.jsonError("电话格式不正确!")
//}
user, err := models.UserModel.GetUserByUserId(userId)
if err != nil {
this.ErrorLog("修改用户 " + userId + " 失败:" + err.Error())
this.jsonError("修改用户出错!")
}
if len(user) == 0 {
this.jsonError("用户不存在!")
}
if user["role_id"] == fmt.Sprintf("%d", models.Role_Root_Id) {
roleId = fmt.Sprintf("%d", models.Role_Root_Id)
}
// 登录非 root 用户不能修改 root 用户信息
if user["role_id"] == fmt.Sprintf("%d", models.Role_Root_Id) && !this.IsRoot() {
this.jsonError("没有权限修改!")
}
updateUser := map[string]interface{}{
"given_name": givenName,
"email": email,
"mobile": mobile,
"phone": phone,
"department": department,
"position": position,
"location": location,
"im": im,
}
// 超级管理员才可以修改其他用户密码
if password != "" && this.IsRoot() {
updateUser["password"] = models.UserModel.EncodePassword(password)
}
if roleId != "" {
updateUser["role_id"] = roleId
}
_, err = models.UserModel.Update(userId, updateUser)
if err != nil {
this.ErrorLog("修改用户 " + userId + " 失败:" + err.Error())
this.jsonError("修改用户失败")
}
this.InfoLog("修改用户 " + userId + " 成功")
this.jsonSuccess("修改用户成功", nil, "/system/user/list")
}
func (this *UserController) Forbidden() {
if !this.IsPost() {
this.ViewError("请求方式有误!", "/system/user/list")
}
userId := this.GetString("user_id", "")
if userId == "" {
this.jsonError("用户不存在")
}
user, err := models.UserModel.GetUserByUserId(userId)
if err != nil {
this.ErrorLog("屏蔽用户 " + userId + " 失败: " + err.Error())
this.jsonError("屏蔽用户失败")
}
if len(user) == 0 {
this.jsonError("用户不存在")
}
if user["role_id"] == fmt.Sprintf("%d", models.Role_Root_Id) {
this.jsonError("不能操作超级管理员")
}
_, err = models.UserModel.Update(userId, map[string]interface{}{
"is_forbidden": models.User_Forbidden_True,
})
if err != nil {
this.ErrorLog("屏蔽用户 " + userId + " 失败: " + err.Error())
this.jsonError("屏蔽用户失败")
}
this.InfoLog("屏蔽用户 " + userId + " 成功")
this.jsonSuccess("屏蔽用户成功", nil, "/system/user/list")
}
func (this *UserController) Recover() {
if !this.IsPost() {
this.ViewError("请求方式有误!", "/system/user/list")
}
userId := this.GetString("user_id", "")
if userId == "" {
this.jsonError("用户不存在")
}
user, err := models.UserModel.GetUserByUserId(userId)
if err != nil {
this.ErrorLog("恢复用户 " + userId + " 失败: " + err.Error())
this.jsonError("恢复用户失败")
}
if len(user) == 0 {
this.jsonError("用户不存在")
}
if user["role_id"] == fmt.Sprintf("%d", models.Role_Root_Id) {
this.jsonError("不能操作超级管理员")
}
_, err = models.UserModel.Update(userId, map[string]interface{}{
"is_forbidden": models.User_Is_Forbidden_False,
})
if err != nil {
this.ErrorLog("恢复用户 " + userId + " 失败: " + err.Error())
this.jsonError("恢复用户失败")
}
this.InfoLog("恢复用户 " + userId + " 成功")
this.jsonSuccess("恢复用户成功", nil, "/system/user/list")
}
func (this *UserController) Info() {
userId := this.GetString("user_id", "")
if userId == "" {
this.ViewError("用户不存在!", "/system/user/list")
}
user, err := models.UserModel.GetUserByUserId(userId)
if err != nil {
this.ErrorLog("查找用户出错:" + err.Error())
this.ViewError("查找用户出错!", "/system/user/list")
}
if len(user) == 0 {
this.ViewError("用户不存在!", "/system/user/list")
}
role, err := models.RoleModel.GetRoleByRoleId(user["role_id"])
if err != nil {
this.ErrorLog("查找用户角色出错:" + err.Error())
this.ViewError("查找用户出错!", "/system/user/list")
}
this.Data["user"] = user
this.Data["role"] = role
this.viewLayout("user/info", "user")
}