若依添加数据权限
因为业务要求,需要给不同部门的人给不同的数据查看权限,所以需要进行权限控制,本功能思路是,在业务表中增加用户id以及部门ID,然后再查询的时候进行过滤,就达到了 个人 以及部门的权限控制。
代码修改
第一步-【新增权限控制字段】
在业务表中创建user_id以及dept_id字段。
alter table 业务表
add user_id bigint not null comment '用户ID';
alter table 业务表
add dept_id bigint not null comment '部门ID';
第二步-【修改Bean以及Mapper.xml】
完善业务Bean以及Mapper.xml
- 在业务对象中新增userId以及deptId,如下图
/**
* 用户ID
*/
private Long userId;
/**
* 部门Id
*/
private Long deptId;
- 在resultMap中新增映射
<result property="userId" column="user_id" />
<result property="deptId" column="dept_id" />
- 新增字段查询,并为此表增加别名
select id, customer_name,customer_num,user_id,dept_id from t_customer_info tci
- 在SQL后增加DataScope串
${params.dataScope}
- 在insert语句中增加userId以及deptId插入
<insert id="insertTCustomerInfo" parameterType="TCustomerInfo">
insert into t_customer_info
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="userId != null">user_id,</if>
<if test="deptId != null">dept_id,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="userId != null">#{userId},</if>
<if test="deptId != null">#{deptId},</if>
</trim>
</insert>
- 最终XML修改如下图
第三步-[修改接口实现类]
- 修改Controller接口-新增@DataScope注解
@DataScope(deptAlias = "tci",userAlias = "tci")
如图下
- 入库时在ServiceImpl中新增权限字段信息入库
tCustomerInfo.setUserId(ShiroUtils.getUserId());
tCustomerInfo.setDeptId(ShiroUtils.getSysUser().getDeptId());
如下图
创建角色配置登录用户信息
- 创建角色
- 分配数据权限
- 创建用户选择客户权限
开始测试
- 使用coustom001账号登录
遇到的问题
- 在进行测试的时候出现了如下错误
发现类型转换出现了错误,后查看若依DataScope注解发现 在使用的时候项目中存在多个BaseEntity类,其中本业务集成的BaseEntity类跟DataScope中强转的类不同,改变一下业务类集成即可解决问题
- 源码地址如下: