updateDeptLogic.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package dept
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "perms-system-server/internal/consts"
  7. authHelper "perms-system-server/internal/logic/auth"
  8. deptModel "perms-system-server/internal/model/dept"
  9. "perms-system-server/internal/response"
  10. "perms-system-server/internal/svc"
  11. "perms-system-server/internal/types"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type UpdateDeptLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewUpdateDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateDeptLogic {
  20. return &UpdateDeptLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx,
  24. }
  25. }
  26. // UpdateDept 更新部门。修改部门名称、排序、类型、备注或启用/禁用状态。使用乐观锁防止并发冲突,变更部门类型或状态时自动清理受影响用户的权限缓存。
  27. func (l *UpdateDeptLogic) UpdateDept(req *types.UpdateDeptReq) error {
  28. if err := authHelper.RequireSuperAdmin(l.ctx); err != nil {
  29. return err
  30. }
  31. if len(req.Name) > 64 {
  32. return response.ErrBadRequest("部门名称长度不能超过64个字符")
  33. }
  34. if len(req.Remark) > 255 {
  35. return response.ErrBadRequest("备注长度不能超过255个字符")
  36. }
  37. dept, err := l.svcCtx.SysDeptModel.FindOne(l.ctx, req.Id)
  38. if err != nil {
  39. return response.ErrNotFound("部门不存在")
  40. }
  41. deptTypeChanged := false
  42. statusChanged := false
  43. dept.Name = req.Name
  44. dept.Sort = req.Sort
  45. dept.Remark = req.Remark
  46. if req.DeptType != "" {
  47. if req.DeptType != consts.DeptTypeNormal && req.DeptType != consts.DeptTypeDev {
  48. return response.ErrBadRequest("部门类型无效,仅支持 NORMAL 和 DEV")
  49. }
  50. if dept.DeptType != req.DeptType {
  51. deptTypeChanged = true
  52. dept.DeptType = req.DeptType
  53. }
  54. }
  55. if req.Status != 0 {
  56. if req.Status != consts.StatusEnabled && req.Status != consts.StatusDisabled {
  57. return response.ErrBadRequest("状态值无效,仅支持 1(启用) 和 2(禁用)")
  58. }
  59. if dept.Status != req.Status {
  60. statusChanged = true
  61. dept.Status = req.Status
  62. }
  63. }
  64. expectedUpdateTime := dept.UpdateTime
  65. dept.UpdateTime = time.Now().Unix()
  66. if err := l.svcCtx.SysDeptModel.UpdateWithOptLock(l.ctx, dept, expectedUpdateTime); err != nil {
  67. if errors.Is(err, deptModel.ErrUpdateConflict) {
  68. return response.ErrConflict("数据已被其他操作修改,请刷新后重试")
  69. }
  70. return err
  71. }
  72. // loadPerms 只检查用户自身部门的 deptType/status,子部门不受影响,
  73. // 因此仅需清理本部门直属用户缓存,且仅在 deptType 或 status 真正变更时才需要。
  74. // 使用 CleanByUserIds 把 N 用户 × 3 RTT 的串行 Clean 压成常数 2 RTT,避免把 handler
  75. // 挂住拖慢乐观锁重试(见审计 M-1)。FindIdsByDeptId 的错误必须显式 Errorf 记录而不能
  76. // 静默 "_, _" 吞掉——DB 抖动会导致"被禁用部门"的旧权限缓存继续在 TTL 窗口内生效,
  77. // 这是安全敏感变更;但遵循 M-4 post-commit 模式不映射 500,由 TTL 过期兜底。
  78. if deptTypeChanged || statusChanged {
  79. userIds, err := l.svcCtx.SysUserModel.FindIdsByDeptId(l.ctx, req.Id)
  80. if err != nil {
  81. l.Errorf("UpdateDept id=%d deptType=%s status=%d 部门已更新但 FindIdsByDeptId 失败,用户权限缓存未能主动失效,将等待 TTL 自然过期: %v", req.Id, dept.DeptType, dept.Status, err)
  82. return nil
  83. }
  84. if len(userIds) > 0 {
  85. l.svcCtx.UserDetailsLoader.CleanByUserIds(l.ctx, userIds)
  86. l.Infof("UpdateDept id=%d deptType=%s status=%d affectedUsers=%d", req.Id, dept.DeptType, dept.Status, len(userIds))
  87. }
  88. }
  89. return nil
  90. }