Skip to content

Commit

Permalink
feat(extension/datapermission): 新增数据权限模块
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Sep 26, 2024
1 parent 04e0b4b commit 7666d56
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ public class PropertiesConstants {
*/
public static final String MESSAGING_WEBSOCKET = MESSAGING + StringConstants.DOT + "websocket";

/**
* 数据权限配置
*/
public static final String DATA_PERMISSION = CONTINEW_STARTER + StringConstants.DOT + "data-permission";

/**
* 多租户配置
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public class MyBatisPlusExtensionProperties {
@NestedConfigurationProperty
private MyBatisPlusIdGeneratorProperties idGenerator;

/**
* 数据权限插件配置
*/
private DataPermissionProperties dataPermission;

/**
* 分页插件配置
*/
Expand All @@ -69,25 +64,6 @@ public class MyBatisPlusExtensionProperties {
*/
private boolean blockAttackPluginEnabled = true;

/**
* 数据权限插件配置属性
*/
public static class DataPermissionProperties {

/**
* 是否启用数据权限插件
*/
private boolean enabled = false;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

/**
* 分页插件配置属性
*/
Expand Down Expand Up @@ -170,14 +146,6 @@ public void setIdGenerator(MyBatisPlusIdGeneratorProperties idGenerator) {
this.idGenerator = idGenerator;
}

public DataPermissionProperties getDataPermission() {
return dataPermission;
}

public void setDataPermission(DataPermissionProperties dataPermission) {
this.dataPermission = dataPermission;
}

public PaginationProperties getPagination() {
return pagination;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.*;
import jakarta.annotation.PostConstruct;
import org.mybatis.spring.annotation.MapperScan;
Expand All @@ -37,8 +36,6 @@
import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.core.util.GeneralPropertySourceFactory;
import top.continew.starter.data.mp.autoconfigure.idgenerator.MyBatisPlusIdGeneratorConfiguration;
import top.continew.starter.data.mp.datapermission.DataPermissionFilter;
import top.continew.starter.data.mp.datapermission.DataPermissionHandlerImpl;
import top.continew.starter.data.mp.handler.MybatisBaseEnumTypeHandler;

import java.util.Map;
Expand Down Expand Up @@ -81,13 +78,6 @@ public MybatisPlusInterceptor mybatisPlusInterceptor(MyBatisPlusExtensionPropert
if (!innerInterceptors.isEmpty()) {
innerInterceptors.values().forEach(interceptor::addInnerInterceptor);
}
// 数据权限插件
MyBatisPlusExtensionProperties.DataPermissionProperties dataPermissionProperties = properties
.getDataPermission();
if (null != dataPermissionProperties && dataPermissionProperties.isEnabled()) {
interceptor.addInnerInterceptor(new DataPermissionInterceptor(SpringUtil
.getBean(DataPermissionHandler.class)));
}
// 分页插件
MyBatisPlusExtensionProperties.PaginationProperties paginationProperties = properties.getPagination();
if (null != paginationProperties && paginationProperties.isEnabled()) {
Expand All @@ -112,16 +102,6 @@ public MybatisPlusInterceptor mybatisPlusInterceptor(MyBatisPlusExtensionPropert
MyBatisPlusIdGeneratorConfiguration.Custom.class})
protected static class MyBatisPlusIdGeneratorAutoConfiguration {}

/**
* 数据权限处理器
*/
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledDataPermission
public DataPermissionHandler dataPermissionHandler(DataPermissionFilter dataPermissionFilter) {
return new DataPermissionHandlerImpl(dataPermissionFilter);
}

/**
* 分页插件配置(<a href="https://baomidou.com/pages/97710a/#paginationinnerinterceptor">PaginationInnerInterceptor</a>)
*/
Expand Down
13 changes: 13 additions & 0 deletions continew-starter-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,19 @@
<version>${revision}</version>
</dependency>

<!-- 扩展模块 - 数据权限 - 核心模块 -->
<dependency>
<groupId>top.continew</groupId>
<artifactId>continew-starter-extension-datapermission-core</artifactId>
<version>${revision}</version>
</dependency>
<!-- 扩展模块 - 数据权限 - MyBatis Plus ORM 模块 -->
<dependency>
<groupId>top.continew</groupId>
<artifactId>continew-starter-extension-datapermission-mp</artifactId>
<version>${revision}</version>
</dependency>

<!-- 扩展模块 - 多租户 - 核心模块 -->
<dependency>
<groupId>top.continew</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>top.continew</groupId>
<artifactId>continew-starter-extension-datapermission</artifactId>
<version>${revision}</version>
</parent>

<artifactId>continew-starter-extension-datapermission-core</artifactId>
<description>ContiNew Starter 扩展模块 - 数据权限 - 核心模块</description>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package top.continew.starter.data.mp.datapermission;
package top.continew.starter.extension.datapermission.annotation;

import java.lang.annotation.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package top.continew.starter.extension.datapermission.autoconfigure;

import org.springframework.boot.context.properties.ConfigurationProperties;
import top.continew.starter.core.constant.PropertiesConstants;

/**
* 数据权限配置属性
*
* @author Charles7c
* @since 2.7.0
*/
@ConfigurationProperties(PropertiesConstants.DATA_PERMISSION)
public class DataPermissionProperties {

/**
* 是否启用多租户
*/
private boolean enabled = true;

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package top.continew.starter.data.mp.datapermission;
package top.continew.starter.extension.datapermission.enums;

/**
* 数据权限枚举
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
* limitations under the License.
*/

package top.continew.starter.data.mp.datapermission;
package top.continew.starter.extension.datapermission.filter;

import top.continew.starter.extension.datapermission.model.UserContext;

/**
* 数据权限过滤器接口
* 数据权限用户上下文提供者
*
* @author Charles7c
* @since 1.1.0
*/
public interface DataPermissionFilter {
public interface DataPermissionUserContextProvider {

/**
* 是否过滤
Expand All @@ -32,9 +34,9 @@ public interface DataPermissionFilter {
boolean isFilter();

/**
* 获取当前用户信息
* 获取用户上下文
*
* @return 当前用户信息
* @return 用户上下文
*/
DataPermissionCurrentUser getCurrentUser();
UserContext getUserContext();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package top.continew.starter.extension.datapermission.model;

import top.continew.starter.extension.datapermission.enums.DataScope;

/**
* 角色上下文
*
* @author Charles7c
* @since 1.1.0
*/
public class RoleContext {

/**
* 角色 ID
*/
private String roleId;

/**
* 数据权限
*/
private DataScope dataScope;

public RoleContext() {
}

public RoleContext(String roleId, DataScope dataScope) {
this.roleId = roleId;
this.dataScope = dataScope;
}

public String getRoleId() {
return roleId;
}

public void setRoleId(String roleId) {
this.roleId = roleId;
}

public DataScope getDataScope() {
return dataScope;
}

public void setDataScope(DataScope dataScope) {
this.dataScope = dataScope;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
* limitations under the License.
*/

package top.continew.starter.data.mp.datapermission;
package top.continew.starter.extension.datapermission.model;

import java.util.Set;

/**
* 当前用户信息
* 用户上下文
*
* @author Charles7c
* @since 1.1.0
*/
public class DataPermissionCurrentUser {
public class UserContext {

/**
* 用户 ID
Expand All @@ -34,53 +34,13 @@ public class DataPermissionCurrentUser {
/**
* 角色列表
*/
private Set<CurrentUserRole> roles;
private Set<RoleContext> roles;

/**
* 部门 ID
*/
private String deptId;

/**
* 当前用户角色信息
*/
public static class CurrentUserRole {

/**
* 角色 ID
*/
private String roleId;

/**
* 数据权限
*/
private DataScope dataScope;

public CurrentUserRole() {
}

public CurrentUserRole(String roleId, DataScope dataScope) {
this.roleId = roleId;
this.dataScope = dataScope;
}

public String getRoleId() {
return roleId;
}

public void setRoleId(String roleId) {
this.roleId = roleId;
}

public DataScope getDataScope() {
return dataScope;
}

public void setDataScope(DataScope dataScope) {
this.dataScope = dataScope;
}
}

public String getUserId() {
return userId;
}
Expand All @@ -89,11 +49,11 @@ public void setUserId(String userId) {
this.userId = userId;
}

public Set<CurrentUserRole> getRoles() {
public Set<RoleContext> getRoles() {
return roles;
}

public void setRoles(Set<CurrentUserRole> roles) {
public void setRoles(Set<RoleContext> roles) {
this.roles = roles;
}

Expand Down
Loading

0 comments on commit 7666d56

Please sign in to comment.