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, "未授权访问")
|
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{
|
role := &model.Role{
|
||||||
RoleName: req.RoleName,
|
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, "获取角色失败")
|
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
|
role.RoleName = *req.RoleName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更新其他字段
|
||||||
if req.RoleDesc != nil {
|
if req.RoleDesc != nil {
|
||||||
role.RoleDesc = *req.RoleDesc
|
role.RoleDesc = *req.RoleDesc
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,20 @@ func (s *RoleStore) GetByName(ctx context.Context, name string) (*model.Role, er
|
|||||||
return &role, nil
|
return &role, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExistsByName 检查角色名是否已存在
|
||||||
|
// excludeID: 排除的角色 ID(用于更新时排除自身),传 0 表示不排除
|
||||||
|
func (s *RoleStore) ExistsByName(ctx context.Context, name string, excludeID uint) (bool, error) {
|
||||||
|
var count int64
|
||||||
|
query := s.db.WithContext(ctx).Model(&model.Role{}).Where("role_name = ?", name)
|
||||||
|
if excludeID > 0 {
|
||||||
|
query = query.Where("id != ?", excludeID)
|
||||||
|
}
|
||||||
|
if err := query.Count(&count).Error; err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return count > 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Update 更新角色
|
// Update 更新角色
|
||||||
func (s *RoleStore) Update(ctx context.Context, role *model.Role) error {
|
func (s *RoleStore) Update(ctx context.Context, role *model.Role) error {
|
||||||
return s.db.WithContext(ctx).Save(role).Error
|
return s.db.WithContext(ctx).Save(role).Error
|
||||||
|
|||||||
Reference in New Issue
Block a user