updateDeptLogic.go 4.0 KB

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