fix(角色提升):

修复角色无法提升问题
This commit is contained in:
Ftz123456 2024-02-28 07:43:46 +08:00
parent cf7d44f6dd
commit ea48169098
4 changed files with 21 additions and 5 deletions

View File

@ -59,9 +59,9 @@ public class RoleController {
@PatchMapping("{id}")
@PreAuthorize("hasAuthority('ROLE_UPDATE')")
public Boolean update(@RequestBody RoleDto dto, @PathVariable("id") Long id) {
Role role = roleMapper.toEntity(dto);
role.setId(id);
return service.updateById(role);
// Role role = roleMapper.toEntity(dto);
// role.setId(id);
return service.updateRole(dto, id);
}
/**

View File

@ -11,7 +11,6 @@ import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import java.util.Set;
/**
* 用户更新Dto
@ -63,7 +62,7 @@ public class UserUpdateDto {
* 用户身份集合
*/
@NotEmpty(message = "角色不能为空")
public Set<Long> roleIds;
public Long roleId;
public String remark;

View File

@ -24,4 +24,6 @@ public interface RoleService extends IService<Role> {
Boolean toggle(Long id);
Boolean delete(Long id);
Boolean updateRole(RoleDto dto, Long id);
}

View File

@ -16,6 +16,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@ -68,6 +69,20 @@ public class RoleServiceImpl extends ServiceImpl<RoleRepository, Role> implement
return removeById(id);
}
@Override
public Boolean updateRole(RoleDto dto, Long id) {
Role role =mapper.toEntity(dto);
role.setId(id);
if (dto.getAuthorities() != null && !dto.getAuthorities().isEmpty()) {
roleAuthService.remove(new LambdaQueryWrapper<RoleAuthority>().eq(RoleAuthority::getRoleId, id));
Set<Authority> authorities = new HashSet<>(dto.getAuthorities());
roleAuthService.saveBatch(authorities.stream().map(authority -> new RoleAuthority(id, authority.getAuthority())).collect(Collectors.toList()));
}
return updateById(role);
}
@Override
public Boolean edit(RoleDto dto, Long id) {
boolean existsByName = count(new LambdaQueryWrapper<Role>().ne(Role::getId, id).eq(Role::getName, dto.getName())) > 0;