mirror of https://github.com/ouqiang/gocron
增加用户修改密码功能
parent
3a15f88829
commit
9a7b53c031
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -46,6 +46,7 @@
|
|||
你好,{{{.LoginUsername}}}
|
||||
<i class="dropdown icon"></i>
|
||||
<div class="menu">
|
||||
<a href="/user/editPassword" class="item"><i class="edit icon"></i>修改密码</a>
|
||||
<a class="item" href="/user/logout"><i class="sign out icon"></i>退出</a>
|
||||
</div>
|
||||
{{{end}}}
|
||||
|
@ -61,7 +62,7 @@
|
|||
<a class="item {{{if eq .Controller "host"}}}active{{{end}}}" href="/host"><i class="linux icon"></i>主机</a>
|
||||
<!-- <a class="item {{{if eq .Controller "user"}}}active{{{end}}}" href="/user"><i class="user icon"></i>账户</a> -->
|
||||
{{{if gt .LoginUid 0}}}
|
||||
<a class="item {{{if eq .Controller "admin"}}}active{{{end}}}" href="/admin/setting/slack"><i class="settings icon"></i>管理</a>
|
||||
<a class="item {{{if eq .Controller "setting"}}}active{{{end}}}" href="/setting/slack/edit"><i class="settings icon"></i>配置</a>
|
||||
{{{end}}}
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
{{{ template "common/header" . }}}
|
||||
<div class="ui small modal">
|
||||
<div class="header">修改密码</div>
|
||||
<div class="content">
|
||||
<form class="ui form">
|
||||
<div class="two fields">
|
||||
<div class="field">
|
||||
<label>原密码</label>
|
||||
<input class="small input" type="password" name="old_password" placeholder="原密码">
|
||||
</div>
|
||||
</div>
|
||||
<div class="two fields">
|
||||
<div class="field">
|
||||
<label>新密码</label>
|
||||
<input type="password" name="new_password" placeholder="新密码">
|
||||
</div>
|
||||
</div>
|
||||
<div class="two fields">
|
||||
<div class="field">
|
||||
<label>确认新密码</label>
|
||||
<input type="password" name="confirm_new_password" placeholder="确认新密码">
|
||||
</div>
|
||||
</div>
|
||||
<button class="ui button primary button" >登录</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
$('.ui.modal').modal('setting', 'closable', false).modal('show');
|
||||
$('.ui.form').form(
|
||||
{
|
||||
onSuccess: function(event, fields) {
|
||||
util.post('/user/editPassword', fields, function(code, message) {
|
||||
swal("操作成功", '修改成功', 'success');
|
||||
location.href = "/task"
|
||||
});
|
||||
|
||||
return false;
|
||||
},
|
||||
fields: {
|
||||
oldPassword: {
|
||||
identifier : 'old_password',
|
||||
rules: [
|
||||
{
|
||||
type : 'empty',
|
||||
prompt : '请输入原密码'
|
||||
}
|
||||
]
|
||||
},
|
||||
newPassword: {
|
||||
identifier : 'new_password',
|
||||
rules: [
|
||||
{
|
||||
type : 'empty',
|
||||
prompt : '请输入新密码'
|
||||
}
|
||||
]
|
||||
},
|
||||
confirmNewPassword: {
|
||||
identifier : 'confirm_new_password',
|
||||
rules: [
|
||||
{
|
||||
type : 'match[new_password]',
|
||||
prompt : '两次输入密码不匹配'
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
inline : true
|
||||
});
|
||||
</script>
|
||||
{{{ template "common/footer" . }}}
|
Loading…
Reference in New Issue