|
|
|
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 修改密码
|
|
|
|
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 重置密码
|
|
|
|
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 获取密码策略
|
|
|
|
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 验证密码
|
|
|
|
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 检查密码状态
|
|
|
|
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 更新密码策略
|
|
|
|
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)
|
|
|
|
}
|