Skip to content

Commit c779c88

Browse files
authored
Merge pull request #128 from phachon/feature/v0.1.5
fix user modify; add root update default user password
2 parents 99c1ed2 + 17cfd69 commit c779c88

File tree

7 files changed

+63
-25
lines changed

7 files changed

+63
-25
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
### Fix Bug & Add Feature
66
#### 修复bug
77
1. 修复空间修改报错
8+
2. 修复用户管理修改用户bug
89

910
#### 新增功能
10-
11+
1. 超级管理员可以重置用户密码
12+
1113
### 升级(Upgrade)
1214
1. 下载新版本到部署该项目的根目录
1315
2. 覆盖解压 (tar -zxvf mm-wiki-v0.1.5-mac-amd64.tar.gz)

app/controllers/template.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func (this *TemplateController) isLogin() bool {
108108

109109
this.Data["login_user_id"] = this.UserId
110110
this.Data["login_username"] = this.User["username"]
111+
this.Data["login_role_id"] = this.User["role_id"]
111112

112113
// success
113114
return true
@@ -264,6 +265,7 @@ func (this *TemplateController) IsGet() bool {
264265
return this.Ctx.Input.Method() == "GET"
265266
}
266267

268+
// 是否是超级管理员
267269
func (this *TemplateController) IsRoot() bool {
268270
return this.User["role_id"] == fmt.Sprintf("%d", models.Role_Root_Id)
269271
}

app/models/role.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ const (
1313
Role_Type_System = 1
1414
Role_Type_Default = 0
1515

16-
Role_Root_Id = 1
17-
Role_Admin_Id = 2
18-
Role_Default_Id = 3
16+
Role_Root_Id = 1 // 超级管理员
17+
Role_Admin_Id = 2 // 管理员
18+
Role_Default_Id = 3 // 普通用户
1919
)
2020

2121
const Table_Role_Name = "role"

app/modules/system/controllers/profile.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ func (this *ProfileController) Modify() {
8989
if mobile == "" {
9090
this.jsonError("手机号不能为空!")
9191
}
92-
if !v.Mobile(mobile, "mobile").Ok {
93-
this.jsonError("手机号格式不正确!")
94-
}
95-
if phone != "" && !v.Phone(phone, "phone").Ok {
96-
this.jsonError("电话格式不正确!")
97-
}
92+
//if !v.Mobile(mobile, "mobile").Ok {
93+
// this.jsonError("手机号格式不正确!")
94+
//}
95+
//if phone != "" && !v.Phone(phone, "phone").Ok {
96+
// this.jsonError("电话格式不正确!")
97+
//}
9898

9999
_, err := models.UserModel.Update(this.UserId, map[string]interface{}{
100100
"given_name": givenName,

app/modules/system/controllers/user.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ func (this *UserController) Edit() {
199199
if len(user) == 0 {
200200
this.ViewError("用户不存在!", "/system/user/list")
201201
}
202+
// 登录非 root 用户不能修改 root 用户信息
203+
if user["role_id"] == fmt.Sprintf("%d", models.Role_Root_Id) && !this.IsRoot() {
204+
this.ViewError("没有权限修改!", "/system/user/list")
205+
}
202206

203207
roles := []map[string]string{}
204208
if this.IsRoot() {
@@ -231,6 +235,8 @@ func (this *UserController) Modify() {
231235
position := strings.TrimSpace(this.GetString("position", ""))
232236
location := strings.TrimSpace(this.GetString("location", ""))
233237
im := strings.TrimSpace(this.GetString("im", ""))
238+
password := strings.TrimSpace(this.GetString("password", ""))
239+
this.Ctx.Request.PostForm.Del("password")
234240

235241
v := validation.Validation{}
236242
if givenName == "" {
@@ -248,37 +254,47 @@ func (this *UserController) Modify() {
248254
//if !v.Mobile(mobile, "mobile").Ok {
249255
// this.jsonError("手机号格式不正确!")
250256
//}
251-
if roleId == "" {
252-
this.jsonError("没有选择角色!")
253-
}
257+
//if roleId == "" {
258+
// this.jsonError("没有选择角色!")
259+
//}
254260
//if phone != "" && !v.Phone(phone, "phone").Ok {
255261
// this.jsonError("电话格式不正确!")
256262
//}
257263

258264
user, err := models.UserModel.GetUserByUserId(userId)
259265
if err != nil {
260266
this.ErrorLog("修改用户 " + userId + " 失败:" + err.Error())
261-
this.ViewError("修改用户出错!", "/system/user/list")
267+
this.jsonError("修改用户出错!")
262268
}
263269
if len(user) == 0 {
264-
this.ViewError("用户不存在!", "/system/user/list")
270+
this.jsonError("用户不存在!")
265271
}
266272
if user["role_id"] == fmt.Sprintf("%d", models.Role_Root_Id) {
267273
roleId = fmt.Sprintf("%d", models.Role_Root_Id)
268274
}
275+
// 登录非 root 用户不能修改 root 用户信息
276+
if user["role_id"] == fmt.Sprintf("%d", models.Role_Root_Id) && !this.IsRoot() {
277+
this.jsonError("没有权限修改!")
278+
}
269279

270-
_, err = models.UserModel.Update(userId, map[string]interface{}{
280+
updateUser := map[string]interface{}{
271281
"given_name": givenName,
272282
"email": email,
273283
"mobile": mobile,
274-
"role_id": roleId,
275284
"phone": phone,
276285
"department": department,
277286
"position": position,
278287
"location": location,
279288
"im": im,
280-
})
281-
289+
}
290+
// 超级管理员才可以修改其他用户密码
291+
if password != "" && this.IsRoot() {
292+
updateUser["password"] = models.UserModel.EncodePassword(password)
293+
}
294+
if roleId != "" {
295+
updateUser["role_id"] = roleId
296+
}
297+
_, err = models.UserModel.Update(userId, updateUser)
282298
if err != nil {
283299
this.ErrorLog("修改用户 " + userId + " 失败:" + err.Error())
284300
this.jsonError("修改用户失败")

views/system/user/edit.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@
6060
</div>
6161
</div>
6262
</div>
63+
<!-- 超级管理员才能修改用户密码 -->
64+
{{if eq $.login_role_id "1"}}
65+
<div class="form-group">
66+
<div class="row">
67+
<label class="col-sm-3 control-label"><span class="text-danger"></span> 密码</label>
68+
<div class="col-sm-8">
69+
<input type="text" name="password" class="form-control" placeholder="注意:输入新密码后将重置该用户密码" value="" autocomplete="new-password">
70+
</div>
71+
</div>
72+
</div>
73+
{{end}}
6374
<div class="form-group">
6475
<div class="row">
6576
<div class="col-sm-offset-3">

views/system/user/list.html

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,21 @@
6262
{{end}}
6363
</td>
6464
<td class="center">
65-
<a name="edit" data-link="/system/user/edit?user_id={{$user.user_id}}"><i class="glyphicon glyphicon-edit"></i>修改</a>
66-
{{if ne $user.user_id "1"}}
67-
{{if eq $user.is_forbidden "1"}}
68-
<a onclick="Layers.confirm('确定恢复该用户吗?', '/system/user/recover?user_id={{$user.user_id}}');"><i class="glyphicon glyphicon-ok"></i>恢复</a>
65+
<!-- 修改 root 用户只能被自己修改 -->
66+
{{if eq $user.role_id "1"}}
67+
{{if eq $.login_role_id "1"}}
68+
<a name="edit" data-link="/system/user/edit?user_id={{$user.user_id}}"><i class="glyphicon glyphicon-edit"></i>修改</a>
69+
{{else}}
70+
<label class="text text-danger">暂无权限</label>
71+
{{end}}
6972
{{else}}
70-
<a onclick="Layers.confirm('确定屏蔽该用户吗?', '/system/user/forbidden?user_id={{$user.user_id}}');"><i class="glyphicon glyphicon-remove"></i>屏蔽</a>
73+
<a name="edit" data-link="/system/user/edit?user_id={{$user.user_id}}"><i class="glyphicon glyphicon-edit"></i>修改</a>
74+
{{if eq $user.is_forbidden "1"}}
75+
<a onclick="Layers.confirm('确定恢复该用户吗?', '/system/user/recover?user_id={{$user.user_id}}');"><i class="glyphicon glyphicon-ok"></i>恢复</a>
76+
{{else}}
77+
<a onclick="Layers.confirm('确定屏蔽该用户吗?', '/system/user/forbidden?user_id={{$user.user_id}}');"><i class="glyphicon glyphicon-remove"></i>屏蔽</a>
78+
{{end}}
7179
{{end}}
72-
{{end}}
7380
</td>
7481
</tr>
7582
{{end}}

0 commit comments

Comments
 (0)