refactor(role): 优化角色服务中的权限查询和数据操作逻辑
This commit is contained in:
-4
@@ -26,17 +26,13 @@ import cn.odboy.system.service.SystemDictDetailService;
|
|||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
|||||||
+1
-1
@@ -52,7 +52,7 @@ public class SystemRoleController {
|
|||||||
@PostMapping(value = "/getRoleById")
|
@PostMapping(value = "/getRoleById")
|
||||||
@PreAuthorize("@el.check('roles:list')")
|
@PreAuthorize("@el.check('roles:list')")
|
||||||
public ResponseEntity<SystemRoleVo> getRoleById(@RequestBody SystemRoleTb args) {
|
public ResponseEntity<SystemRoleVo> getRoleById(@RequestBody SystemRoleTb args) {
|
||||||
return ResponseEntity.ok(systemRoleService.getRoleById(args.getId()));
|
return ResponseEntity.ok(systemRoleService.getRoleVoById(args.getId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiOperation("导出角色数据")
|
@ApiOperation("导出角色数据")
|
||||||
|
|||||||
+3
@@ -17,7 +17,9 @@ package cn.odboy.system.dal.mysql;
|
|||||||
|
|
||||||
import cn.odboy.system.dal.dataobject.SystemRoleMenuTb;
|
import cn.odboy.system.dal.dataobject.SystemRoleMenuTb;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import java.util.List;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 角色菜单关联 Mapper
|
* 角色菜单关联 Mapper
|
||||||
@@ -27,4 +29,5 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
@Mapper
|
@Mapper
|
||||||
public interface SystemRoleMenuMapper extends BaseMapper<SystemRoleMenuTb> {
|
public interface SystemRoleMenuMapper extends BaseMapper<SystemRoleMenuTb> {
|
||||||
|
|
||||||
|
List<String> listMenuPermissionByRoleIds(@Param("roleIds") List<Long> roleIds);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
@@ -27,5 +27,8 @@ import org.apache.ibatis.annotations.Param;
|
|||||||
public interface SystemUserRoleMapper extends BaseMapper<SystemUserRoleTb> {
|
public interface SystemUserRoleMapper extends BaseMapper<SystemUserRoleTb> {
|
||||||
|
|
||||||
Set<SystemRoleTb> listUserRoleByUserId(@Param("userId") Long userId);
|
Set<SystemRoleTb> listUserRoleByUserId(@Param("userId") Long userId);
|
||||||
|
|
||||||
List<Integer> listUserRoleLevelByUserId(@Param("userId") Long userId);
|
List<Integer> listUserRoleLevelByUserId(@Param("userId") Long userId);
|
||||||
|
|
||||||
|
List<Long> listUserRoleIdByUserId(@Param("userId") Long userId);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -129,7 +129,7 @@ public class SystemDeptService {
|
|||||||
/**
|
/**
|
||||||
* 更新父节点中子节点数目 -> TestPassed
|
* 更新父节点中子节点数目 -> TestPassed
|
||||||
*
|
*
|
||||||
* @param deptId /
|
* @param deptId 部门id
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void updateDeptSubCnt(Long deptId) {
|
public void updateDeptSubCnt(Long deptId) {
|
||||||
@@ -179,7 +179,7 @@ public class SystemDeptService {
|
|||||||
/**
|
/**
|
||||||
* 查询所有数据
|
* 查询所有数据
|
||||||
*
|
*
|
||||||
* @param args 条件
|
* @param args /
|
||||||
* @param isQuery /
|
* @param isQuery /
|
||||||
* @return /
|
* @return /
|
||||||
* @throws Exception /
|
* @throws Exception /
|
||||||
|
|||||||
+27
@@ -16,6 +16,7 @@
|
|||||||
package cn.odboy.system.service;
|
package cn.odboy.system.service;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.odboy.system.dal.dataobject.SystemMenuTb;
|
import cn.odboy.system.dal.dataobject.SystemMenuTb;
|
||||||
import cn.odboy.system.dal.dataobject.SystemRoleMenuTb;
|
import cn.odboy.system.dal.dataobject.SystemRoleMenuTb;
|
||||||
import cn.odboy.system.dal.model.response.SystemMenuVo;
|
import cn.odboy.system.dal.model.response.SystemMenuVo;
|
||||||
@@ -40,6 +41,11 @@ public class SystemRoleMenuService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private SystemMenuMapper systemMenuMapper;
|
private SystemMenuMapper systemMenuMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除角色菜单关联 -> TestPassed
|
||||||
|
*
|
||||||
|
* @param roleId 角色id
|
||||||
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void deleteRoleMenuByRoleId(Long roleId) {
|
public void deleteRoleMenuByRoleId(Long roleId) {
|
||||||
LambdaQueryWrapper<SystemRoleMenuTb> wrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<SystemRoleMenuTb> wrapper = new LambdaQueryWrapper<>();
|
||||||
@@ -47,6 +53,12 @@ public class SystemRoleMenuService {
|
|||||||
systemRoleMenuMapper.delete(wrapper);
|
systemRoleMenuMapper.delete(wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量绑定角色菜单关联 -> TestPassed
|
||||||
|
*
|
||||||
|
* @param menus 菜单集合
|
||||||
|
* @param roleId 角色id
|
||||||
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void batchInsertRoleMenu(Set<SystemMenuVo> menus, Long roleId) {
|
public void batchInsertRoleMenu(Set<SystemMenuVo> menus, Long roleId) {
|
||||||
if (CollUtil.isNotEmpty(menus)) {
|
if (CollUtil.isNotEmpty(menus)) {
|
||||||
@@ -61,6 +73,11 @@ public class SystemRoleMenuService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量删除角色菜单关联 -> TestPassed
|
||||||
|
*
|
||||||
|
* @param roleIds 角色id集合
|
||||||
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void batchDeleteRoleMenu(Set<Long> roleIds) {
|
public void batchDeleteRoleMenu(Set<Long> roleIds) {
|
||||||
if (CollUtil.isNotEmpty(roleIds)) {
|
if (CollUtil.isNotEmpty(roleIds)) {
|
||||||
@@ -92,6 +109,11 @@ public class SystemRoleMenuService {
|
|||||||
return new LinkedHashSet<>(systemMenuVos);
|
return new LinkedHashSet<>(systemMenuVos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量删除菜单角色关联 -> TestPassed
|
||||||
|
*
|
||||||
|
* @param menuIds 菜单id集合
|
||||||
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void deleteRoleMenuByMenuIds(List<Long> menuIds) {
|
public void deleteRoleMenuByMenuIds(List<Long> menuIds) {
|
||||||
if (CollUtil.isEmpty(menuIds)) {
|
if (CollUtil.isEmpty(menuIds)) {
|
||||||
@@ -101,4 +123,9 @@ public class SystemRoleMenuService {
|
|||||||
wrapper.in(SystemRoleMenuTb::getMenuId, menuIds);
|
wrapper.in(SystemRoleMenuTb::getMenuId, menuIds);
|
||||||
systemRoleMenuMapper.delete(wrapper);
|
systemRoleMenuMapper.delete(wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> listMenuPermissionByRoleIds(List<Long> roleIds) {
|
||||||
|
List<String> permissions = systemRoleMenuMapper.listMenuPermissionByRoleIds(roleIds);
|
||||||
|
return permissions.stream().filter(StrUtil::isNotBlank).collect(Collectors.toList());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+45
-57
@@ -32,9 +32,9 @@ import cn.odboy.system.dal.mysql.SystemRoleMapper;
|
|||||||
import cn.odboy.system.framework.permission.core.KitSecurityHelper;
|
import cn.odboy.system.framework.permission.core.KitSecurityHelper;
|
||||||
import cn.odboy.util.KitBeanUtil;
|
import cn.odboy.util.KitBeanUtil;
|
||||||
import cn.odboy.util.KitPageUtil;
|
import cn.odboy.util.KitPageUtil;
|
||||||
import cn.odboy.util.KitValidUtil;
|
|
||||||
import cn.odboy.util.xlsx.KitExcelExporter;
|
import cn.odboy.util.xlsx.KitExcelExporter;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -67,7 +67,7 @@ public class SystemRoleService {
|
|||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void saveRole(SystemCreateRoleArgs args) {
|
public void saveRole(SystemCreateRoleArgs args) {
|
||||||
checkRoleLevels(args.getLevel());
|
checkRoleLevels(args.getLevel());
|
||||||
if (this.countRoleByName(args.getName()) > 0) {
|
if (this.existRoleWithName(args.getName())) {
|
||||||
throw new BadRequestException("角色名称已存在");
|
throw new BadRequestException("角色名称已存在");
|
||||||
}
|
}
|
||||||
SystemRoleTb roleTb = KitBeanUtil.copyToClass(args, SystemRoleTb.class);
|
SystemRoleTb roleTb = KitBeanUtil.copyToClass(args, SystemRoleTb.class);
|
||||||
@@ -78,16 +78,17 @@ public class SystemRoleService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private SystemRoleTb getRoleByName(String name) {
|
private boolean existRoleWithName(String name) {
|
||||||
LambdaQueryWrapper<SystemRoleTb> wrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<SystemRoleTb> wrapper = new LambdaQueryWrapper<>();
|
||||||
wrapper.eq(SystemRoleTb::getName, name);
|
wrapper.eq(SystemRoleTb::getName, name);
|
||||||
return systemRoleMapper.selectOne(wrapper);
|
return systemRoleMapper.exists(wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
private long countRoleByName(String name) {
|
private boolean existRoleWithNameNeSelf(String name, Long roleId) {
|
||||||
LambdaQueryWrapper<SystemRoleTb> wrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<SystemRoleTb> wrapper = new LambdaQueryWrapper<>();
|
||||||
wrapper.eq(SystemRoleTb::getName, name);
|
wrapper.eq(SystemRoleTb::getName, name);
|
||||||
return systemRoleMapper.selectCount(wrapper);
|
wrapper.eq(SystemRoleTb::getId, roleId);
|
||||||
|
return systemRoleMapper.exists(wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -98,9 +99,8 @@ public class SystemRoleService {
|
|||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void updateRoleById(SystemRoleVo args) {
|
public void updateRoleById(SystemRoleVo args) {
|
||||||
checkRoleLevels(args.getLevel());
|
checkRoleLevels(args.getLevel());
|
||||||
SystemRoleTb role = this.getRoleTbById(args.getId());
|
SystemRoleTb role = systemRoleMapper.selectById(args.getId());
|
||||||
SystemRoleTb role1 = this.getRoleByName(args.getName());
|
if (this.existRoleWithNameNeSelf(args.getName(), role.getId())) {
|
||||||
if (role1 != null && !role1.getId().equals(role.getId())) {
|
|
||||||
throw new BadRequestException("角色名称已存在");
|
throw new BadRequestException("角色名称已存在");
|
||||||
}
|
}
|
||||||
role.setName(args.getName());
|
role.setName(args.getName());
|
||||||
@@ -108,7 +108,7 @@ public class SystemRoleService {
|
|||||||
role.setDataScope(args.getDataScope());
|
role.setDataScope(args.getDataScope());
|
||||||
role.setLevel(args.getLevel());
|
role.setLevel(args.getLevel());
|
||||||
// 更新
|
// 更新
|
||||||
systemRoleMapper.insertOrUpdate(role);
|
systemRoleMapper.updateById(role);
|
||||||
// 删除关联部门数据
|
// 删除关联部门数据
|
||||||
systemRoleDeptService.batchDeleteRoleDept(Collections.singleton(args.getId()));
|
systemRoleDeptService.batchDeleteRoleDept(Collections.singleton(args.getId()));
|
||||||
// 判断是否有部门数据, 若有, 则需更新关联
|
// 判断是否有部门数据, 若有, 则需更新关联
|
||||||
@@ -117,10 +117,6 @@ public class SystemRoleService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private SystemRoleTb getRoleTbById(Long id) {
|
|
||||||
return systemRoleMapper.selectById(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改绑定的菜单 -> TestPassed
|
* 修改绑定的菜单 -> TestPassed
|
||||||
*
|
*
|
||||||
@@ -132,7 +128,6 @@ public class SystemRoleService {
|
|||||||
checkRoleLevels(role.getLevel());
|
checkRoleLevels(role.getLevel());
|
||||||
// 更新菜单
|
// 更新菜单
|
||||||
systemRoleMenuService.deleteRoleMenuByRoleId(role.getId());
|
systemRoleMenuService.deleteRoleMenuByRoleId(role.getId());
|
||||||
// 判断是否为空
|
|
||||||
Set<SystemMenuVo> menus = args.getMenus();
|
Set<SystemMenuVo> menus = args.getMenus();
|
||||||
if (CollUtil.isNotEmpty(menus)) {
|
if (CollUtil.isNotEmpty(menus)) {
|
||||||
systemRoleMenuService.batchInsertRoleMenu(menus, role.getId());
|
systemRoleMenuService.batchInsertRoleMenu(menus, role.getId());
|
||||||
@@ -146,8 +141,7 @@ public class SystemRoleService {
|
|||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void deleteRoleByIds(Set<Long> ids) {
|
public void deleteRoleByIds(Set<Long> ids) {
|
||||||
List<Integer> roleLevels = systemRoleMapper.selectByIds(ids).stream().map(SystemRoleTb::getLevel).distinct()
|
List<Integer> roleLevels = systemRoleMapper.listRoleLevelByRoleIds(new ArrayList<>(ids));
|
||||||
.collect(Collectors.toList());
|
|
||||||
for (Integer roleLevel : roleLevels) {
|
for (Integer roleLevel : roleLevels) {
|
||||||
checkRoleLevels(roleLevel);
|
checkRoleLevels(roleLevel);
|
||||||
}
|
}
|
||||||
@@ -165,7 +159,7 @@ public class SystemRoleService {
|
|||||||
*/
|
*/
|
||||||
public List<SystemRoleTb> listAllRole() {
|
public List<SystemRoleTb> listAllRole() {
|
||||||
LambdaQueryWrapper<SystemRoleTb> wrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<SystemRoleTb> wrapper = new LambdaQueryWrapper<>();
|
||||||
wrapper.orderByAsc(SystemRoleTb::getLevel);
|
wrapper.orderByDesc(SystemRoleTb::getCreateTime);
|
||||||
return systemRoleMapper.selectList(wrapper);
|
return systemRoleMapper.selectList(wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,24 +170,21 @@ public class SystemRoleService {
|
|||||||
* @param rolePage 分页参数
|
* @param rolePage 分页参数
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public List<SystemRoleVo> queryRoleByArgs(SystemQueryRoleArgs args, Page<SystemRoleTb> rolePage) {
|
public List<SystemRoleTb> queryRoleByArgs(SystemQueryRoleArgs args, Page<SystemRoleTb> rolePage) {
|
||||||
KitValidUtil.notNull(args);
|
|
||||||
// 查询角色基本信息
|
|
||||||
LambdaQueryWrapper<SystemRoleTb> wrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<SystemRoleTb> wrapper = new LambdaQueryWrapper<>();
|
||||||
wrapper.and(StrUtil.isNotBlank(args.getBlurry()), c -> c.like(SystemRoleTb::getName, args.getBlurry()).or()
|
this.injectQueryParams(args, wrapper);
|
||||||
.like(SystemRoleTb::getDescription, args.getBlurry()));
|
return systemRoleMapper.selectList(wrapper);
|
||||||
if (CollUtil.isNotEmpty(args.getCreateTime()) && args.getCreateTime().size() >= 2) {
|
}
|
||||||
wrapper.between(SystemRoleTb::getCreateTime, args.getCreateTime().get(0), args.getCreateTime().get(1));
|
|
||||||
|
private void injectQueryParams(SystemQueryRoleArgs args, LambdaQueryWrapper<SystemRoleTb> wrapper) {
|
||||||
|
if (args != null) {
|
||||||
|
wrapper.and(StrUtil.isNotBlank(args.getBlurry()), c -> c.like(SystemRoleTb::getName, args.getBlurry()).or()
|
||||||
|
.like(SystemRoleTb::getDescription, args.getBlurry()));
|
||||||
|
if (CollUtil.isNotEmpty(args.getCreateTime()) && args.getCreateTime().size() >= 2) {
|
||||||
|
wrapper.between(SystemRoleTb::getCreateTime, args.getCreateTime().get(0), args.getCreateTime().get(1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
wrapper.orderByAsc(SystemRoleTb::getLevel);
|
wrapper.orderByAsc(SystemRoleTb::getLevel);
|
||||||
List<SystemRoleTb> roles;
|
|
||||||
if (rolePage == null) {
|
|
||||||
roles = systemRoleMapper.selectList(wrapper);
|
|
||||||
} else {
|
|
||||||
Page<SystemRoleTb> page = systemRoleMapper.selectPage(rolePage, wrapper);
|
|
||||||
roles = page.getRecords();
|
|
||||||
}
|
|
||||||
return roles.stream().map(systemUserRoleService::convertToRoleVo).collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -204,13 +195,14 @@ public class SystemRoleService {
|
|||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public KitPageResult<SystemRoleVo> searchRoleByArgs(SystemQueryRoleArgs args, Page<SystemRoleTb> page) {
|
public KitPageResult<SystemRoleVo> searchRoleByArgs(SystemQueryRoleArgs args, Page<SystemRoleTb> page) {
|
||||||
List<SystemRoleVo> roles = this.queryRoleByArgs(args, page);
|
LambdaQueryWrapper<SystemRoleTb> wrapper = new LambdaQueryWrapper<>();
|
||||||
Long total = this.countRoleByArgs(args);
|
this.injectQueryParams(args, wrapper);
|
||||||
return KitPageUtil.toPage(roles, total);
|
IPage<SystemRoleVo> data = systemRoleMapper.selectPage(page, wrapper).convert(systemUserRoleService::convertToRoleVo);
|
||||||
|
return KitPageUtil.toPage(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询用户权限信息 -> TestPassed
|
* 查询用户权限信息 -> TestPassed?
|
||||||
*
|
*
|
||||||
* @param user 用户信息
|
* @param user 用户信息
|
||||||
* @return 权限信息
|
* @return 权限信息
|
||||||
@@ -222,9 +214,17 @@ public class SystemRoleService {
|
|||||||
permissions.add("admin");
|
permissions.add("admin");
|
||||||
return permissions.stream().map(SystemRoleCodeVo::new).collect(Collectors.toList());
|
return permissions.stream().map(SystemRoleCodeVo::new).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
List<SystemRoleVo> roles = systemUserRoleService.listRoleVoByUsersId(user.getId());
|
// List<SystemRoleVo> roles = systemUserRoleService.listRoleVoByUsersId(user.getId());
|
||||||
permissions = roles.stream().flatMap(role -> role.getMenus().stream()).map(SystemMenuVo::getPermission)
|
// permissions = roles.stream().flatMap(role -> role.getMenus().stream()).map(SystemMenuVo::getPermission)
|
||||||
.filter(StrUtil::isNotBlank).collect(Collectors.toSet());
|
// .filter(StrUtil::isNotBlank).collect(Collectors.toSet());
|
||||||
|
// return permissions.stream().map(SystemRoleCodeVo::new).collect(Collectors.toList());
|
||||||
|
List<Long> roleIds = systemUserRoleService.listUserRoleIdByUserId(user.getId());
|
||||||
|
// 查询角色权限
|
||||||
|
if (CollUtil.isNotEmpty(roleIds)) {
|
||||||
|
return systemRoleMenuService.listMenuPermissionByRoleIds(roleIds).stream().map(SystemRoleCodeVo::new).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
// 没有包含任何角色,赋予默认权限
|
||||||
|
permissions.add("IsAJoker");
|
||||||
return permissions.stream().map(SystemRoleCodeVo::new).collect(Collectors.toList());
|
return permissions.stream().map(SystemRoleCodeVo::new).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,23 +234,12 @@ public class SystemRoleService {
|
|||||||
* @param ids /
|
* @param ids /
|
||||||
*/
|
*/
|
||||||
public void verifyBindRelationByIds(Set<Long> ids) {
|
public void verifyBindRelationByIds(Set<Long> ids) {
|
||||||
if (systemUserRoleService.countUserByRoleIds(ids) > 0) {
|
if (systemUserRoleService.existUserWithRoleIds(ids)) {
|
||||||
throw new BadRequestException("所选角色存在用户关联, 请解除关联再试!");
|
throw new BadRequestException("所选角色存在用户关联, 请解除关联再试!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long countRoleByArgs(SystemQueryRoleArgs args) {
|
public SystemRoleVo getRoleVoById(Long id) {
|
||||||
KitValidUtil.notNull(args);
|
|
||||||
LambdaQueryWrapper<SystemRoleTb> wrapper = new LambdaQueryWrapper<>();
|
|
||||||
wrapper.and(StrUtil.isNotBlank(args.getBlurry()), c -> c.like(SystemRoleTb::getName, args.getBlurry()).or()
|
|
||||||
.like(SystemRoleTb::getDescription, args.getBlurry()));
|
|
||||||
if (CollUtil.isNotEmpty(args.getCreateTime()) && args.getCreateTime().size() >= 2) {
|
|
||||||
wrapper.between(SystemRoleTb::getCreateTime, args.getCreateTime().get(0), args.getCreateTime().get(1));
|
|
||||||
}
|
|
||||||
return systemRoleMapper.selectCount(wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
public SystemRoleVo getRoleById(Long id) {
|
|
||||||
SystemRoleTb roleTb = systemRoleMapper.selectById(id);
|
SystemRoleTb roleTb = systemRoleMapper.selectById(id);
|
||||||
if (roleTb == null) {
|
if (roleTb == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -268,8 +257,7 @@ public class SystemRoleService {
|
|||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
private int checkRoleLevels(Integer level) {
|
private int checkRoleLevels(Integer level) {
|
||||||
List<Integer> levels = systemUserRoleService.listRoleVoByUsersId(KitSecurityHelper.getCurrentUserId()).stream()
|
List<Integer> levels = systemUserRoleService.listUserRoleLevelByUserId(KitSecurityHelper.getCurrentUserId());
|
||||||
.map(SystemRoleVo::getLevel).collect(Collectors.toList());
|
|
||||||
int min = Collections.min(levels);
|
int min = Collections.min(levels);
|
||||||
if (level != null) {
|
if (level != null) {
|
||||||
if (level < min) {
|
if (level < min) {
|
||||||
@@ -280,9 +268,9 @@ public class SystemRoleService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void exportRoleXlsx(HttpServletResponse response, SystemQueryRoleArgs args) {
|
public void exportRoleXlsx(HttpServletResponse response, SystemQueryRoleArgs args) {
|
||||||
List<SystemRoleVo> systemRoleVos = this.queryRoleByArgs(args, null);
|
List<SystemRoleTb> systemRoleVos = this.queryRoleByArgs(args, null);
|
||||||
List<SystemRoleExportRowVo> rowVos = new ArrayList<>();
|
List<SystemRoleExportRowVo> rowVos = new ArrayList<>();
|
||||||
for (SystemRoleVo dataObject : systemRoleVos) {
|
for (SystemRoleTb dataObject : systemRoleVos) {
|
||||||
SystemRoleExportRowVo rowVo = new SystemRoleExportRowVo();
|
SystemRoleExportRowVo rowVo = new SystemRoleExportRowVo();
|
||||||
rowVo.setName(dataObject.getName());
|
rowVo.setName(dataObject.getName());
|
||||||
rowVo.setLevel(dataObject.getLevel());
|
rowVo.setLevel(dataObject.getLevel());
|
||||||
|
|||||||
+26
-10
@@ -148,15 +148,6 @@ public class SystemUserRoleService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long countUserByRoleIds(Set<Long> ids) {
|
|
||||||
if (CollUtil.isEmpty(ids)) {
|
|
||||||
return 0L;
|
|
||||||
}
|
|
||||||
LambdaQueryWrapper<SystemUserRoleTb> wrapper = new LambdaQueryWrapper<>();
|
|
||||||
wrapper.in(SystemUserRoleTb::getRoleId, ids);
|
|
||||||
return systemUserRoleMapper.selectCount(wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据用户id查询角色 -> TestPassed
|
* 根据用户id查询角色 -> TestPassed
|
||||||
*
|
*
|
||||||
@@ -168,7 +159,7 @@ public class SystemUserRoleService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据用户id查询角色级别 -> TestPassed
|
* 根据用户id查询角色级别集合 -> TestPassed
|
||||||
*
|
*
|
||||||
* @param userId /
|
* @param userId /
|
||||||
* @return /
|
* @return /
|
||||||
@@ -176,4 +167,29 @@ public class SystemUserRoleService {
|
|||||||
public List<Integer> listUserRoleLevelByUserId(Long userId) {
|
public List<Integer> listUserRoleLevelByUserId(Long userId) {
|
||||||
return systemUserRoleMapper.listUserRoleLevelByUserId(userId);
|
return systemUserRoleMapper.listUserRoleLevelByUserId(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户id查询角色id集合 -> TestPassed
|
||||||
|
*
|
||||||
|
* @param userId /
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public List<Long> listUserRoleIdByUserId(Long userId) {
|
||||||
|
return systemUserRoleMapper.listUserRoleIdByUserId(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据角色id集合判断是否存在关联用户 -> TestPassed
|
||||||
|
*
|
||||||
|
* @param ids 角色id集合
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public boolean existUserWithRoleIds(Set<Long> ids) {
|
||||||
|
if (CollUtil.isEmpty(ids)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
LambdaQueryWrapper<SystemUserRoleTb> wrapper = new LambdaQueryWrapper<>();
|
||||||
|
wrapper.in(SystemUserRoleTb::getRoleId, ids);
|
||||||
|
return systemUserRoleMapper.exists(wrapper);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-25
@@ -1,29 +1,29 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
<mapper namespace="cn.odboy.system.dal.mysql.SystemDictDetailMapper">
|
<mapper namespace="cn.odboy.system.dal.mysql.SystemDictDetailMapper">
|
||||||
<select id="selectPageByArgs" resultType="cn.odboy.system.dal.model.response.SystemDictDetailVo">
|
<select id="selectPageByArgs" resultType="cn.odboy.system.dal.model.response.SystemDictDetailVo">
|
||||||
SELECT
|
SELECT
|
||||||
t1.*
|
t1.*
|
||||||
FROM system_dict_detail t1 LEFT JOIN system_dict t2 ON t1.dict_id = t2.id
|
FROM system_dict_detail t1 LEFT JOIN system_dict t2 ON t1.dict_id = t2.id
|
||||||
<where>
|
<where>
|
||||||
<if test="args.dictName != null and args.dictName != ''">
|
<if test="args.dictName != null and args.dictName != ''">
|
||||||
AND t2.name = #{args.dictName}
|
AND t2.name = #{args.dictName}
|
||||||
</if>
|
</if>
|
||||||
<if test="args.label != null and args.label != ''">
|
<if test="args.label != null and args.label != ''">
|
||||||
AND t1.label = #{args.label}
|
AND t1.label = #{args.label}
|
||||||
</if>
|
</if>
|
||||||
</where>
|
</where>
|
||||||
ORDER BY t1.dict_sort
|
ORDER BY t1.dict_sort
|
||||||
</select>
|
</select>
|
||||||
<select id="listDictDetailByName" resultType="cn.odboy.system.dal.model.response.SystemDictDetailVo">
|
<select id="listDictDetailByName" resultType="cn.odboy.system.dal.model.response.SystemDictDetailVo">
|
||||||
SELECT t1.*
|
SELECT t1.*
|
||||||
FROM system_dict_detail t1
|
FROM system_dict_detail t1
|
||||||
LEFT JOIN system_dict t2 ON t1.dict_id = t2.id
|
LEFT JOIN system_dict t2 ON t1.dict_id = t2.id
|
||||||
<where>
|
<where>
|
||||||
<if test="name != null and name != ''">
|
<if test="name != null and name != ''">
|
||||||
AND t2.name = #{name}
|
AND t2.name = #{name}
|
||||||
</if>
|
</if>
|
||||||
</where>
|
</where>
|
||||||
ORDER BY t1.dict_sort
|
ORDER BY t1.dict_sort
|
||||||
</select>
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
<mapper namespace="cn.odboy.system.dal.mysql.SystemRoleMapper">
|
<mapper namespace="cn.odboy.system.dal.mysql.SystemRoleMapper">
|
||||||
<select id="listRoleLevelByRoleIds" resultType="java.lang.Integer">
|
<select id="listRoleLevelByRoleIds" resultType="java.lang.Integer">
|
||||||
SELECT DISTINCT t1.`level`
|
SELECT DISTINCT t1.`level`
|
||||||
FROM system_role t1
|
FROM system_role t1
|
||||||
WHERE t1.`level` IS NOT NULL
|
WHERE t1.`level` IS NOT NULL
|
||||||
AND t1.role_id IN
|
AND t1.role_id IN
|
||||||
<foreach collection="roleIds" separator="," open="(" close=")" item="roleId">
|
<foreach collection="roleIds" separator="," open="(" close=")" item="roleId">
|
||||||
#{roleId}
|
#{roleId}
|
||||||
</foreach>
|
</foreach>
|
||||||
</select>
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||||
|
<mapper namespace="cn.odboy.system.dal.mysql.SystemRoleMenuMapper">
|
||||||
|
<select id="listMenuPermissionByRoleIds" resultType="java.lang.String">
|
||||||
|
SELECT DISTINCT
|
||||||
|
t2.permission
|
||||||
|
FROM
|
||||||
|
system_roles_menus t1
|
||||||
|
LEFT JOIN system_menu t2 ON t1.menu_id = t2.menu_id
|
||||||
|
WHERE
|
||||||
|
t1.role_id IN
|
||||||
|
<foreach collection="roleIds" item="roleId" open="(" close=")" separator=",">
|
||||||
|
#{roleId}
|
||||||
|
</foreach>
|
||||||
|
</select>
|
||||||
|
</mapper>
|
||||||
@@ -13,4 +13,10 @@
|
|||||||
LEFT JOIN system_users_roles t2 ON t1.role_id = t2.role_id
|
LEFT JOIN system_users_roles t2 ON t1.role_id = t2.role_id
|
||||||
WHERE t2.user_id = #{userId}
|
WHERE t2.user_id = #{userId}
|
||||||
</select>
|
</select>
|
||||||
|
<select id="listUserRoleIdByUserId" resultType="java.lang.Long">
|
||||||
|
SELECT DISTINCT t1.`role_id`
|
||||||
|
FROM system_role t1
|
||||||
|
LEFT JOIN system_users_roles t2 ON t1.role_id = t2.role_id
|
||||||
|
WHERE t2.user_id = #{userId}
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
Reference in New Issue
Block a user