updateUserStatusLogic.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package user
  2. import (
  3. "context"
  4. "perms-system-server/internal/consts"
  5. authHelper "perms-system-server/internal/logic/auth"
  6. "perms-system-server/internal/middleware"
  7. "perms-system-server/internal/response"
  8. "perms-system-server/internal/svc"
  9. "perms-system-server/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type UpdateUserStatusLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewUpdateUserStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserStatusLogic {
  18. return &UpdateUserStatusLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. }
  23. }
  24. // UpdateUserStatus 冻结/解冻用户。修改用户启用状态并递增 tokenVersion 使其令牌失效。不能修改自身或超管状态。
  25. func (l *UpdateUserStatusLogic) UpdateUserStatus(req *types.UpdateUserStatusReq) error {
  26. if req.Status != consts.StatusEnabled && req.Status != consts.StatusDisabled {
  27. return response.ErrBadRequest("状态值无效,仅支持 1(启用) 和 2(冻结)")
  28. }
  29. callerId := middleware.GetUserId(l.ctx)
  30. if callerId == req.Id {
  31. return response.ErrBadRequest("不能修改自己的状态")
  32. }
  33. user, err := l.svcCtx.SysUserModel.FindOne(l.ctx, req.Id)
  34. if err != nil {
  35. return response.ErrNotFound("用户不存在")
  36. }
  37. if user.IsSuperAdmin == consts.IsSuperAdminYes {
  38. return response.ErrForbidden("不能修改超级管理员的状态")
  39. }
  40. productCode := middleware.GetProductCode(l.ctx)
  41. if err := authHelper.CheckManageAccess(l.ctx, l.svcCtx, req.Id, productCode); err != nil {
  42. return err
  43. }
  44. if err := l.svcCtx.SysUserModel.UpdateStatus(l.ctx, req.Id, req.Status); err != nil {
  45. return err
  46. }
  47. l.svcCtx.UserDetailsLoader.Clean(l.ctx, req.Id)
  48. return nil
  49. }