Merge pull request #10 from hisequel/fix-role-add-duplicated-users
fix:修复角色分配用户时会有重复用户ID的情况。
This commit is contained in:
commit
457a7dd958
|
@ -22,8 +22,8 @@ import net.maku.system.vo.SysUserVO;
|
||||||
import org.springdoc.core.annotations.ParameterObject;
|
import org.springdoc.core.annotations.ParameterObject;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色管理
|
* 角色管理
|
||||||
|
@ -152,10 +152,24 @@ public class SysRoleController {
|
||||||
|
|
||||||
@PostMapping("user/{roleId}")
|
@PostMapping("user/{roleId}")
|
||||||
@Operation(summary = "分配角色给用户列表")
|
@Operation(summary = "分配角色给用户列表")
|
||||||
@OperateLog(type = OperateTypeEnum.DELETE)
|
@OperateLog(type = OperateTypeEnum.UPDATE)
|
||||||
@PreAuthorize("hasAuthority('sys:role:update')")
|
@PreAuthorize("hasAuthority('sys:role:update')")
|
||||||
public Result<String> userSave(@PathVariable("roleId") Long roleId, @RequestBody List<Long> userIdList) {
|
public Result<String> userSave(@PathVariable("roleId") Long roleId, @RequestBody List<Long> userIdList) {
|
||||||
sysUserRoleService.saveUserList(roleId, userIdList);
|
if(userIdList.isEmpty()) {
|
||||||
|
return Result.error("UserId is empty!");
|
||||||
|
}
|
||||||
|
//查询数据库该角色对应的用户列表
|
||||||
|
List<Long> existsUserIdList = sysUserRoleService.getExistsUserIdList(roleId);
|
||||||
|
|
||||||
|
//取出需要新增的用户列表
|
||||||
|
List<Long> addUserIdList = userIdList.stream()
|
||||||
|
.filter(userId -> !existsUserIdList.contains(userId))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
if(addUserIdList.isEmpty()) {
|
||||||
|
return Result.error("UserId existed!");
|
||||||
|
}
|
||||||
|
sysUserRoleService.saveUserList(roleId, addUserIdList);
|
||||||
|
|
||||||
return Result.ok();
|
return Result.ok();
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,4 +23,12 @@ public interface SysUserRoleDao extends BaseDao<SysUserRoleEntity> {
|
||||||
* @return 返回角色ID列表
|
* @return 返回角色ID列表
|
||||||
*/
|
*/
|
||||||
List<Long> getRoleIdList(@Param("userId") Long userId);
|
List<Long> getRoleIdList(@Param("userId") Long userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据角色ID查询对应的用户ID列表
|
||||||
|
* @param roleId 角色ID
|
||||||
|
*
|
||||||
|
* @return 返回用户ID列表
|
||||||
|
*/
|
||||||
|
List<Long> getExistsUserIdList(@Param("roleId") Long roleId);
|
||||||
}
|
}
|
|
@ -51,4 +51,10 @@ public interface SysUserRoleService extends BaseService<SysUserRoleEntity> {
|
||||||
* @param userId 用户ID
|
* @param userId 用户ID
|
||||||
*/
|
*/
|
||||||
List<Long> getRoleIdList(Long userId);
|
List<Long> getRoleIdList(Long userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据角色ID查询对应的用户ID列表
|
||||||
|
* @param roleId 角色ID
|
||||||
|
*/
|
||||||
|
List<Long> getExistsUserIdList(Long roleId);
|
||||||
}
|
}
|
|
@ -91,4 +91,9 @@ public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRoleDao, SysU
|
||||||
public List<Long> getRoleIdList(Long userId) {
|
public List<Long> getRoleIdList(Long userId) {
|
||||||
return baseMapper.getRoleIdList(userId);
|
return baseMapper.getRoleIdList(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Long> getExistsUserIdList(Long roleId) {
|
||||||
|
return baseMapper.getExistsUserIdList(roleId);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -7,4 +7,7 @@
|
||||||
select role_id from sys_user_role where user_id = #{userId} and deleted = 0
|
select role_id from sys_user_role where user_id = #{userId} and deleted = 0
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="getExistsUserIdList" resultType="long">
|
||||||
|
select user_id from sys_user_role where role_id = #{roleId} and deleted = 0
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
Loading…
Reference in New Issue
Block a user