fix: 添加角色名重复检查
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m46s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m46s
- 创建角色时检查角色名是否已存在 - 更新角色时检查角色名是否与其他角色重复 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -41,6 +41,15 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateRoleRequest) (*mode
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
// 检查角色名是否已存在
|
||||
exists, err := s.roleStore.ExistsByName(ctx, req.RoleName, 0)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "检查角色名失败")
|
||||
}
|
||||
if exists {
|
||||
return nil, errors.New(errors.CodeRoleNameExists)
|
||||
}
|
||||
|
||||
// 创建角色
|
||||
role := &model.Role{
|
||||
RoleName: req.RoleName,
|
||||
@@ -85,10 +94,19 @@ func (s *Service) Update(ctx context.Context, id uint, req *dto.UpdateRoleReques
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "获取角色失败")
|
||||
}
|
||||
|
||||
// 更新字段
|
||||
if req.RoleName != nil {
|
||||
// 如果修改了角色名,检查是否与其他角色重复
|
||||
if req.RoleName != nil && *req.RoleName != role.RoleName {
|
||||
exists, err := s.roleStore.ExistsByName(ctx, *req.RoleName, id)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "检查角色名失败")
|
||||
}
|
||||
if exists {
|
||||
return nil, errors.New(errors.CodeRoleNameExists)
|
||||
}
|
||||
role.RoleName = *req.RoleName
|
||||
}
|
||||
|
||||
// 更新其他字段
|
||||
if req.RoleDesc != nil {
|
||||
role.RoleDesc = *req.RoleDesc
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user