feat: 支持管理员自助修改密码 (#57)
This commit is contained in:
@@ -113,6 +113,39 @@ func (h *Handler) Logout(c *gin.Context) {
|
||||
c.Redirect(http.StatusSeeOther, "/login?msg="+url.QueryEscape("已退出登录"))
|
||||
}
|
||||
|
||||
// ChangePassword 修改当前管理员自己的密码;目标身份只取 Session,不读表单用户编号。
|
||||
func (h *Handler) ChangePassword(c *gin.Context) {
|
||||
returnPath := passwordReturnPath(c.PostForm("next"))
|
||||
err := service.ChangeAdminPassword(h.db, currentUser(c), c.PostForm("current_password"),
|
||||
c.PostForm("new_password"), c.PostForm("password_confirm"), time.Now())
|
||||
if service.IsValidationError(err) {
|
||||
query := url.Values{
|
||||
"change_password": {"1"},
|
||||
"password_error": {err.Error()},
|
||||
}
|
||||
if field := service.ValidationField(err); field != "" {
|
||||
query.Set("password_field", field)
|
||||
}
|
||||
separator := "?"
|
||||
if strings.Contains(returnPath, "?") {
|
||||
separator = "&"
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, returnPath+separator+query.Encode())
|
||||
return
|
||||
}
|
||||
if errors.Is(err, repository.ErrUserPasswordChanged) {
|
||||
clearAuthCookie(c)
|
||||
c.Redirect(http.StatusSeeOther, "/login?msg="+url.QueryEscape("密码已在其他操作中变更,请重新登录"))
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
fail(c, http.StatusInternalServerError, "修改密码失败,原密码和现有登录保持不变。刷新后重试。")
|
||||
return
|
||||
}
|
||||
clearAuthCookie(c)
|
||||
c.Redirect(http.StatusSeeOther, "/login?msg="+url.QueryEscape("密码修改成功,请使用新密码重新登录"))
|
||||
}
|
||||
|
||||
// AuthRequired 只挂在 Web 业务路由组。Client API 注册在另一个组,不能经过这里。
|
||||
func AuthRequired(db *sql.DB) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user