diff --git a/models/user.go b/models/user.go index 5ae94e2..64dd24a 100644 --- a/models/user.go +++ b/models/user.go @@ -41,6 +41,13 @@ func (user *User) Update(id int, data CommonMap) (int64, error) { return Db.Table(user).ID(id).Update(data) } +func (user *User) UpdatePassword(id int, password string) (int64, error) { + salt := user.generateSalt() + safePassword := user.encryptPassword(password, salt) + + return user.Update(id, CommonMap{"password": safePassword, "salt": salt}) +} + // 删除 func (user *User) Delete(id int) (int64, error) { return Db.Id(id).Delete(user) diff --git a/routers/user/user.go b/routers/user/user.go index 9bfd146..d00fef0 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -16,6 +16,37 @@ func Login(ctx *macaron.Context) { ctx.HTML(200, "user/login") } +func EditPassword(ctx *macaron.Context) { + ctx.Data["Title"] = "修改密码" + ctx.HTML(200, "user/editPassword") +} + +func UpdatePassword(ctx *macaron.Context, sess session.Store) string { + oldPassword := ctx.QueryTrim("old_password") + newPassword := ctx.QueryTrim("new_password") + confirmNewPassword := ctx.QueryTrim("confirm_new_password") + json := utils.JsonResponse{} + if oldPassword == "" || newPassword == "" || confirmNewPassword == "" { + return json.CommonFailure("原密码和新密码均不能为空") + } + if newPassword != confirmNewPassword { + return json.CommonFailure("两次输入密码不一致") + } + if oldPassword == newPassword { + return json.CommonFailure("原密码与新密码不能相同") + } + userModel := new(models.User) + if !userModel.Match(Username(sess), oldPassword) { + return json.CommonFailure("原密码输入错误") + } + _, err := userModel.UpdatePassword(Uid(sess), newPassword) + if err != nil { + return json.CommonFailure("修改失败") + } + + return json.Success("修改成功", nil) +} + func ValidateLogin(ctx *macaron.Context, sess session.Store) string { username := ctx.QueryTrim("username") password := ctx.QueryTrim("password") diff --git a/templates/common/header.html b/templates/common/header.html index 16c660d..80ca88a 100644 --- a/templates/common/header.html +++ b/templates/common/header.html @@ -46,6 +46,7 @@ 你好,{{{.LoginUsername}}}
{{{end}}} @@ -61,7 +62,7 @@ 主机 {{{if gt .LoginUid 0}}} - 管理 + 配置 {{{end}}} diff --git a/templates/user/editPassword.html b/templates/user/editPassword.html new file mode 100644 index 0000000..a9714aa --- /dev/null +++ b/templates/user/editPassword.html @@ -0,0 +1,73 @@ +{{{ template "common/header" . }}} +