fix: 禁用角色时检查是否已分配给账号或店铺
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled

角色禁用前新增分配情况检查,若角色已分配给账号或店铺则拒绝禁用操作,
需先移除相关分配后才能禁用,与删除角色的防护逻辑保持一致。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-09 14:35:55 +08:00
parent c1cec0aede
commit 0f6fd42aff

View File

@@ -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