fix(youlai-admin): 修复开启数据权限时查询角色和部门过滤失败

修复开启数据权限时查询角色和部门过滤失败
This commit is contained in:
chuan 2022-09-24 20:02:02 +08:00
parent ae12a34008
commit 48bf574e2f
3 changed files with 63 additions and 31 deletions

View File

@ -1,15 +1,10 @@
package com.youlai.admin.mapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.youlai.admin.pojo.entity.SysDept;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.admin.pojo.entity.SysRole;
import com.youlai.admin.pojo.entity.SysUser;
import com.youlai.admin.pojo.query.RolePageQuery;
import com.youlai.common.mybatis.annotation.DataPermission;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@ -18,11 +13,12 @@ import java.util.List;
public interface SysRoleMapper extends BaseMapper<SysRole> {
@DataPermission()
@Override
List<SysRole> selectList(@Param(Constants.WRAPPER) Wrapper<SysRole> queryWrapper);
@DataPermission()
<E extends IPage<SysRole>> E selectPage(E page, @Param("ew") Wrapper<SysRole> queryWrapper);
@DataPermission(deptAlias = "d",userAlias = "u")
Page<SysRole> listRolePages(Page<SysRole> page, RolePageQuery queryParams,boolean isRoot,String rootCode);
@DataPermission(deptAlias = "d",userAlias = "u")
List<SysRole> listDeptOptions(boolean isRoot,String rootCode);
}

View File

@ -59,19 +59,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
// 查询参数
int pageNum = queryParams.getPageNum();
int pageSize = queryParams.getPageSize();
String keywords = queryParams.getKeywords();
// 查询数据
Page<SysRole> rolePage = this.page(
new Page<>(pageNum, pageSize),
new LambdaQueryWrapper<SysRole>()
.like(StrUtil.isNotBlank(keywords), SysRole::getName, keywords)
.or()
.like(StrUtil.isNotBlank(keywords), SysRole::getCode, keywords)
.ne(!UserUtils.isRoot(), SysRole::getCode, GlobalConstants.ROOT_ROLE_CODE) // 非超级管理员不显示超级管理员角色
.select(SysRole::getId, SysRole::getName, SysRole::getCode,SysRole::getDataScope)
);
Page<SysRole> rolePage = this.baseMapper.listRolePages( new Page<>(pageNum, pageSize), queryParams,UserUtils.isRoot(),GlobalConstants.ROOT_ROLE_CODE);
// 实体转换
Page<RolePageVO> pageResult = roleConverter.entity2Page(rolePage);
return pageResult;
@ -85,12 +73,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
@Override
public List<Option> listRoleOptions() {
// 查询数据
List<SysRole> roleList = this.list(new LambdaQueryWrapper<SysRole>()
.ne(!UserUtils.isRoot(), SysRole::getCode, GlobalConstants.ROOT_ROLE_CODE)
.select(SysRole::getId, SysRole::getName)
.orderByAsc(SysRole::getSort)
);
List<SysRole> roleList = this.baseMapper.listDeptOptions(UserUtils.isRoot(),GlobalConstants.ROOT_ROLE_CODE);
// 实体转换
List<Option> list = roleConverter.roles2Options(roleList);
return list;

View File

@ -0,0 +1,53 @@
<?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="com.youlai.admin.mapper.SysRoleMapper">
<resultMap type="com.youlai.admin.pojo.entity.SysRole" id="SysRoleResult">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="code" column="code" />
<result property="sort" column="sort" />
<result property="status" column="status" />
<result property="deleted" column="deleted" />
<result property="createTime" column="create_time" />
<result property="updateTime" column="update_time" />
<result property="dataScope" column="data_scope" />
</resultMap>
<sql id="selectRoleVo">
select distinct r.id, r.name, r.code, r.sort, r.status, r.deleted, r.create_time,
r.update_time, r.data_scope
from sys_role r
LEFT JOIN sys_user_role ur ON ur.role_id = r.id
LEFT JOIN sys_user u ON u.id = ur.user_id
LEFT JOIN sys_dept d ON u.dept_id = d.id
</sql>
<select id="listRolePages" parameterType="com.youlai.admin.pojo.entity.SysRole" resultMap="SysRoleResult">
<include refid="selectRoleVo"/>
where r.deleted = 0
<if test='queryParams.keywords!=null and queryParams.keywords.trim() neq ""'>
AND (
u.name LIKE CONCAT('%',#{queryParams.keywords},'%')
OR u.code LIKE CONCAT('%',#{queryParams.keywords},'%')
)
</if>
<if test="isRoot == false">
AND code != #{rootCode}
</if>
order by r.sort
</select>
<select id="listDeptOptions" parameterType="com.youlai.admin.pojo.entity.SysRole" resultMap="SysRoleResult">
<include refid="selectRoleVo"/>
where r.deleted = 0
<if test="isRoot == false">
AND code != #{rootCode}
</if>
order by r.sort
</select>
</mapper>