|
| 1 | +package com.baomidou.mybatisplus.extension.plugins.inner; |
| 2 | + |
| 3 | +import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler; |
| 4 | +import net.sf.jsqlparser.JSQLParserException; |
| 5 | +import net.sf.jsqlparser.expression.Expression; |
| 6 | +import net.sf.jsqlparser.expression.operators.conditional.AndExpression; |
| 7 | +import net.sf.jsqlparser.expression.operators.conditional.OrExpression; |
| 8 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
| 16 | +/** |
| 17 | + * 数据权限拦截器测试 |
| 18 | + * |
| 19 | + * @author hubin |
| 20 | + * @since 3.4.1 + |
| 21 | + */ |
| 22 | +public class DataPermissionInterceptorTest { |
| 23 | + private static String TEST_1 = "com.baomidou.userMapper.selectByUsername"; |
| 24 | + private static String TEST_2 = "com.baomidou.userMapper.selectById"; |
| 25 | + private static String TEST_3 = "com.baomidou.roleMapper.selectByCompanyId"; |
| 26 | + private static String TEST_4 = "com.baomidou.roleMapper.selectById"; |
| 27 | + |
| 28 | + /** |
| 29 | + * 这里可以理解为数据库配置的数据权限规则 SQL |
| 30 | + */ |
| 31 | + private static Map<String, String> sqlSegmentMap = new HashMap<String, String>() { |
| 32 | + { |
| 33 | + put(TEST_1, "username='123' or userId IN (1,2,3)"); |
| 34 | + put(TEST_2, "u.state=1 and u.amount > 1000"); |
| 35 | + put(TEST_3, "companyId in (1,2,3)"); |
| 36 | + put(TEST_4, "username like 'abc%'"); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + private static DataPermissionInterceptor interceptor = new DataPermissionInterceptor(new DataPermissionHandler() { |
| 41 | + |
| 42 | + @Override |
| 43 | + public Expression getSqlSegment(Expression where, String mappedStatementId) { |
| 44 | + try { |
| 45 | + String sqlSegment = sqlSegmentMap.get(mappedStatementId); |
| 46 | + Expression sqlSegmentExpression = CCJSqlParserUtil.parseCondExpression(sqlSegment); |
| 47 | + if (null != where) { |
| 48 | + System.out.println("原 where = " + where.toString()); |
| 49 | + if (mappedStatementId.equals(TEST_4)) { |
| 50 | + // 这里测试返回 OR 条件 |
| 51 | + return new OrExpression(where, sqlSegmentExpression); |
| 52 | + } |
| 53 | + return new AndExpression(where, sqlSegmentExpression); |
| 54 | + } |
| 55 | + return sqlSegmentExpression; |
| 56 | + } catch (JSQLParserException e) { |
| 57 | + e.printStackTrace(); |
| 58 | + } |
| 59 | + return null; |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + @Test |
| 64 | + void test1() { |
| 65 | + assertSql(TEST_1, "select * from sys_user", |
| 66 | + "SELECT * FROM sys_user WHERE username = '123' OR userId IN (1, 2, 3)"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void test2() { |
| 71 | + assertSql(TEST_2, "select u.username from sys_user u join sys_user_role r on u.id=r.user_id where r.role_id=3", |
| 72 | + "SELECT u.username FROM sys_user u JOIN sys_user_role r ON u.id = r.user_id WHERE r.role_id = 3 AND u.state = 1 AND u.amount > 1000"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void test3() { |
| 77 | + assertSql(TEST_3, "select * from sys_role where company_id=6", |
| 78 | + "SELECT * FROM sys_role WHERE company_id = 6 AND companyId IN (1, 2, 3)"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void test4() { |
| 83 | + assertSql(TEST_4, "select * from sys_role where id=3", |
| 84 | + "SELECT * FROM sys_role WHERE id = 3 OR username LIKE 'abc%'"); |
| 85 | + } |
| 86 | + |
| 87 | + void assertSql(String mappedStatementId, String sql, String targetSql) { |
| 88 | + assertThat(interceptor.parserSingle(sql, mappedStatementId)).isEqualTo(targetSql); |
| 89 | + } |
| 90 | +} |
0 commit comments