一、根据用户获取角色数据
1、UserController
@ApiOperation(value = "根据用户获取角色数据")
@GetMapping("/toAssign/{userId}")
public R toAssign(@PathVariable String userId) {
Map<String, Object> roleMap = roleService.findRoleByUserId(userId);
return R.ok().data(roleMap);
}
2、RoleServiceImpl
//根据用户获取角色数据
@Override
public Map<String, Object> findRoleByUserId(String userId) {
//查询所有的角色
List<Role> allRolesList =baseMapper.selectList(null);
Map<String, Object> roleMap = new HashMap<>();
roleMap.put("allRolesList", allRolesList);
//根据用户id,查询用户拥有的角色id
LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserRole::getUserId,userId);
List<UserRole> existUserRoleList = userRoleService.list(wrapper);
// List<UserRole> existUserRoleList = userRoleService.list(new QueryWrapper<UserRole>().eq("user_id", userId).select("role_id"));
if(CollectionUtils.isEmpty(existUserRoleList)){
roleMap.put("assignRoles", existUserRoleList);
return roleMap;
}
List<String> existRoleList = existUserRoleList.stream().map(c -> c.getRoleId()).collect(Collectors.toList());
//根据角色id查询对应的角色对象
List<Role> assignRoles = (List<Role>)roleService.listByIds(existRoleList);
//对角色进行分类
// List<Role> assignRoles = new ArrayList<Role>();
// for (Role role : allRolesList) {
// //已分配
// if(existRoleList.contains(role.getId())) {
// assignRoles.add(role);
// }
// }
roleMap.put("assignRoles", assignRoles);
return roleMap;
}
二、根据用户分配角色
1、UserController
@ApiOperation(value = "根据用户分配角色")
@PostMapping("/doAssign")
public R doAssign(@RequestParam String userId,@RequestParam String[] roleId) {
roleService.saveUserRoleRealtionShip(userId,roleId);
return R.ok();
}
2、RoleServiceImpl
//根据用户分配角色
@Override
public void saveUserRoleRealtionShip(String userId, String[] roleIds) {
// 删除当前用户下所有的角色
LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(UserRole::getUserId,userId);
userRoleService.remove(wrapper);
// 分配角色
List<UserRole> userRoles = new ArrayList<>();
if(StringUtils.isEmpty(roleIds)){
return;
}
for(String roleId : roleIds){
UserRole userRole = new UserRole();
userRole.setUserId(userId);
userRole.setRoleId(roleId);
userRoles.add(userRole);
}
userRoleService.saveBatch(userRoles);
// userRoleService.remove(new QueryWrapper<UserRole>().eq("user_id", userId));
// List<UserRole> userRoleList = new ArrayList<>();
// for(String roleId : roleIds) {
// if(StringUtils.isEmpty(roleId)) continue;
// UserRole userRole = new UserRole();
// userRole.setUserId(userId);
// userRole.setRoleId(roleId);
//
// userRoleList.add(userRole);
// }
// userRoleService.saveBatch(userRoleList);
}
三、根据用户id拥有的角色id
1、RoleService
public interface RoleService extends IService<Role> {
// 根据用户id拥有的角色id
List<Role> selectRoleByUserId(String id);
}
2、RoleServiceImpl
@Override
public List<Role> selectRoleByUserId(String id) {
//根据用户id拥有的角色id
List<UserRole> userRoleList = userRoleService.list(new QueryWrapper<UserRole>().eq("user_id", id).select("role_id"));
List<String> roleIdList = userRoleList.stream().map(item -> item.getRoleId()).collect(Collectors.toList());
List<Role> roleList = new ArrayList<>();
if(roleIdList.size() > 0) {
roleList = baseMapper.selectBatchIds(roleIdList);
}
return roleList;
}
3、IndexServiceImpl
/**
* 根据用户名获取用户登录信息
*
* @param username
* @return
*/
public Map<String, Object> getUserInfo(String username) {
Map<String, Object> result = new HashMap<>();
User user = userService.selectByUsername(username);
if (null == user) {
//throw new GuliException(ResultCodeEnum.FETCH_USERINFO_ERROR);
}
//根据用户id获取角色
List<Role> roleList = roleService.selectRoleByUserId(user.getId());
List<String> roleNameList = roleList.stream().map(item -> item.getRoleName()).collect(Collectors.toList());
if(roleNameList.size() == 0) {
//前端框架必须返回一个角色,否则报错,如果没有角色,返回一个空角色
roleNameList.add("");
}
//根据用户id获取操作权限值
List<String> permissionValueList = permissionService.selectPermissionValueByUserId(user.getId());
redisTemplate.opsForValue().set(username, permissionValueList);
result.put("name", user.getUsername());
result.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
result.put("roles", roleNameList);
result.put("permissionValueList", permissionValueList);
return result;
}