From 0f6fd42aff98107dd6a1cc3125f10ca00b585099 Mon Sep 17 00:00:00 2001 From: huang Date: Thu, 9 Apr 2026 14:35:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A6=81=E7=94=A8=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E6=97=B6=E6=A3=80=E6=9F=A5=E6=98=AF=E5=90=A6=E5=B7=B2=E5=88=86?= =?UTF-8?q?=E9=85=8D=E7=BB=99=E8=B4=A6=E5=8F=B7=E6=88=96=E5=BA=97=E9=93=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 角色禁用前新增分配情况检查,若角色已分配给账号或店铺则拒绝禁用操作, 需先移除相关分配后才能禁用,与删除角色的防护逻辑保持一致。 Co-Authored-By: Claude Sonnet 4.6 --- internal/service/role/service.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/service/role/service.go b/internal/service/role/service.go index 867aa03..2ba991c 100644 --- a/internal/service/role/service.go +++ b/internal/service/role/service.go @@ -313,6 +313,7 @@ func (s *Service) BatchRemovePermissions(ctx context.Context, roleID uint, permI } // UpdateStatus 更新角色状态 +// 禁用角色时,若角色已分配给账号或店铺,则拒绝操作 func (s *Service) UpdateStatus(ctx context.Context, id uint, status int) error { currentUserID := middleware.GetUserIDFromContext(ctx) if currentUserID == 0 { @@ -327,6 +328,23 @@ func (s *Service) UpdateStatus(ctx context.Context, id uint, status int) error { return errors.Wrap(errors.CodeInternalError, err, "获取角色失败") } + // 禁用角色时检查是否已有分配 + if status == constants.StatusDisabled { + accountCount, err := s.accountRoleStore.CountByRoleID(ctx, id) + if err != nil { + return errors.Wrap(errors.CodeInternalError, err, "检查角色分配情况失败") + } + + shopCount, err := s.shopRoleStore.CountByRoleID(ctx, id) + if err != nil { + return errors.Wrap(errors.CodeInternalError, err, "检查角色分配情况失败") + } + + if accountCount > 0 || shopCount > 0 { + return errors.New(errors.CodeRoleInUse, fmt.Sprintf("该角色已分配给 %d 个账号、%d 个店铺,请先移除相关分配后再禁用", accountCount, shopCount)) + } + } + role.Status = status role.Updater = currentUserID