updateUserStatusLogic.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 productCode != "" {
  41. caller := middleware.GetUserDetails(l.ctx)
  42. if caller != nil && !caller.IsSuperAdmin {
  43. if _, err := l.svcCtx.SysProductMemberModel.FindOneByProductCodeUserId(l.ctx, productCode, req.Id); err != nil {
  44. return response.ErrBadRequest("目标用户不是当前产品的成员")
  45. }
  46. }
  47. }
  48. if err := authHelper.CheckManageAccess(l.ctx, l.svcCtx, req.Id, productCode); err != nil {
  49. return err
  50. }
  51. if err := l.svcCtx.SysUserModel.UpdateStatus(l.ctx, req.Id, req.Status); err != nil {
  52. return err
  53. }
  54. l.svcCtx.UserDetailsLoader.Clean(l.ctx, req.Id)
  55. return nil
  56. }