| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package dept
- import (
- "context"
- "errors"
- "time"
- "perms-system-server/internal/consts"
- "perms-system-server/internal/loaders"
- authHelper "perms-system-server/internal/logic/auth"
- deptModel "perms-system-server/internal/model/dept"
- "perms-system-server/internal/response"
- "perms-system-server/internal/svc"
- "perms-system-server/internal/types"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type UpdateDeptLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewUpdateDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateDeptLogic {
- return &UpdateDeptLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- }
- }
- // UpdateDept 更新部门。修改部门名称、排序、类型、备注或启用/禁用状态。使用乐观锁防止并发冲突,变更部门类型或状态时自动清理受影响用户的权限缓存。
- func (l *UpdateDeptLogic) UpdateDept(req *types.UpdateDeptReq) error {
- if err := authHelper.RequireSuperAdmin(l.ctx); err != nil {
- return err
- }
- if len(req.Name) > 64 {
- return response.ErrBadRequest("部门名称长度不能超过64个字符")
- }
- if len(req.Remark) > 255 {
- return response.ErrBadRequest("备注长度不能超过255个字符")
- }
- dept, err := l.svcCtx.SysDeptModel.FindOne(l.ctx, req.Id)
- if err != nil {
- return response.ErrNotFound("部门不存在")
- }
- deptTypeChanged := false
- statusChanged := false
- dept.Name = req.Name
- dept.Sort = req.Sort
- dept.Remark = req.Remark
- if req.DeptType != "" {
- if req.DeptType != consts.DeptTypeNormal && req.DeptType != consts.DeptTypeDev {
- return response.ErrBadRequest("部门类型无效,仅支持 NORMAL 和 DEV")
- }
- if dept.DeptType != req.DeptType {
- deptTypeChanged = true
- dept.DeptType = req.DeptType
- }
- }
- if req.Status != 0 {
- if req.Status != consts.StatusEnabled && req.Status != consts.StatusDisabled {
- return response.ErrBadRequest("状态值无效,仅支持 1(启用) 和 2(禁用)")
- }
- if dept.Status != req.Status {
- statusChanged = true
- dept.Status = req.Status
- }
- }
- expectedUpdateTime := dept.UpdateTime
- dept.UpdateTime = time.Now().Unix()
- if err := l.svcCtx.SysDeptModel.UpdateWithOptLock(l.ctx, dept, expectedUpdateTime); err != nil {
- if errors.Is(err, deptModel.ErrUpdateConflict) {
- return response.ErrConflict("数据已被其他操作修改,请刷新后重试")
- }
- return err
- }
- // loadPerms 只检查用户自身部门的 deptType/status,子部门不受影响,
- // 因此仅需清理本部门直属用户缓存,且仅在 deptType 或 status 真正变更时才需要。
- // 使用 CleanByUserIds 把 N 用户 × 3 RTT 的串行 Clean 压成常数 2 RTT,避免把 handler
- // 挂住拖慢乐观锁重试(见审计 M-1)。FindIdsByDeptId 的错误必须显式 Errorf 记录而不能
- // 静默 "_, _" 吞掉——DB 抖动会导致"被禁用部门"的旧权限缓存继续在 TTL 窗口内生效,
- // 这是安全敏感变更;但遵循 M-4 post-commit 模式不映射 500,由 TTL 过期兜底。
- if deptTypeChanged || statusChanged {
- userIds, err := l.svcCtx.SysUserModel.FindIdsByDeptId(l.ctx, req.Id)
- if err != nil {
- l.Errorf("UpdateDept id=%d deptType=%s status=%d 部门已更新但 FindIdsByDeptId 失败,用户权限缓存未能主动失效,将等待 TTL 自然过期: %v", req.Id, dept.DeptType, dept.Status, err)
- return nil
- }
- if len(userIds) > 0 {
- // 审计 L-R13-5 方案 A:DeptType / 禁用状态是 loadPerms 的授权输入,post-commit 失效
- // 必须脱离请求 ctx——这条路径的 userIds 可能成百上千,批处理耗时略长,client 断连
- // 或 HTTP 超时后旧权限缓存滞留 5 分钟 TTL 会直接等于"禁用部门仍在放行权限"。
- cleanCtx, cancel := loaders.DetachCacheCleanCtx(l.ctx)
- defer cancel()
- l.svcCtx.UserDetailsLoader.CleanByUserIds(cleanCtx, userIds)
- l.Infof("UpdateDept id=%d deptType=%s status=%d affectedUsers=%d", req.Id, dept.DeptType, dept.Status, len(userIds))
- }
- }
- return nil
- }
|