You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
6.1 KiB
201 lines
6.1 KiB
package controller |
|
|
|
import ( |
|
"net/http" |
|
|
|
"gofaster/internal/auth/model" |
|
"gofaster/internal/auth/service" |
|
"gofaster/internal/shared/middleware" |
|
"gofaster/internal/shared/response" |
|
|
|
"github.com/gin-gonic/gin" |
|
) |
|
|
|
type PasswordController struct { |
|
passwordService *service.PasswordService |
|
userService *service.UserService |
|
} |
|
|
|
func NewPasswordController( |
|
passwordService *service.PasswordService, |
|
userService *service.UserService, |
|
) *PasswordController { |
|
return &PasswordController{ |
|
passwordService: passwordService, |
|
userService: userService, |
|
} |
|
} |
|
|
|
// ChangePassword 修改密码 |
|
// @Summary 修改密码 |
|
// @Description 用户修改自己的密码 |
|
// @Tags 密码管理 |
|
// @Accept json |
|
// @Produce json |
|
// @Param request body object{current_password=string,new_password=string,confirm_password=string} true "密码修改请求" |
|
// @Success 200 {object} response.Response |
|
// @Failure 400 {object} response.Response |
|
// @Failure 500 {object} response.Response |
|
// @Router /api/auth/change-password [post] |
|
func (c *PasswordController) ChangePassword(ctx *gin.Context) { |
|
var req struct { |
|
CurrentPassword string `json:"current_password" binding:"required"` |
|
NewPassword string `json:"new_password" binding:"required"` |
|
ConfirmPassword string `json:"confirm_password" binding:"required"` |
|
} |
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error()) |
|
return |
|
} |
|
|
|
// 验证确认密码 |
|
if req.NewPassword != req.ConfirmPassword { |
|
response.Error(ctx, http.StatusBadRequest, "新密码与确认密码不一致", "") |
|
return |
|
} |
|
|
|
// 获取当前用户ID |
|
userID := middleware.GetUserID(ctx) |
|
|
|
// 调用服务层修改密码 |
|
err := c.passwordService.ChangePassword(ctx, userID, req.CurrentPassword, req.NewPassword) |
|
if err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "修改密码失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "密码修改成功", nil) |
|
} |
|
|
|
// ResetPassword 重置密码 |
|
// @Summary 重置密码 |
|
// @Description 管理员重置用户密码 |
|
// @Tags 密码管理 |
|
// @Accept json |
|
// @Produce json |
|
// @Param id path int true "用户ID" |
|
// @Param request body object{new_password=string} true "新密码" |
|
// @Success 200 {object} response.Response |
|
// @Failure 400 {object} response.Response |
|
// @Failure 500 {object} response.Response |
|
// @Router /api/auth/admin/users/{id}/reset-password [post] |
|
func (c *PasswordController) ResetPassword(ctx *gin.Context) { |
|
var req struct { |
|
UserID uint `json:"user_id" binding:"required"` |
|
} |
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error()) |
|
return |
|
} |
|
|
|
// 调用服务层重置密码 |
|
err := c.passwordService.ResetPassword(ctx, req.UserID) |
|
if err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "重置密码失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "密码重置成功", nil) |
|
} |
|
|
|
// GetPasswordPolicy 获取密码策略 |
|
// @Summary 获取密码策略 |
|
// @Description 获取系统密码策略配置 |
|
// @Tags 密码管理 |
|
// @Accept json |
|
// @Produce json |
|
// @Success 200 {object} response.Response{data=model.PasswordPolicy} |
|
// @Failure 500 {object} response.Response |
|
// @Router /api/auth/password-policy [get] |
|
func (c *PasswordController) GetPasswordPolicy(ctx *gin.Context) { |
|
policy, err := c.passwordService.GetPasswordPolicy() |
|
if err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "获取密码策略失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "获取密码策略成功", policy) |
|
} |
|
|
|
// ValidatePassword 验证密码 |
|
// @Summary 验证密码 |
|
// @Description 验证密码是否符合策略要求 |
|
// @Tags 密码管理 |
|
// @Accept json |
|
// @Produce json |
|
// @Param request body object{password=string} true "密码" |
|
// @Success 200 {object} response.Response{data=object{valid=bool,message=string}} |
|
// @Failure 400 {object} response.Response |
|
// @Router /api/auth/validate-password [post] |
|
func (c *PasswordController) ValidatePassword(ctx *gin.Context) { |
|
var req struct { |
|
Password string `json:"password" binding:"required"` |
|
} |
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error()) |
|
return |
|
} |
|
|
|
// 获取当前用户ID |
|
userID := middleware.GetUserID(ctx) |
|
|
|
result, err := c.passwordService.ValidatePassword(ctx, userID, req.Password) |
|
if err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "密码验证失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "密码验证完成", result) |
|
} |
|
|
|
// CheckPasswordStatus 检查密码状态 |
|
// @Summary 检查密码状态 |
|
// @Description 检查当前用户密码状态(是否需要修改) |
|
// @Tags 密码管理 |
|
// @Accept json |
|
// @Produce json |
|
// @Success 200 {object} response.Response{data=object{need_change=bool,days_remaining=int}} |
|
// @Failure 500 {object} response.Response |
|
// @Router /api/auth/password-status [get] |
|
func (c *PasswordController) CheckPasswordStatus(ctx *gin.Context) { |
|
userID := middleware.GetUserID(ctx) |
|
|
|
status, err := c.passwordService.CheckPasswordStatus(ctx, userID) |
|
if err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "检查密码状态失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "检查密码状态成功", status) |
|
} |
|
|
|
// UpdatePasswordPolicy 更新密码策略 |
|
// @Summary 更新密码策略 |
|
// @Description 管理员更新系统密码策略配置 |
|
// @Tags 密码管理 |
|
// @Accept json |
|
// @Produce json |
|
// @Param policy body model.PasswordPolicy true "密码策略" |
|
// @Success 200 {object} response.Response{data=model.PasswordPolicy} |
|
// @Failure 400 {object} response.Response |
|
// @Failure 500 {object} response.Response |
|
// @Router /api/auth/admin/password-policy [put] |
|
func (c *PasswordController) UpdatePasswordPolicy(ctx *gin.Context) { |
|
var policy model.PasswordPolicy |
|
|
|
if err := ctx.ShouldBindJSON(&policy); err != nil { |
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error()) |
|
return |
|
} |
|
|
|
err := c.passwordService.UpdatePasswordPolicy(&policy) |
|
if err != nil { |
|
response.Error(ctx, http.StatusInternalServerError, "更新密码策略失败", err.Error()) |
|
return |
|
} |
|
|
|
response.Success(ctx, "密码策略更新成功", nil) |
|
}
|
|
|