用户赋角色、角色配用户添加组织id参数,添加根据角色id查询用户列表接口

This commit is contained in:
yangsb
2024-04-25 11:40:51 +08:00
parent aa7144a090
commit 992f073d12
15 changed files with 73 additions and 22 deletions
@@ -75,9 +75,9 @@ public class PostController {
@GetMapping(value = {"/list-all-simple", "simple-list"}) @GetMapping(value = {"/list-all-simple", "simple-list"})
@Operation(summary = "获取岗位全列表", description = "只包含被开启的岗位,主要用于前端的下拉选项") @Operation(summary = "获取岗位全列表", description = "只包含被开启的岗位,主要用于前端的下拉选项")
public CommonResult<List<PostSimpleRespVO>> getSimplePostList() { public CommonResult<List<PostSimpleRespVO>> getSimplePostList(@RequestParam(value = "organId", required = false) Long organId) {
// 获得岗位列表,只要开启状态的 // 获得岗位列表,只要开启状态的
List<PostDO> list = postService.getPostList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus())); List<PostDO> list = postService.getPostList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()), organId);
// 排序后,返回给前端 // 排序后,返回给前端
list.sort(Comparator.comparing(PostDO::getSort)); list.sort(Comparator.comparing(PostDO::getSort));
return success(BeanUtils.toBean(list, PostSimpleRespVO.class)); return success(BeanUtils.toBean(list, PostSimpleRespVO.class));
@@ -76,8 +76,9 @@ public class PermissionController {
reqVO.getMenuIds().removeIf(menuId -> !CollUtil.contains(menuIds, menuId)); reqVO.getMenuIds().removeIf(menuId -> !CollUtil.contains(menuIds, menuId));
}); });
} }
Long organId = reqVO.getOrganId() == null? OrganContextHolder.getOrganId(): reqVO.getOrganId();
// 执行菜单的分配 // 执行菜单的分配
permissionService.assignRoleMenu(reqVO.getRoleId(), reqVO.getMenuIds()); permissionService.assignRoleMenu(reqVO.getRoleId(), reqVO.getMenuIds(), organId);
return success(true); return success(true);
} }
@@ -101,7 +102,8 @@ public class PermissionController {
@PostMapping("/assign-user-role") @PostMapping("/assign-user-role")
@PreAuthorize("@ss.hasPermission('system:permission:assign-user-role')") @PreAuthorize("@ss.hasPermission('system:permission:assign-user-role')")
public CommonResult<Boolean> assignUserRole(@Validated @RequestBody PermissionAssignUserRoleReqVO reqVO) { public CommonResult<Boolean> assignUserRole(@Validated @RequestBody PermissionAssignUserRoleReqVO reqVO) {
permissionService.assignUserRole(reqVO.getUserId(), reqVO.getRoleIds(),OrganContextHolder.getOrganId()); Long organId = reqVO.getOrganId() == null ? OrganContextHolder.getOrganId() : reqVO.getOrganId();
permissionService.assignUserRole(reqVO.getUserId(), reqVO.getRoleIds(), organId);
return success(true); return success(true);
} }
@@ -124,4 +126,13 @@ public class PermissionController {
return success(true); return success(true);
} }
@Operation(summary = "获得拥有角色的用户列表")
@Parameter(name = "roleId", description = "角色id", required = true)
@GetMapping("/list-role-users")
@PreAuthorize("@ss.hasPermission('system:permission:assign-user-role')")
public CommonResult<Set<Long>> listRoleUsers(@RequestParam("roleId") Long roleId, @RequestParam(value = "organId", required = false) Long organId) {
return success(permissionService.getListRoleUsers(roleId, organId));
}
} }
@@ -18,4 +18,7 @@ public class PermissionAssignRoleMenuReqVO {
@Schema(description = "菜单编号列表", example = "1,3,5") @Schema(description = "菜单编号列表", example = "1,3,5")
private Set<Long> menuIds = Collections.emptySet(); // 兜底 private Set<Long> menuIds = Collections.emptySet(); // 兜底
@Schema(description = "组织id")
private Long organId;
} }
@@ -18,4 +18,7 @@ public class PermissionAssignUserRoleReqVO {
@Schema(description = "角色编号列表", example = "1,3,5") @Schema(description = "角色编号列表", example = "1,3,5")
private Set<Long> roleIds = Collections.emptySet(); // 兜底 private Set<Long> roleIds = Collections.emptySet(); // 兜底
@Schema(description = "组织id")
private Long organId;
} }
@@ -126,8 +126,9 @@ public class UserController {
@GetMapping({"/list-all-simple", "/simple-list"}) @GetMapping({"/list-all-simple", "/simple-list"})
@Operation(summary = "获取用户精简信息列表", description = "只包含被开启的用户,主要用于前端的下拉选项") @Operation(summary = "获取用户精简信息列表", description = "只包含被开启的用户,主要用于前端的下拉选项")
public CommonResult<List<UserSimpleRespVO>> getSimpleUserList() { public CommonResult<List<UserSimpleRespVO>> getSimpleUserList( @RequestParam(value = "organId", required = false) Long organId,
List<AdminUserDO> list = userService.getUserListByStatus(CommonStatusEnum.ENABLE.getStatus()); @RequestParam(value = "deptId", required = false) Long deptId) {
List<AdminUserDO> list = userService.getUserListByStatus(CommonStatusEnum.ENABLE.getStatus(), organId, deptId);
// 拼接数据 // 拼接数据
Map<Long, DeptDO> deptMap = deptService.getDeptMap( Map<Long, DeptDO> deptMap = deptService.getDeptMap(
convertList(list, AdminUserDO::getDeptId)); convertList(list, AdminUserDO::getDeptId));
@@ -17,8 +17,9 @@ import java.util.Objects;
@Mapper @Mapper
public interface PostMapper extends BaseMapperX<PostDO> { public interface PostMapper extends BaseMapperX<PostDO> {
default List<PostDO> selectList(Collection<Long> ids, Collection<Integer> statuses) { default List<PostDO> selectList(Collection<Long> ids, Collection<Integer> statuses, Long organId) {
return selectList(new LambdaQueryWrapperX<PostDO>() return selectList(new LambdaQueryWrapperX<PostDO>()
.eqIfPresent(PostDO::getOrganId, organId)
.inIfPresent(PostDO::getId, ids) .inIfPresent(PostDO::getId, ids)
.inIfPresent(PostDO::getStatus, statuses)); .inIfPresent(PostDO::getStatus, statuses));
} }
@@ -62,8 +62,12 @@ public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {
return selectList(new LambdaQueryWrapperX<AdminUserDO>().like(AdminUserDO::getNickname, nickname)); return selectList(new LambdaQueryWrapperX<AdminUserDO>().like(AdminUserDO::getNickname, nickname));
} }
default List<AdminUserDO> selectListByStatus(Integer status) { default List<AdminUserDO> selectListByStatus(Integer status, Long organId, Long deptId) {
return selectList(AdminUserDO::getStatus, status); return selectList(new LambdaQueryWrapperX<AdminUserDO>()
.eqIfPresent(AdminUserDO::getOrganId, organId)
.eqIfPresent(AdminUserDO::getDeptId, deptId)
.eq(AdminUserDO::getStatus, status));
// return selectList(AdminUserDO::getStatus, status);
} }
default List<AdminUserDO> selectListByDeptIds(Collection<Long> deptIds) { default List<AdminUserDO> selectListByDeptIds(Collection<Long> deptIds) {
@@ -54,7 +54,9 @@ public interface PostService {
* @return 部门列表 * @return 部门列表
*/ */
List<PostDO> getPostList(@Nullable Collection<Long> ids, List<PostDO> getPostList(@Nullable Collection<Long> ids,
@Nullable Collection<Integer> statuses); @Nullable Collection<Integer> statuses,
Long organId
);
/** /**
* 获得岗位分页列表 * 获得岗位分页列表
@@ -118,8 +118,8 @@ public class PostServiceImpl implements PostService {
} }
@Override @Override
public List<PostDO> getPostList(Collection<Long> ids, Collection<Integer> statuses) { public List<PostDO> getPostList(Collection<Long> ids, Collection<Integer> statuses, Long organId) {
return postMapper.selectList(ids, statuses); return postMapper.selectList(ids, statuses, organId);
} }
@Override @Override
@@ -174,7 +174,7 @@ public class OrganServiceImpl implements OrganService {
.setSort(0).setRemark("系统自动生成").setOrganId(organId); .setSort(0).setRemark("系统自动生成").setOrganId(organId);
Long roleId = roleService.createRole(reqVO, RoleTypeEnum.SYSTEM.getType()); Long roleId = roleService.createRole(reqVO, RoleTypeEnum.SYSTEM.getType());
// 分配权限 // 分配权限
permissionService.assignRoleMenu(roleId, tenantPackage.getMenuIds()); permissionService.assignRoleMenu(roleId, tenantPackage.getMenuIds(), organId);
return roleId; return roleId;
} }
@@ -249,14 +249,14 @@ public class OrganServiceImpl implements OrganService {
roles.forEach(role -> { roles.forEach(role -> {
// 如果是组织管理员,重新分配其权限为组织套餐的权限 // 如果是组织管理员,重新分配其权限为组织套餐的权限
if (Objects.equals(role.getCode(), RoleCodeEnum.TENANT_ADMIN.getCode())) { if (Objects.equals(role.getCode(), RoleCodeEnum.TENANT_ADMIN.getCode())) {
permissionService.assignRoleMenu(role.getId(), menuIds); permissionService.assignRoleMenu(role.getId(), menuIds, organId);
log.info("[updateTenantRoleMenu][组织管理员({}/{}) 的权限修改为({})]", role.getId(), role.getOrganId(), menuIds); log.info("[updateTenantRoleMenu][组织管理员({}/{}) 的权限修改为({})]", role.getId(), role.getOrganId(), menuIds);
return; return;
} }
// 如果是其他角色,则去掉超过套餐的权限 // 如果是其他角色,则去掉超过套餐的权限
Set<Long> roleMenuIds = permissionService.getRoleMenuListByRoleId(role.getId()); Set<Long> roleMenuIds = permissionService.getRoleMenuListByRoleId(role.getId());
roleMenuIds = CollUtil.intersectionDistinct(roleMenuIds, menuIds); roleMenuIds = CollUtil.intersectionDistinct(roleMenuIds, menuIds);
permissionService.assignRoleMenu(role.getId(), roleMenuIds); permissionService.assignRoleMenu(role.getId(), roleMenuIds, organId);
log.info("[updateTenantRoleMenu][角色({}/{}) 的权限修改为({})]", role.getId(), role.getOrganId(), roleMenuIds); log.info("[updateTenantRoleMenu][角色({}/{}) 的权限修改为({})]", role.getId(), role.getOrganId(), roleMenuIds);
}); });
}); });
@@ -43,7 +43,7 @@ public interface PermissionService {
* @param roleId 角色编号 * @param roleId 角色编号
* @param menuIds 菜单编号集合 * @param menuIds 菜单编号集合
*/ */
void assignRoleMenu(Long roleId, Set<Long> menuIds); void assignRoleMenu(Long roleId, Set<Long> menuIds, Long organId);
/** /**
* 处理角色删除时,删除关联授权数据 * 处理角色删除时,删除关联授权数据
@@ -156,4 +156,11 @@ public interface PermissionService {
Set<Long> getRoleMenuListByRoleId2(Set<Long> roleIds); Set<Long> getRoleMenuListByRoleId2(Set<Long> roleIds);
void bathAssignUserRole(List<PermissionAssignUserRoleReqVO> listReqVO); void bathAssignUserRole(List<PermissionAssignUserRoleReqVO> listReqVO);
/**
* 获得拥有角色的用户列表
* @param roleId
* @return
*/
Set<Long> getListRoleUsers(Long roleId, Long organId);
} }
@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cf.imes.framework.common.enums.CommonStatusEnum; import com.cf.imes.framework.common.enums.CommonStatusEnum;
import com.cf.imes.framework.common.util.collection.CollectionUtils; import com.cf.imes.framework.common.util.collection.CollectionUtils;
import com.cf.imes.framework.datapermission.core.annotation.DataPermission; import com.cf.imes.framework.datapermission.core.annotation.DataPermission;
@@ -39,6 +40,7 @@ import javax.annotation.Resource;
import java.util.*; import java.util.*;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors;
import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception; import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.framework.common.util.collection.CollectionUtils.convertSet; import static com.cf.imes.framework.common.util.collection.CollectionUtils.convertSet;
@@ -146,7 +148,7 @@ public class PermissionServiceImpl implements PermissionService {
@DSTransactional // 多数据源,使用 @DSTransactional 保证本地事务,以及数据源的切换 @DSTransactional // 多数据源,使用 @DSTransactional 保证本地事务,以及数据源的切换
@CacheEvict(value = RedisKeyConstants.MENU_ROLE_ID_LIST, @CacheEvict(value = RedisKeyConstants.MENU_ROLE_ID_LIST,
allEntries = true) // allEntries 清空所有缓存,主要一次更新涉及到的 menuIds 较多,反倒批量会更快 allEntries = true) // allEntries 清空所有缓存,主要一次更新涉及到的 menuIds 较多,反倒批量会更快
public void assignRoleMenu(Long roleId, Set<Long> menuIds) { public void assignRoleMenu(Long roleId, Set<Long> menuIds, Long organId) {
// 获得角色拥有菜单编号 // 获得角色拥有菜单编号
Set<Long> dbMenuIds = null; Set<Long> dbMenuIds = null;
if (Objects.equals(roleId, ORGAN_ADMIN_ROLE_ID) || Objects.equals(roleId, ORGAN_STAFF_ROLE_ID)) { if (Objects.equals(roleId, ORGAN_ADMIN_ROLE_ID) || Objects.equals(roleId, ORGAN_STAFF_ROLE_ID)) {
@@ -167,6 +169,8 @@ public class PermissionServiceImpl implements PermissionService {
entity.setMenuId(menuId); entity.setMenuId(menuId);
if (Objects.equals(roleId, ORGAN_ADMIN_ROLE_ID) || Objects.equals(roleId, ORGAN_STAFF_ROLE_ID)) { if (Objects.equals(roleId, ORGAN_ADMIN_ROLE_ID) || Objects.equals(roleId, ORGAN_STAFF_ROLE_ID)) {
entity.setOrganId(0L); entity.setOrganId(0L);
}else {
entity.setOrganId(organId);
} }
return entity; return entity;
}; };
@@ -238,12 +242,27 @@ public class PermissionServiceImpl implements PermissionService {
@Override @Override
@DSTransactional @DSTransactional
public void bathAssignUserRole(List<PermissionAssignUserRoleReqVO> listReqVO) { public void bathAssignUserRole(List<PermissionAssignUserRoleReqVO> listReqVO) {
Long organId = OrganContextHolder.getOrganId(); Long contextOrganId = OrganContextHolder.getOrganId();
listReqVO.forEach(e->{ listReqVO.forEach(e->{
Long organId = e.getOrganId() ==null ? contextOrganId : e.getOrganId();
assignUserRole(e.getUserId(), e.getRoleIds(), organId); assignUserRole(e.getUserId(), e.getRoleIds(), organId);
}); });
} }
@Override
public Set<Long> getListRoleUsers(Long roleId, Long organId) {
List<UserRoleDO> userRoleDOS = userRoleMapper.selectList(new LambdaQueryWrapperX<UserRoleDO>()
.eqIfPresent(UserRoleDO::getOrganId, organId)
.eq(UserRoleDO::getRoleId, roleId)
.select(UserRoleDO::getUserId)
);
if(CollUtil.isEmpty(userRoleDOS)) {
return new HashSet<>();
}
return userRoleDOS.stream().map(UserRoleDO::getUserId).collect(Collectors.toSet());
}
@Override @Override
@Cacheable(value = RedisKeyConstants.MENU_ROLE_ID_LIST, key = "#menuId") @Cacheable(value = RedisKeyConstants.MENU_ROLE_ID_LIST, key = "#menuId")
public Set<Long> getMenuRoleIdListByMenuIdFromCache(Long menuId) { public Set<Long> getMenuRoleIdListByMenuIdFromCache(Long menuId) {
@@ -206,7 +206,7 @@ public interface AdminUserService {
* @param status 状态 * @param status 状态
* @return 用户们 * @return 用户们
*/ */
List<AdminUserDO> getUserListByStatus(Integer status); List<AdminUserDO> getUserListByStatus(Integer status, Long organId, Long deptId);
/** /**
* 判断密码是否匹配 * 判断密码是否匹配
@@ -489,8 +489,8 @@ public class AdminUserServiceImpl implements AdminUserService {
} }
@Override @Override
public List<AdminUserDO> getUserListByStatus(Integer status) { public List<AdminUserDO> getUserListByStatus(Integer status, Long organId, Long deptId) {
return userMapper.selectListByStatus(status); return userMapper.selectListByStatus(status, organId, deptId);
} }
@Override @Override
@@ -710,7 +710,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest {
Integer status = CommonStatusEnum.DISABLE.getStatus(); Integer status = CommonStatusEnum.DISABLE.getStatus();
// 调用 // 调用
List<AdminUserDO> result = userService.getUserListByStatus(status); List<AdminUserDO> result = userService.getUserListByStatus(status, 1L);
// 断言 // 断言
assertEquals(1, result.size()); assertEquals(1, result.size());
assertEquals(user, result.get(0)); assertEquals(user, result.get(0));