Skip to content

Commit

Permalink
Merge pull request #128 from phachon/feature/v0.1.5
Browse files Browse the repository at this point in the history
fix user modify; add root update default user password
  • Loading branch information
phachon authored Dec 28, 2019
2 parents 99c1ed2 + 17cfd69 commit c779c88
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 25 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
### Fix Bug & Add Feature
#### 修复bug
1. 修复空间修改报错
2. 修复用户管理修改用户bug

#### 新增功能
1. 超级管理员可以重置用户密码

### 升级(Upgrade)
1. 下载新版本到部署该项目的根目录
2. 覆盖解压 (tar -zxvf mm-wiki-v0.1.5-mac-amd64.tar.gz)
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (this *TemplateController) isLogin() bool {

this.Data["login_user_id"] = this.UserId
this.Data["login_username"] = this.User["username"]
this.Data["login_role_id"] = this.User["role_id"]

// success
return true
Expand Down Expand Up @@ -264,6 +265,7 @@ func (this *TemplateController) IsGet() bool {
return this.Ctx.Input.Method() == "GET"
}

// 是否是超级管理员
func (this *TemplateController) IsRoot() bool {
return this.User["role_id"] == fmt.Sprintf("%d", models.Role_Root_Id)
}
Expand Down
6 changes: 3 additions & 3 deletions app/models/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const (
Role_Type_System = 1
Role_Type_Default = 0

Role_Root_Id = 1
Role_Admin_Id = 2
Role_Default_Id = 3
Role_Root_Id = 1 // 超级管理员
Role_Admin_Id = 2 // 管理员
Role_Default_Id = 3 // 普通用户
)

const Table_Role_Name = "role"
Expand Down
12 changes: 6 additions & 6 deletions app/modules/system/controllers/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ func (this *ProfileController) Modify() {
if mobile == "" {
this.jsonError("手机号不能为空!")
}
if !v.Mobile(mobile, "mobile").Ok {
this.jsonError("手机号格式不正确!")
}
if phone != "" && !v.Phone(phone, "phone").Ok {
this.jsonError("电话格式不正确!")
}
//if !v.Mobile(mobile, "mobile").Ok {
// this.jsonError("手机号格式不正确!")
//}
//if phone != "" && !v.Phone(phone, "phone").Ok {
// this.jsonError("电话格式不正确!")
//}

_, err := models.UserModel.Update(this.UserId, map[string]interface{}{
"given_name": givenName,
Expand Down
34 changes: 25 additions & 9 deletions app/modules/system/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ func (this *UserController) Edit() {
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() {
Expand Down Expand Up @@ -231,6 +235,8 @@ func (this *UserController) Modify() {
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 == "" {
Expand All @@ -248,37 +254,47 @@ func (this *UserController) Modify() {
//if !v.Mobile(mobile, "mobile").Ok {
// this.jsonError("手机号格式不正确!")
//}
if roleId == "" {
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.ViewError("修改用户出错!", "/system/user/list")
this.jsonError("修改用户出错!")
}
if len(user) == 0 {
this.ViewError("用户不存在!", "/system/user/list")
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("没有权限修改!")
}

_, err = models.UserModel.Update(userId, map[string]interface{}{
updateUser := map[string]interface{}{
"given_name": givenName,
"email": email,
"mobile": mobile,
"role_id": roleId,
"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("修改用户失败")
Expand Down
11 changes: 11 additions & 0 deletions views/system/user/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@
</div>
</div>
</div>
<!-- 超级管理员才能修改用户密码 -->
{{if eq $.login_role_id "1"}}
<div class="form-group">
<div class="row">
<label class="col-sm-3 control-label"><span class="text-danger"></span> 密码</label>
<div class="col-sm-8">
<input type="text" name="password" class="form-control" placeholder="注意:输入新密码后将重置该用户密码" value="" autocomplete="new-password">
</div>
</div>
</div>
{{end}}
<div class="form-group">
<div class="row">
<div class="col-sm-offset-3">
Expand Down
19 changes: 13 additions & 6 deletions views/system/user/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,21 @@
{{end}}
</td>
<td class="center">
<a name="edit" data-link="/system/user/edit?user_id={{$user.user_id}}"><i class="glyphicon glyphicon-edit"></i>修改</a>
{{if ne $user.user_id "1"}}
{{if eq $user.is_forbidden "1"}}
<a onclick="Layers.confirm('确定恢复该用户吗?', '/system/user/recover?user_id={{$user.user_id}}');"><i class="glyphicon glyphicon-ok"></i>恢复</a>
<!-- 修改 root 用户只能被自己修改 -->
{{if eq $user.role_id "1"}}
{{if eq $.login_role_id "1"}}
<a name="edit" data-link="/system/user/edit?user_id={{$user.user_id}}"><i class="glyphicon glyphicon-edit"></i>修改</a>
{{else}}
<label class="text text-danger">暂无权限</label>
{{end}}
{{else}}
<a onclick="Layers.confirm('确定屏蔽该用户吗?', '/system/user/forbidden?user_id={{$user.user_id}}');"><i class="glyphicon glyphicon-remove"></i>屏蔽</a>
<a name="edit" data-link="/system/user/edit?user_id={{$user.user_id}}"><i class="glyphicon glyphicon-edit"></i>修改</a>
{{if eq $user.is_forbidden "1"}}
<a onclick="Layers.confirm('确定恢复该用户吗?', '/system/user/recover?user_id={{$user.user_id}}');"><i class="glyphicon glyphicon-ok"></i>恢复</a>
{{else}}
<a onclick="Layers.confirm('确定屏蔽该用户吗?', '/system/user/forbidden?user_id={{$user.user_id}}');"><i class="glyphicon glyphicon-remove"></i>屏蔽</a>
{{end}}
{{end}}
{{end}}
</td>
</tr>
{{end}}
Expand Down

0 comments on commit c779c88

Please sign in to comment.