¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.common.annotation; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * æ°æ®æé |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Target(ElementType.METHOD) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Documented |
| | | public @interface DataColumn { |
| | | |
| | | /** |
| | | * å ä½ç¬¦å
³é®å |
| | | */ |
| | | String key() default "deptName"; |
| | | |
| | | /** |
| | | * å ä½ç¬¦æ¿æ¢å¼ |
| | | */ |
| | | String value() default "dept_id"; |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.common.annotation; |
| | | |
| | | import java.lang.annotation.*; |
| | | |
| | | /** |
| | | * æ°æ®æéç» |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Target(ElementType.METHOD) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Documented |
| | | public @interface DataPermission { |
| | | |
| | | DataColumn[] value(); |
| | | |
| | | } |
| | |
| | | * æ°æ®æéè¿æ»¤æ³¨è§£ |
| | | * |
| | | * @author ruoyi |
| | | * @deprecated 3.6.0 ç§»é¤ {@link com.ruoyi.common.annotation.DataPermission} |
| | | */ |
| | | @Target(ElementType.METHOD) |
| | | @Retention(RetentionPolicy.RUNTIME) |
| | | @Documented |
| | | @Deprecated |
| | | public @interface DataScope { |
| | | |
| | | /** |
| | | * é¨é¨è¡¨çå«å |
| | | */ |
| | |
| | | * æ¯å¦è¿æ»¤ç¨æ·æé |
| | | */ |
| | | boolean isUser() default false; |
| | | |
| | | } |
| | |
| | | private Long[] postIds; |
| | | |
| | | /** |
| | | * è§è²ID |
| | | * æ°æ®æé å½åè§è²ID |
| | | */ |
| | | @ApiModelProperty(value = "è§è²ID") |
| | | @TableField(exist = false) |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.common.enums; |
| | | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * æ°æ®æéç±»å |
| | | * |
| | | * è¯æ³æ¯æ spel 模æ¿è¡¨è¾¾å¼ |
| | | * |
| | | * å
ç½®æ°æ® user å½åç¨æ· å
容åè SysUser |
| | | * å¦éæ©å±æ°æ® éå¾ SysUser å
注å
¥ |
| | | * å
ç½®æå¡ sdss ç³»ç»æ°æ®æéæå¡ å
容åè SysDataScopeService |
| | | * å¦éæ©å±æ´å¤èªå®ä¹æå¡ å¯ä»¥åè sdss èªè¡ç¼å |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum DataScopeType { |
| | | |
| | | /** |
| | | * å
¨é¨æ°æ®æé |
| | | */ |
| | | ALL("1", ""), |
| | | |
| | | /** |
| | | * èªå®æ°æ®æé |
| | | */ |
| | | CUSTOM("2", " #{#deptName} IN ( #{@sdss.getRoleCustom( #user.roleId )} ) "), |
| | | |
| | | /** |
| | | * é¨é¨æ°æ®æé |
| | | */ |
| | | DEPT("3", " #{#deptName} = #{#user.deptId} "), |
| | | |
| | | /** |
| | | * é¨é¨å以䏿°æ®æé |
| | | */ |
| | | DEPT_AND_CHILD("4", " #{#deptName} IN ( #{@sdss.getDeptAndChild( #user.deptId )} )"), |
| | | |
| | | /** |
| | | * ä»
æ¬äººæ°æ®æé |
| | | */ |
| | | SELF("5", " #{#userName?:1} = #{#user.userId} "); |
| | | |
| | | private final String code; |
| | | |
| | | /** |
| | | * è¯æ³ éç¨ spel 模æ¿è¡¨è¾¾å¼ |
| | | */ |
| | | private final String sql; |
| | | |
| | | public static DataScopeType findCode(String code) { |
| | | if (StringUtils.isBlank(code)) { |
| | | return null; |
| | | } |
| | | for (DataScopeType type : values()) { |
| | | if (type.getCode().equals(code)) { |
| | | return type; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | } |
| | |
| | | package com.ruoyi.demo.mapper; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.Constants; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.common.annotation.DataColumn; |
| | | import com.ruoyi.common.annotation.DataPermission; |
| | | import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus; |
| | | import com.ruoyi.demo.domain.TestDemo; |
| | | import com.ruoyi.demo.domain.vo.TestDemoVo; |
| | | import org.apache.ibatis.annotations.Param; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * æµè¯å表Mapperæ¥å£ |
| | |
| | | */ |
| | | public interface TestDemoMapper extends BaseMapperPlus<TestDemo> { |
| | | |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "dept_id"), |
| | | @DataColumn(key = "userName", value = "user_id") |
| | | }) |
| | | Page<TestDemoVo> customPageList(@Param("page") Page<TestDemo> page, @Param("ew") Wrapper<TestDemo> wrapper); |
| | | |
| | | @Override |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "dept_id"), |
| | | @DataColumn(key = "userName", value = "user_id") |
| | | }) |
| | | <P extends IPage<TestDemo>> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper<TestDemo> queryWrapper); |
| | | |
| | | @Override |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "dept_id"), |
| | | @DataColumn(key = "userName", value = "user_id") |
| | | }) |
| | | List<TestDemo> selectList(@Param(Constants.WRAPPER) Wrapper<TestDemo> queryWrapper); |
| | | |
| | | @Override |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "dept_id"), |
| | | @DataColumn(key = "userName", value = "user_id") |
| | | }) |
| | | int updateById(@Param(Constants.ENTITY) TestDemo entity); |
| | | |
| | | @Override |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "dept_id"), |
| | | @DataColumn(key = "userName", value = "user_id") |
| | | }) |
| | | int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); |
| | | } |
| | |
| | | package com.ruoyi.demo.mapper; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.Wrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Constants; |
| | | import com.ruoyi.common.annotation.DataColumn; |
| | | import com.ruoyi.common.annotation.DataPermission; |
| | | import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus; |
| | | import com.ruoyi.demo.domain.TestTree; |
| | | import org.apache.ibatis.annotations.Param; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * æµè¯æ 表Mapperæ¥å£ |
| | |
| | | */ |
| | | public interface TestTreeMapper extends BaseMapperPlus<TestTree> { |
| | | |
| | | @Override |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "dept_id"), |
| | | @DataColumn(key = "userName", value = "user_id") |
| | | }) |
| | | List<TestTree> selectList(@Param(Constants.WRAPPER) Wrapper<TestTree> queryWrapper); |
| | | |
| | | @Override |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "dept_id"), |
| | | @DataColumn(key = "userName", value = "user_id") |
| | | }) |
| | | int updateById(@Param(Constants.ENTITY) TestTree entity); |
| | | |
| | | @Override |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "dept_id"), |
| | | @DataColumn(key = "userName", value = "user_id") |
| | | }) |
| | | int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); |
| | | } |
| | |
| | | package com.ruoyi.demo.service.impl; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import com.ruoyi.common.core.domain.PageQuery; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.common.annotation.DataScope; |
| | | import com.ruoyi.common.core.domain.PageQuery; |
| | | import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl; |
| | | import com.ruoyi.common.core.page.PagePlus; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.common.utils.PageUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.demo.domain.TestDemo; |
| | | import com.ruoyi.demo.domain.bo.TestDemoBo; |
| | | import com.ruoyi.demo.domain.vo.TestDemoVo; |
| | |
| | | return getVoById(id); |
| | | } |
| | | |
| | | @DataScope(isUser = true) |
| | | @Override |
| | | public TableDataInfo<TestDemoVo> queryPageList(TestDemoBo bo, PageQuery pageQuery) { |
| | | LambdaQueryWrapper<TestDemo> lqw = buildQueryWrapper(bo); |
| | |
| | | /** |
| | | * èªå®ä¹å页æ¥è¯¢ |
| | | */ |
| | | @DataScope(isUser = true) |
| | | @Override |
| | | public TableDataInfo<TestDemoVo> customPageList(TestDemoBo bo, PageQuery pageQuery) { |
| | | LambdaQueryWrapper<TestDemo> lqw = buildQueryWrapper(bo); |
| | |
| | | return PageUtils.buildDataInfo(result); |
| | | } |
| | | |
| | | @DataScope(isUser = true) |
| | | @Override |
| | | public List<TestDemoVo> queryList(TestDemoBo bo) { |
| | | return listVo(buildQueryWrapper(bo)); |
| | |
| | | |
| | | private LambdaQueryWrapper<TestDemo> buildQueryWrapper(TestDemoBo bo) { |
| | | Map<String, Object> params = bo.getParams(); |
| | | Object dataScope = params.get("dataScope"); |
| | | LambdaQueryWrapper<TestDemo> lqw = Wrappers.lambdaQuery(); |
| | | lqw.like(StringUtils.isNotBlank(bo.getTestKey()), TestDemo::getTestKey, bo.getTestKey()); |
| | | lqw.eq(StringUtils.isNotBlank(bo.getValue()), TestDemo::getValue, bo.getValue()); |
| | | lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null, |
| | | TestDemo::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime")); |
| | | lqw.apply(dataScope != null && StringUtils.isNotBlank(dataScope.toString()), |
| | | dataScope != null ? dataScope.toString() : null); |
| | | return lqw; |
| | | } |
| | | |
| | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ruoyi.common.annotation.DataScope; |
| | | import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.demo.domain.TestTree; |
| | |
| | | } |
| | | |
| | | // @DS("slave") // 忢ä»åºæ¥è¯¢ |
| | | @DataScope(isUser = true) |
| | | @Override |
| | | public List<TestTreeVo> queryList(TestTreeBo bo) { |
| | | LambdaQueryWrapper<TestTree> lqw = buildQueryWrapper(bo); |
| | |
| | | |
| | | private LambdaQueryWrapper<TestTree> buildQueryWrapper(TestTreeBo bo) { |
| | | Map<String, Object> params = bo.getParams(); |
| | | Object dataScope = params.get("dataScope"); |
| | | LambdaQueryWrapper<TestTree> lqw = Wrappers.lambdaQuery(); |
| | | lqw.like(StringUtils.isNotBlank(bo.getTreeName()), TestTree::getTreeName, bo.getTreeName()); |
| | | lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null, |
| | | TestTree::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime")); |
| | | lqw.apply(dataScope != null && StringUtils.isNotBlank(dataScope.toString()), |
| | | dataScope != null ? dataScope.toString() : null); |
| | | return lqw; |
| | | } |
| | | |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.framework.Interceptor; |
| | | |
| | | import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; |
| | | import com.baomidou.mybatisplus.core.toolkit.PluginUtils; |
| | | import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; |
| | | import com.ruoyi.framework.handler.PlusDataPermissionHandler; |
| | | import net.sf.jsqlparser.expression.Expression; |
| | | import net.sf.jsqlparser.statement.delete.Delete; |
| | | import net.sf.jsqlparser.statement.select.PlainSelect; |
| | | import net.sf.jsqlparser.statement.select.Select; |
| | | import net.sf.jsqlparser.statement.select.SelectBody; |
| | | import net.sf.jsqlparser.statement.select.SetOperationList; |
| | | import net.sf.jsqlparser.statement.update.Update; |
| | | import org.apache.ibatis.executor.Executor; |
| | | import org.apache.ibatis.executor.statement.StatementHandler; |
| | | import org.apache.ibatis.mapping.BoundSql; |
| | | import org.apache.ibatis.mapping.MappedStatement; |
| | | import org.apache.ibatis.mapping.SqlCommandType; |
| | | import org.apache.ibatis.session.ResultHandler; |
| | | import org.apache.ibatis.session.RowBounds; |
| | | |
| | | import java.sql.Connection; |
| | | import java.sql.SQLException; |
| | | import java.util.List; |
| | | |
| | | public class PlusDataPermissionInterceptor extends JsqlParserSupport implements InnerInterceptor { |
| | | |
| | | private final PlusDataPermissionHandler dataPermissionHandler = new PlusDataPermissionHandler(); |
| | | |
| | | @Override |
| | | public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { |
| | | if (InterceptorIgnoreHelper.willIgnoreDataPermission(ms.getId())) { |
| | | return; |
| | | } |
| | | PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql); |
| | | mpBs.sql(parserSingle(mpBs.sql(), ms.getId())); |
| | | } |
| | | |
| | | @Override |
| | | public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) { |
| | | PluginUtils.MPStatementHandler mpSh = PluginUtils.mpStatementHandler(sh); |
| | | MappedStatement ms = mpSh.mappedStatement(); |
| | | SqlCommandType sct = ms.getSqlCommandType(); |
| | | if (sct == SqlCommandType.UPDATE || sct == SqlCommandType.DELETE) { |
| | | if (InterceptorIgnoreHelper.willIgnoreDataPermission(ms.getId())) { |
| | | return; |
| | | } |
| | | PluginUtils.MPBoundSql mpBs = mpSh.mPBoundSql(); |
| | | mpBs.sql(parserMulti(mpBs.sql(), ms.getId())); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void processSelect(Select select, int index, String sql, Object obj) { |
| | | SelectBody selectBody = select.getSelectBody(); |
| | | if (selectBody instanceof PlainSelect) { |
| | | this.setWhere((PlainSelect) selectBody, (String) obj); |
| | | } else if (selectBody instanceof SetOperationList) { |
| | | SetOperationList setOperationList = (SetOperationList) selectBody; |
| | | List<SelectBody> selectBodyList = setOperationList.getSelects(); |
| | | selectBodyList.forEach(s -> this.setWhere((PlainSelect) s, (String) obj)); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void processUpdate(Update update, int index, String sql, Object obj) { |
| | | Expression sqlSegment = dataPermissionHandler.getSqlSegment(update.getWhere(), (String) obj, false); |
| | | if (null != sqlSegment) { |
| | | update.setWhere(sqlSegment); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected void processDelete(Delete delete, int index, String sql, Object obj) { |
| | | Expression sqlSegment = dataPermissionHandler.getSqlSegment(delete.getWhere(), (String) obj, false); |
| | | if (null != sqlSegment) { |
| | | delete.setWhere(sqlSegment); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置 where æ¡ä»¶ |
| | | * |
| | | * @param plainSelect æ¥è¯¢å¯¹è±¡ |
| | | * @param mappedStatementId æ§è¡æ¹æ³id |
| | | */ |
| | | protected void setWhere(PlainSelect plainSelect, String mappedStatementId) { |
| | | Expression sqlSegment = dataPermissionHandler.getSqlSegment(plainSelect.getWhere(), mappedStatementId, true); |
| | | if (null != sqlSegment) { |
| | | plainSelect.setWhere(sqlSegment); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | |
| | | * æ°æ®è¿æ»¤å¤ç |
| | | * |
| | | * @author Lion Li |
| | | * @deprecated 3.6.0 ç§»é¤ {@link com.ruoyi.framework.handler.PlusDataPermissionHandler} |
| | | */ |
| | | @Aspect |
| | | @Component |
| | | @Deprecated |
| | | public class DataScopeAspect { |
| | | |
| | | /** |
| | |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; |
| | | import com.ruoyi.common.core.mybatisplus.methods.InsertAll; |
| | | import com.ruoyi.framework.Interceptor.PlusDataPermissionInterceptor; |
| | | import com.ruoyi.framework.handler.CreateAndUpdateMetaObjectHandler; |
| | | import org.mybatis.spring.annotation.MapperScan; |
| | | import org.springframework.context.annotation.Bean; |
| | |
| | | @Bean |
| | | public MybatisPlusInterceptor mybatisPlusInterceptor() { |
| | | MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); |
| | | // æ°æ®æéå¤ç |
| | | interceptor.addInnerInterceptor(dataPermissionInterceptor()); |
| | | // å页æä»¶ |
| | | interceptor.addInnerInterceptor(paginationInnerInterceptor()); |
| | | // ä¹è§éæä»¶ |
| | |
| | | return interceptor; |
| | | } |
| | | |
| | | /** |
| | | * æ°æ®æéæ¦æªå¨ |
| | | */ |
| | | public PlusDataPermissionInterceptor dataPermissionInterceptor() { |
| | | return new PlusDataPermissionInterceptor(); |
| | | } |
| | | |
| | | /** |
| | | * å页æä»¶ï¼èªå¨è¯å«æ°æ®åºç±»å |
| | | */ |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.framework.handler; |
| | | |
| | | import cn.hutool.core.annotation.AnnotationUtil; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.util.ClassUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.ruoyi.common.annotation.DataColumn; |
| | | import com.ruoyi.common.annotation.DataPermission; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.service.UserService; |
| | | import com.ruoyi.common.enums.DataScopeType; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import net.sf.jsqlparser.JSQLParserException; |
| | | import net.sf.jsqlparser.expression.Expression; |
| | | import net.sf.jsqlparser.expression.operators.conditional.AndExpression; |
| | | import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| | | import org.springframework.context.expression.BeanFactoryResolver; |
| | | import org.springframework.expression.BeanResolver; |
| | | import org.springframework.expression.ExpressionParser; |
| | | import org.springframework.expression.ParserContext; |
| | | import org.springframework.expression.common.TemplateParserContext; |
| | | import org.springframework.expression.spel.standard.SpelExpressionParser; |
| | | import org.springframework.expression.spel.support.StandardEvaluationContext; |
| | | |
| | | import java.lang.reflect.Method; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * æ°æ®æéè¿æ»¤ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | public class PlusDataPermissionHandler { |
| | | |
| | | private final ExpressionParser parser = new SpelExpressionParser(); |
| | | private final ParserContext parserContext = new TemplateParserContext(); |
| | | private final BeanResolver beanResolver = new BeanFactoryResolver(SpringUtils.getBeanFactory()); |
| | | |
| | | public Expression getSqlSegment(Expression where, String mappedStatementId, boolean isSelect) { |
| | | DataColumn[] dataColumns = findAnnotation(mappedStatementId); |
| | | if (ArrayUtil.isEmpty(dataColumns)) { |
| | | return where; |
| | | } |
| | | SysUser currentUser = SpringUtils.getBean(UserService.class).selectUserById(SecurityUtils.getUserId()); |
| | | // 妿æ¯è¶
级管çåï¼åä¸è¿æ»¤æ°æ® |
| | | if (StringUtils.isNull(currentUser) || currentUser.isAdmin()) { |
| | | return where; |
| | | } |
| | | String dataFilterSql = buildDataFilter(currentUser, dataColumns, isSelect); |
| | | if (StringUtils.isBlank(dataFilterSql)) { |
| | | return where; |
| | | } |
| | | try { |
| | | Expression expression = CCJSqlParserUtil.parseExpression(dataFilterSql); |
| | | if (ObjectUtil.isNotNull(where)) { |
| | | return new AndExpression(where, expression); |
| | | } else { |
| | | return expression; |
| | | } |
| | | } catch (JSQLParserException e) { |
| | | throw new ServiceException("æ°æ®æéè§£æå¼å¸¸ => " + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æé æ°æ®è¿æ»¤sql |
| | | */ |
| | | private String buildDataFilter(SysUser user, DataColumn[] dataColumns, boolean isSelect) { |
| | | StringBuilder sqlString = new StringBuilder(); |
| | | |
| | | StandardEvaluationContext context = new StandardEvaluationContext(); |
| | | context.setBeanResolver(beanResolver); |
| | | context.setVariable("user", user); |
| | | |
| | | for (DataColumn dataColumn : dataColumns) { |
| | | // 设置注解åé key 为表达å¼åé value 为åéå¼ |
| | | context.setVariable(dataColumn.key(), dataColumn.value()); |
| | | for (SysRole role : user.getRoles()) { |
| | | user.setRoleId(role.getRoleId()); |
| | | |
| | | // è·åè§è²æéæ³å |
| | | DataScopeType type = DataScopeType.findCode(role.getDataScope()); |
| | | if (ObjectUtil.isNull(type)) { |
| | | throw new ServiceException("è§è²æ°æ®èå´å¼å¸¸ => " + role.getDataScope()); |
| | | } |
| | | // å
¨é¨æ°æ®æéç´æ¥è¿å |
| | | if (type == DataScopeType.ALL) { |
| | | return ""; |
| | | } |
| | | // ä¸å
å« key åé åä¸å¤ç |
| | | if (!StringUtils.contains(type.getSql(), "#" + dataColumn.key())) { |
| | | continue; |
| | | } |
| | | // æ´æ°æå é¤éæ»¡è¶³æææ¡ä»¶ |
| | | sqlString.append(isSelect ? " OR " : " AND "); |
| | | // è§£æsql模æ¿å¹¶å¡«å
|
| | | String sql = parser.parseExpression(type.getSql(), parserContext).getValue(context, String.class); |
| | | sqlString.append(sql); |
| | | } |
| | | } |
| | | |
| | | if (StringUtils.isNotBlank(sqlString.toString())) { |
| | | return sqlString.substring(isSelect ? 4 : 5); |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | private DataColumn[] findAnnotation(String mappedStatementId) { |
| | | StringBuilder sb = new StringBuilder(mappedStatementId); |
| | | int index = sb.lastIndexOf("."); |
| | | String clazzName = sb.substring(0, index); |
| | | String methodName = sb.substring(index + 1, sb.length()); |
| | | Class<?> clazz = ClassUtil.loadClass(clazzName); |
| | | List<Method> methods = Arrays.stream(ClassUtil.getDeclaredMethods(clazz)) |
| | | .filter(method -> method.getName().equals(methodName)).collect(Collectors.toList()); |
| | | DataPermission dataPermission; |
| | | for (Method method : methods) { |
| | | if (AnnotationUtil.hasAnnotation(method, DataPermission.class)) { |
| | | dataPermission = AnnotationUtil.getAnnotation(method, DataPermission.class); |
| | | return dataPermission.value(); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | } |
| | |
| | | package com.ruoyi.system.mapper; |
| | | |
| | | import com.ruoyi.common.annotation.DataColumn; |
| | | import com.ruoyi.common.annotation.DataPermission; |
| | | import com.ruoyi.common.core.domain.entity.SysDept; |
| | | import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus; |
| | | import org.apache.ibatis.annotations.Param; |
| | |
| | | * @param dept é¨é¨ä¿¡æ¯ |
| | | * @return é¨é¨ä¿¡æ¯éå |
| | | */ |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "d.dept_id") |
| | | }) |
| | | List<SysDept> selectDeptList(SysDept dept); |
| | | |
| | | /** |
| | |
| | | package com.ruoyi.system.mapper; |
| | | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.common.annotation.DataColumn; |
| | | import com.ruoyi.common.annotation.DataPermission; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | | import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus; |
| | | import org.apache.ibatis.annotations.Param; |
| | |
| | | */ |
| | | public interface SysRoleMapper extends BaseMapperPlus<SysRole> { |
| | | |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "d.dept_id") |
| | | }) |
| | | Page<SysRole> selectPageRoleList(@Param("page") Page<SysRole> page, @Param("role") SysRole role); |
| | | |
| | | /** |
| | |
| | | * @param role è§è²ä¿¡æ¯ |
| | | * @return è§è²æ°æ®éåä¿¡æ¯ |
| | | */ |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "d.dept_id") |
| | | }) |
| | | List<SysRole> selectRoleList(SysRole role); |
| | | |
| | | /** |
| | |
| | | package com.ruoyi.system.mapper; |
| | | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.common.annotation.DataColumn; |
| | | import com.ruoyi.common.annotation.DataPermission; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.mybatisplus.core.BaseMapperPlus; |
| | | import org.apache.ibatis.annotations.Param; |
| | |
| | | */ |
| | | public interface SysUserMapper extends BaseMapperPlus<SysUser> { |
| | | |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "d.dept_id"), |
| | | @DataColumn(key = "userName", value = "u.user_id") |
| | | }) |
| | | Page<SysUser> selectPageUserList(@Param("page") Page<SysUser> page, @Param("user") SysUser user); |
| | | |
| | | /** |
| | |
| | | * @param sysUser ç¨æ·ä¿¡æ¯ |
| | | * @return ç¨æ·ä¿¡æ¯éåä¿¡æ¯ |
| | | */ |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "d.dept_id"), |
| | | @DataColumn(key = "userName", value = "u.user_id") |
| | | }) |
| | | List<SysUser> selectUserList(SysUser sysUser); |
| | | |
| | | /** |
| | |
| | | * @param user ç¨æ·ä¿¡æ¯ |
| | | * @return ç¨æ·ä¿¡æ¯éåä¿¡æ¯ |
| | | */ |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "d.dept_id"), |
| | | @DataColumn(key = "userName", value = "u.user_id") |
| | | }) |
| | | Page<SysUser> selectAllocatedList(@Param("page") Page<SysUser> page, @Param("user") SysUser user); |
| | | |
| | | /** |
| | |
| | | * @param user ç¨æ·ä¿¡æ¯ |
| | | * @return ç¨æ·ä¿¡æ¯éåä¿¡æ¯ |
| | | */ |
| | | @DataPermission({ |
| | | @DataColumn(key = "deptName", value = "d.dept_id"), |
| | | @DataColumn(key = "userName", value = "u.user_id") |
| | | }) |
| | | Page<SysUser> selectUnallocatedList(@Param("page") Page<SysUser> page, @Param("user") SysUser user); |
| | | |
| | | /** |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.system.service; |
| | | |
| | | /** |
| | | * éç¨ æ°æ®æé æå¡ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface SysDataScopeService { |
| | | |
| | | /** |
| | | * è·åè§è²èªå®ä¹æé |
| | | * @param roleId è§è²id |
| | | * @return é¨é¨idç» |
| | | */ |
| | | String getRoleCustom(Long roleId); |
| | | |
| | | /** |
| | | * è·åé¨é¨å以䏿é |
| | | * @param deptId é¨é¨id |
| | | * @return é¨é¨idç» |
| | | */ |
| | | String getDeptAndChild(Long deptId); |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.convert.Convert; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.ruoyi.common.core.domain.entity.SysDept; |
| | | import com.ruoyi.system.domain.SysRoleDept; |
| | | import com.ruoyi.system.mapper.SysDeptMapper; |
| | | import com.ruoyi.system.mapper.SysRoleDeptMapper; |
| | | import com.ruoyi.system.service.SysDataScopeService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service("sdss") |
| | | public class SysDataScopeServiceImpl implements SysDataScopeService { |
| | | |
| | | @Autowired |
| | | private SysRoleDeptMapper roleDeptMapper; |
| | | @Autowired |
| | | private SysDeptMapper deptMapper; |
| | | |
| | | @Override |
| | | public String getRoleCustom(Long roleId) { |
| | | List<SysRoleDept> list = roleDeptMapper.selectList( |
| | | new LambdaQueryWrapper<SysRoleDept>() |
| | | .select(SysRoleDept::getDeptId) |
| | | .eq(SysRoleDept::getRoleId, roleId)); |
| | | if (CollUtil.isNotEmpty(list)) { |
| | | return list.stream().map(rd -> Convert.toStr(rd.getDeptId())).collect(Collectors.joining(",")); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | public String getDeptAndChild(Long deptId) { |
| | | List<SysDept> list = deptMapper.selectList(new LambdaQueryWrapper<SysDept>() |
| | | .select(SysDept::getDeptId) |
| | | .eq(SysDept::getDeptId, deptId) |
| | | .or() |
| | | .apply("find_in_set({0},ancestors)", deptId)); |
| | | if (CollUtil.isNotEmpty(list)) { |
| | | return list.stream().map(d -> Convert.toStr(d.getDeptId())).collect(Collectors.joining(",")); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | } |
| | |
| | | import cn.hutool.core.lang.tree.Tree; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.ruoyi.common.annotation.DataScope; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.entity.SysDept; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | |
| | | * @return é¨é¨ä¿¡æ¯éå |
| | | */ |
| | | @Override |
| | | @DataScope(deptAlias = "d") |
| | | public List<SysDept> selectDeptList(SysDept dept) { |
| | | // return baseMapper.selectList(); |
| | | // return baseMapper.selectList(new LambdaQueryWrapper<>()); |
| | | return baseMapper.selectDeptList(dept); |
| | | } |
| | | |
| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.common.annotation.DataScope; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.PageQuery; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | |
| | | private SysRoleDeptMapper roleDeptMapper; |
| | | |
| | | @Override |
| | | @DataScope(deptAlias = "d") |
| | | public TableDataInfo<SysRole> selectPageRoleList(SysRole role, PageQuery pageQuery) { |
| | | Page<SysRole> page = baseMapper.selectPageRoleList(PageUtils.buildPage(pageQuery), role); |
| | | return PageUtils.buildDataInfo(page); |
| | |
| | | * @return è§è²æ°æ®éåä¿¡æ¯ |
| | | */ |
| | | @Override |
| | | @DataScope(deptAlias = "d") |
| | | public List<SysRole> selectRoleList(SysRole role) { |
| | | return baseMapper.selectRoleList(role); |
| | | } |
| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.ruoyi.common.annotation.DataScope; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.PageQuery; |
| | | import com.ruoyi.common.core.domain.entity.SysRole; |
| | |
| | | private SysUserPostMapper userPostMapper; |
| | | |
| | | @Override |
| | | @DataScope(deptAlias = "d", userAlias = "u", isUser = true) |
| | | public TableDataInfo<SysUser> selectPageUserList(SysUser user, PageQuery pageQuery) { |
| | | Page<SysUser> page = baseMapper.selectPageUserList(PageUtils.buildPage(pageQuery), user); |
| | | return PageUtils.buildDataInfo(page); |
| | |
| | | * @return ç¨æ·ä¿¡æ¯éåä¿¡æ¯ |
| | | */ |
| | | @Override |
| | | @DataScope(deptAlias = "d", userAlias = "u", isUser = true) |
| | | public List<SysUser> selectUserList(SysUser user) { |
| | | return baseMapper.selectUserList(user); |
| | | } |
| | |
| | | * @return ç¨æ·ä¿¡æ¯éåä¿¡æ¯ |
| | | */ |
| | | @Override |
| | | @DataScope(deptAlias = "d", userAlias = "u", isUser = true) |
| | | public TableDataInfo<SysUser> selectAllocatedList(SysUser user, PageQuery pageQuery) { |
| | | Page<SysUser> page = baseMapper.selectAllocatedList(PageUtils.buildPage(pageQuery), user); |
| | | return PageUtils.buildDataInfo(page); |
| | |
| | | * @return ç¨æ·ä¿¡æ¯éåä¿¡æ¯ |
| | | */ |
| | | @Override |
| | | @DataScope(deptAlias = "d", userAlias = "u", isUser = true) |
| | | public TableDataInfo<SysUser> selectUnallocatedList(SysUser user, PageQuery pageQuery) { |
| | | Page<SysUser> page = baseMapper.selectUnallocatedList(PageUtils.buildPage(pageQuery), user); |
| | | return PageUtils.buildDataInfo(page); |
| | |
| | | <if test="status != null and status != ''"> |
| | | AND status = #{status} |
| | | </if> |
| | | <!-- æ°æ®èå´è¿æ»¤ --> |
| | | <if test="params.dataScope != null and params.dataScope != ''"> |
| | | AND ( ${params.dataScope} ) |
| | | </if> |
| | | <!-- <!– æ°æ®èå´è¿æ»¤ –>--> |
| | | <!-- <if test="params.dataScope != null and params.dataScope != ''">--> |
| | | <!-- AND ( ${params.dataScope} )--> |
| | | <!-- </if>--> |
| | | order by d.parent_id, d.order_num |
| | | </select> |
| | | |
| | |
| | | <if test="role.params.endTime != null and role.params.endTime != ''"><!-- ç»ææ¶é´æ£ç´¢ --> |
| | | and date_format(r.create_time,'%y%m%d') <= date_format(#{role.params.endTime},'%y%m%d') |
| | | </if> |
| | | <!-- æ°æ®èå´è¿æ»¤ --> |
| | | <if test="role.params.dataScope != null and role.params.dataScope != ''"> |
| | | AND ( ${role.params.dataScope} ) |
| | | </if> |
| | | <!-- <!– æ°æ®èå´è¿æ»¤ –>--> |
| | | <!-- <if test="role.params.dataScope != null and role.params.dataScope != ''">--> |
| | | <!-- AND ( ${role.params.dataScope} )--> |
| | | <!-- </if>--> |
| | | order by r.role_sort |
| | | </select> |
| | | |
| | |
| | | <if test="params.endTime != null and params.endTime != ''"><!-- ç»ææ¶é´æ£ç´¢ --> |
| | | and date_format(r.create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d') |
| | | </if> |
| | | <!-- æ°æ®èå´è¿æ»¤ --> |
| | | <if test="params.dataScope != null and params.dataScope != ''"> |
| | | AND ( ${params.dataScope} ) |
| | | </if> |
| | | <!-- <!– æ°æ®èå´è¿æ»¤ –>--> |
| | | <!-- <if test="params.dataScope != null and params.dataScope != ''">--> |
| | | <!-- AND ( ${params.dataScope} )--> |
| | | <!-- </if>--> |
| | | order by r.role_sort |
| | | </select> |
| | | |
| | |
| | | AND (u.dept_id = #{user.deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{user.deptId}, |
| | | ancestors) )) |
| | | </if> |
| | | <!-- æ°æ®èå´è¿æ»¤ --> |
| | | <if test="user.params.dataScope != null and user.params.dataScope != ''"> |
| | | AND ( ${user.params.dataScope} ) |
| | | </if> |
| | | <!-- <!– æ°æ®èå´è¿æ»¤ –>--> |
| | | <!-- <if test="user.params.dataScope != null and user.params.dataScope != ''">--> |
| | | <!-- AND ( ${user.params.dataScope} )--> |
| | | <!-- </if>--> |
| | | </select> |
| | | |
| | | <select id="selectUserList" parameterType="SysUser" resultMap="SysUserResult"> |
| | |
| | | AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, |
| | | ancestors) )) |
| | | </if> |
| | | <!-- æ°æ®èå´è¿æ»¤ --> |
| | | <if test="params.dataScope != null and params.dataScope != ''"> |
| | | AND ( ${params.dataScope} ) |
| | | </if> |
| | | <!-- <!– æ°æ®èå´è¿æ»¤ –>--> |
| | | <!-- <if test="params.dataScope != null and params.dataScope != ''">--> |
| | | <!-- AND ( ${params.dataScope} )--> |
| | | <!-- </if>--> |
| | | </select> |
| | | |
| | | <select id="selectAllocatedList" parameterType="SysUser" resultMap="SysUserResult"> |