updateUserStatusLogic.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. func (l *UpdateUserStatusLogic) UpdateUserStatus(req *types.UpdateUserStatusReq) error {
  25. if req.Status != consts.StatusEnabled && req.Status != consts.StatusDisabled {
  26. return response.ErrBadRequest("状态值无效,仅支持 1(启用) 和 2(冻结)")
  27. }
  28. callerId := middleware.GetUserId(l.ctx)
  29. if callerId == req.Id {
  30. return response.ErrBadRequest("不能修改自己的状态")
  31. }
  32. user, err := l.svcCtx.SysUserModel.FindOne(l.ctx, req.Id)
  33. if err != nil {
  34. return response.ErrNotFound("用户不存在")
  35. }
  36. if user.IsSuperAdmin == consts.IsSuperAdminYes {
  37. return response.ErrForbidden("不能修改超级管理员的状态")
  38. }
  39. productCode := middleware.GetProductCode(l.ctx)
  40. if err := authHelper.CheckManageAccess(l.ctx, l.svcCtx, req.Id, productCode); err != nil {
  41. return err
  42. }
  43. if err := l.svcCtx.SysUserModel.UpdateStatus(l.ctx, req.Id, req.Status); err != nil {
  44. return err
  45. }
  46. l.svcCtx.UserDetailsLoader.Clean(l.ctx, req.Id)
  47. return nil
  48. }