Commit 4425b81e by RuoYi

新增数据权限过滤注解

parent eb2a24dc
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -374,8 +374,8 @@ insert into sys_role_dept values ('2', '105'); ...@@ -374,8 +374,8 @@ insert into sys_role_dept values ('2', '105');
drop table if exists sys_user_post; drop table if exists sys_user_post;
create table sys_user_post create table sys_user_post
( (
user_id varchar(64) not null comment '用户ID', user_id int(11) not null comment '用户ID',
post_id varchar(64) not null comment '岗位ID', post_id int(11) not null comment '岗位ID',
primary key (user_id, post_id) primary key (user_id, post_id)
) engine=innodb default charset=utf8 comment = '用户与岗位关联表'; ) engine=innodb default charset=utf8 comment = '用户与岗位关联表';
...@@ -450,8 +450,8 @@ create table sys_dict_data ...@@ -450,8 +450,8 @@ create table sys_dict_data
dict_label varchar(100) default '' comment '字典标签', dict_label varchar(100) default '' comment '字典标签',
dict_value varchar(100) default '' comment '字典键值', dict_value varchar(100) default '' comment '字典键值',
dict_type varchar(100) default '' comment '字典类型', dict_type varchar(100) default '' comment '字典类型',
css_class varchar(500) default '' comment '样式属性(其他样式扩展)', css_class varchar(100) default '' comment '样式属性(其他样式扩展)',
list_class varchar(500) default '' comment '表格回显样式', list_class varchar(100) default '' comment '表格回显样式',
is_default char(1) default 'N' comment '是否默认(Y是 N否)', is_default char(1) default 'N' comment '是否默认(Y是 N否)',
status char(1) default '0' comment '状态(0正常 1停用)', status char(1) default '0' comment '状态(0正常 1停用)',
create_by varchar(64) default '' comment '创建者', create_by varchar(64) default '' comment '创建者',
......
package com.ruoyi.framework.datascope; package com.ruoyi.framework.aspectj;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils; import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.framework.web.domain.BaseEntity;
import com.ruoyi.project.system.role.domain.Role; import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.user.domain.User; import com.ruoyi.project.system.user.domain.User;
/** /**
* 数据范围处理 * 数据过滤处理
* *
* @author ruoyi * @author ruoyi
*/ */
public class DataScopeUtils @Aspect
@Component
public class DataScopeAspect
{ {
/** /**
* 全部数据权限 * 全部数据权限
...@@ -23,13 +35,40 @@ public class DataScopeUtils ...@@ -23,13 +35,40 @@ public class DataScopeUtils
public static final String DATA_SCOPE_CUSTOM = "2"; public static final String DATA_SCOPE_CUSTOM = "2";
/** /**
* 数据范围过滤 * 数据权限过滤关键字
*
* @return 标准连接条件对象
*/ */
public static String dataScopeFilter() public static final String DATA_SCOPE = "dataScope";
// 配置织入点
@Pointcut("@annotation(com.ruoyi.framework.aspectj.lang.annotation.DataScope)")
public void dataScopePointCut()
{ {
return dataScopeFilter("u"); }
@Before("dataScopePointCut()")
public void doBefore(JoinPoint point) throws Throwable
{
handleDataScope(point);
}
protected void handleDataScope(final JoinPoint joinPoint)
{
// 获得注解
DataScope controllerDataScope = getAnnotationLog(joinPoint);
if (controllerDataScope == null)
{
return;
}
// 获取当前的用户
User currentUser = ShiroUtils.getUser();
if (currentUser != null)
{
// 如果是超级管理员,则不过滤数据
if (!currentUser.isAdmin())
{
dataScopeFilter(joinPoint, currentUser, controllerDataScope.tableAlias());
}
}
} }
/** /**
...@@ -38,15 +77,8 @@ public class DataScopeUtils ...@@ -38,15 +77,8 @@ public class DataScopeUtils
* @param da 部门表别名 * @param da 部门表别名
* @return 标准连接条件对象 * @return 标准连接条件对象
*/ */
public static String dataScopeFilter(String da) public static void dataScopeFilter(JoinPoint joinPoint, User user, String alias)
{
User user = ShiroUtils.getUser();
// 如果是超级管理员,则不过滤数据
if (user.isAdmin())
{ {
return StringUtils.EMPTY;
}
StringBuilder sqlString = new StringBuilder(); StringBuilder sqlString = new StringBuilder();
for (Role role : user.getRoles()) for (Role role : user.getRoles())
...@@ -59,14 +91,32 @@ public class DataScopeUtils ...@@ -59,14 +91,32 @@ public class DataScopeUtils
} }
else if (DATA_SCOPE_CUSTOM.equals(dataScope)) else if (DATA_SCOPE_CUSTOM.equals(dataScope))
{ {
sqlString.append(StringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", da, role.getRoleId())); sqlString.append(StringUtils.format(
" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", alias,
role.getRoleId()));
} }
} }
if (StringUtils.isNotBlank(sqlString.toString())) if (StringUtils.isNotBlank(sqlString.toString()))
{ {
return " AND (" + sqlString.substring(4) + ")"; BaseEntity baseEntity = (BaseEntity) joinPoint.getArgs()[0];
baseEntity.getParams().put(DATA_SCOPE, " AND (" + sqlString.substring(4) + ")");
}
}
/**
* 是否存在注解,如果存在就获取
*/
private DataScope getAnnotationLog(JoinPoint joinPoint)
{
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null)
{
return method.getAnnotation(DataScope.class);
} }
return StringUtils.EMPTY; return null;
} }
} }
...@@ -11,7 +11,7 @@ import org.slf4j.LoggerFactory; ...@@ -11,7 +11,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Ds; import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder; import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
/** /**
...@@ -22,11 +22,11 @@ import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder; ...@@ -22,11 +22,11 @@ import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
@Aspect @Aspect
@Order(1) @Order(1)
@Component @Component
public class DsAspect public class DataSourceAspect
{ {
protected Logger logger = LoggerFactory.getLogger(getClass()); protected Logger logger = LoggerFactory.getLogger(getClass());
@Pointcut("@annotation(com.ruoyi.framework.aspectj.lang.annotation.Ds)") @Pointcut("@annotation(com.ruoyi.framework.aspectj.lang.annotation.DataSource)")
public void dsPointCut() public void dsPointCut()
{ {
...@@ -39,7 +39,7 @@ public class DsAspect ...@@ -39,7 +39,7 @@ public class DsAspect
Method method = signature.getMethod(); Method method = signature.getMethod();
Ds dataSource = method.getAnnotation(Ds.class); DataSource dataSource = method.getAnnotation(DataSource.class);
if (StringUtils.isNotNull(dataSource)) if (StringUtils.isNotNull(dataSource))
{ {
......
package com.ruoyi.framework.aspectj.lang.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 数据权限过滤注解
*
* @author ruoyi
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataScope
{
/** 表的别名 */
String tableAlias() default "";
}
...@@ -14,7 +14,7 @@ import com.ruoyi.framework.aspectj.lang.enums.DataSourceType; ...@@ -14,7 +14,7 @@ import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
*/ */
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface Ds public @interface DataSource
{ {
/** /**
* 切换数据源名称 * 切换数据源名称
......
...@@ -32,7 +32,7 @@ public class ResourcesConfig implements WebMvcConfigurer ...@@ -32,7 +32,7 @@ public class ResourcesConfig implements WebMvcConfigurer
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) public void addResourceHandlers(ResourceHandlerRegistry registry)
{ {
/** 头像上传路径 */ /** 文件上传路径 */
registry.addResourceHandler("/profile/**").addResourceLocations("file:" + RuoYiConfig.getProfile()); registry.addResourceHandler("/profile/**").addResourceLocations("file:" + RuoYiConfig.getProfile());
/** swagger配置 */ /** swagger配置 */
......
...@@ -9,7 +9,7 @@ import org.springframework.stereotype.Service; ...@@ -9,7 +9,7 @@ import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.UserConstants; import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils; import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.datascope.DataScopeUtils; import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.project.system.dept.domain.Dept; import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.mapper.DeptMapper; import com.ruoyi.project.system.dept.mapper.DeptMapper;
import com.ruoyi.project.system.role.domain.Role; import com.ruoyi.project.system.role.domain.Role;
...@@ -31,9 +31,9 @@ public class DeptServiceImpl implements IDeptService ...@@ -31,9 +31,9 @@ public class DeptServiceImpl implements IDeptService
* @return 部门信息集合 * @return 部门信息集合
*/ */
@Override @Override
@DataScope(tableAlias = "d")
public List<Dept> selectDeptList(Dept dept) public List<Dept> selectDeptList(Dept dept)
{ {
dept.getParams().put("dataScope", DataScopeUtils.dataScopeFilter("d"));
return deptMapper.selectDeptList(dept); return deptMapper.selectDeptList(dept);
} }
......
...@@ -11,7 +11,7 @@ import com.ruoyi.common.constant.UserConstants; ...@@ -11,7 +11,7 @@ import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.support.Convert; import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils; import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.datascope.DataScopeUtils; import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.project.system.role.domain.Role; import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.domain.RoleDept; import com.ruoyi.project.system.role.domain.RoleDept;
import com.ruoyi.project.system.role.domain.RoleMenu; import com.ruoyi.project.system.role.domain.RoleMenu;
...@@ -48,9 +48,9 @@ public class RoleServiceImpl implements IRoleService ...@@ -48,9 +48,9 @@ public class RoleServiceImpl implements IRoleService
* @return 角色数据集合信息 * @return 角色数据集合信息
*/ */
@Override @Override
@DataScope(tableAlias = "u")
public List<Role> selectRoleList(Role role) public List<Role> selectRoleList(Role role)
{ {
role.getParams().put("dataScope", DataScopeUtils.dataScopeFilter());
return roleMapper.selectRoleList(role); return roleMapper.selectRoleList(role);
} }
......
...@@ -8,7 +8,7 @@ import com.ruoyi.common.constant.UserConstants; ...@@ -8,7 +8,7 @@ import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.support.Convert; import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils; import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.datascope.DataScopeUtils; import com.ruoyi.framework.aspectj.lang.annotation.DataScope;
import com.ruoyi.framework.shiro.service.PasswordService; import com.ruoyi.framework.shiro.service.PasswordService;
import com.ruoyi.project.system.post.domain.Post; import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.post.mapper.PostMapper; import com.ruoyi.project.system.post.mapper.PostMapper;
...@@ -55,10 +55,10 @@ public class UserServiceImpl implements IUserService ...@@ -55,10 +55,10 @@ public class UserServiceImpl implements IUserService
* @return 用户信息集合信息 * @return 用户信息集合信息
*/ */
@Override @Override
@DataScope(tableAlias = "u")
public List<User> selectUserList(User user) public List<User> selectUserList(User user)
{ {
// 生成数据权限过滤条件 // 生成数据权限过滤条件
user.getParams().put("dataScope", DataScopeUtils.dataScopeFilter());
return userMapper.selectUserList(user); return userMapper.selectUserList(user);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment