1. 整体去除租户相关概念
This commit is contained in:
parent
e9328b4542
commit
7ece014b7a
|
|
@ -120,20 +120,4 @@ public class JwtUtils
|
|||
{
|
||||
return Convert.toStr(claims.get(key), "");
|
||||
}
|
||||
|
||||
public static String getTenantId(Claims claims) {
|
||||
return getValue(claims, SecurityConstants.TENANT_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据令牌获取用户ID
|
||||
*
|
||||
* @param token 令牌
|
||||
* @return 用户ID
|
||||
*/
|
||||
public static String getTenantId(String token)
|
||||
{
|
||||
Claims claims = parseToken(token);
|
||||
return getValue(claims, SecurityConstants.TENANT_ID);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
package com.chushang.datascope.annotation;
|
||||
|
||||
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
|
||||
import com.chushang.datascope.enums.ScopeKeyWord;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Target(value = {ElementType.METHOD})
|
||||
|
|
@ -18,4 +21,7 @@ public @interface DataScope
|
|||
String userAlias() default "";
|
||||
|
||||
String permission() default "";
|
||||
|
||||
ScopeKeyWord scopeKey() default ScopeKeyWord.AND;
|
||||
|
||||
}
|
||||
|
|
@ -7,7 +7,9 @@ import com.chushang.common.mybatis.page.CommonParam;
|
|||
import com.chushang.common.core.util.StringUtils;
|
||||
import com.chushang.common.mybatis.base.BaseEntity;
|
||||
import com.chushang.datascope.annotation.DataScope;
|
||||
import com.chushang.datascope.constants.ScopeConstants;
|
||||
import com.chushang.datascope.entity.DataScopeEntity;
|
||||
import com.chushang.datascope.enums.ScopeKeyWord;
|
||||
import com.chushang.security.context.SecurityContextHolder;
|
||||
import com.chushang.security.utils.SecurityUtils;
|
||||
import com.chushang.security.entity.vo.LoginUser;
|
||||
|
|
@ -21,43 +23,13 @@ import org.springframework.stereotype.Component;
|
|||
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Aspect
|
||||
@Slf4j
|
||||
@Component
|
||||
public class DataScopeAspect {
|
||||
/**
|
||||
* 数据权限过滤关键字
|
||||
*/
|
||||
public static final String DATA_SCOPE = "dataScope";
|
||||
/**
|
||||
* 部门角色权限过滤
|
||||
*/
|
||||
public static final String ROLE_SCOPE = "roleScope";
|
||||
/**
|
||||
* 全部数据权限
|
||||
*/
|
||||
public static final String DATA_SCOPE_ALL = "1";
|
||||
/**
|
||||
* 自定数据权限
|
||||
*/
|
||||
public static final String DATA_SCOPE_CUSTOM = "2";
|
||||
/**
|
||||
* 部门数据权限
|
||||
*/
|
||||
public static final String DATA_SCOPE_DEPT = "3";
|
||||
/**
|
||||
* 部门及以下数据权限
|
||||
*/
|
||||
public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";
|
||||
/**
|
||||
* 仅本人数据权限
|
||||
*/
|
||||
public static final String DATA_SCOPE_SELF = "5";
|
||||
@Pointcut("@annotation(com.chushang.datascope.annotation.DataScope)")
|
||||
public void dataScopePointCut() {
|
||||
}
|
||||
|
|
@ -86,8 +58,9 @@ public class DataScopeAspect {
|
|||
method.getAnnotation(DataScope.class);
|
||||
String permission = StringUtils.defaultIfEmpty(dataScope.permission(),
|
||||
SecurityContextHolder.getPermission());
|
||||
ScopeKeyWord scopeKeyWord = dataScope.scopeKey();
|
||||
dataScopeFilter(joinPoint, scopes, dataScope.deptAlias(),
|
||||
dataScope.userAlias(), permission, userId);
|
||||
dataScope.userAlias(), permission, userId, scopeKeyWord);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -101,7 +74,7 @@ public class DataScopeAspect {
|
|||
* @param userAlias 用户别名
|
||||
* @param userId 用户id
|
||||
*/
|
||||
public void dataScopeFilter(JoinPoint joinPoint, List<DataScopeEntity> dataScopes, String deptAlias, String userAlias, String permission, Long userId) {
|
||||
public void dataScopeFilter(JoinPoint joinPoint, List<DataScopeEntity> dataScopes, String deptAlias, String userAlias, String permission, Long userId, ScopeKeyWord scopeKeyWord) {
|
||||
StringBuilder dataScopeSqlString = new StringBuilder();
|
||||
// 根据部门过滤role 显示
|
||||
StringBuilder roleSqlString = new StringBuilder();
|
||||
|
|
@ -109,46 +82,72 @@ public class DataScopeAspect {
|
|||
for (DataScopeEntity scope : dataScopes) {
|
||||
String dataScope = scope.getScope();
|
||||
Set<String> permissions = scope.getPermissions();
|
||||
if (!DATA_SCOPE_CUSTOM.equals(dataScope) && conditions.contains(dataScope)) {
|
||||
// 自定义
|
||||
if (!ScopeConstants.DATA_SCOPE_CUSTOM.equals(dataScope) && conditions.contains(dataScope)) {
|
||||
continue;
|
||||
}
|
||||
if (StringUtils.isNotEmpty(permission) && CollectionUtil.isNotEmpty(permissions)
|
||||
&& !CollectionUtil.containsAny(permissions, Convert.toList(permission))) {
|
||||
continue;
|
||||
}
|
||||
if (DATA_SCOPE_ALL.equals(dataScope)) {
|
||||
// 全部
|
||||
if (ScopeConstants.DATA_SCOPE_ALL.equals(dataScope)) {
|
||||
dataScopeSqlString = new StringBuilder();
|
||||
break;
|
||||
} else if (DATA_SCOPE_CUSTOM.equals(dataScope)) {
|
||||
dataScopeSqlString.append(StringUtils.format(
|
||||
" OR {}.dept_id IN ( SELECT dept_id FROM sys_role_dept WHERE role_id = {} ) ", deptAlias,
|
||||
scope.getRoleId()));
|
||||
} else if (DATA_SCOPE_DEPT.equals(dataScope)) {
|
||||
}
|
||||
// 自定义
|
||||
else if (ScopeConstants.DATA_SCOPE_CUSTOM.equals(dataScope)) {
|
||||
// 这个时候就会报错
|
||||
dataScopeSqlString
|
||||
.append(ScopeKeyWord.OR.getCode())
|
||||
.append(StringUtils.format(
|
||||
"{}.dept_id IN ({}) ", deptAlias, String.join(",",scope.getDeptIds())));
|
||||
}
|
||||
// 部门数据
|
||||
else if (ScopeConstants.DATA_SCOPE_DEPT.equals(dataScope)) {
|
||||
dataScopeSqlString.append(StringUtils.format(" OR {}.dept_id = {} ", deptAlias, scope.getDeptId()));
|
||||
|
||||
} else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope)) {
|
||||
dataScopeSqlString.append(StringUtils.format(
|
||||
" OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors ) )",
|
||||
deptAlias, scope.getDeptId(), scope.getDeptId()));
|
||||
// 当且仅当用在角色列表页面, 并且角色为 部门及以下
|
||||
roleSqlString.append(StringUtils.format(" OR {}.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = {} OR find_in_set( {}, ancestors ) ) ",
|
||||
"r", scope.getDeptId(), scope.getDeptId()));
|
||||
} else if (DATA_SCOPE_SELF.equals(dataScope)) {
|
||||
}
|
||||
// 部门及以下
|
||||
else if (ScopeConstants.DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope)) {
|
||||
dataScopeSqlString
|
||||
.append(ScopeKeyWord.OR.getCode())
|
||||
.append(StringUtils.format(
|
||||
"{}.dept_id IN ({})",
|
||||
deptAlias, String.join(",",scope.getDeptIds())));
|
||||
// 当且仅当用在角色列表页面, 并且角色为 部门及以下 -- 用于控制 角色的显示, 每个部门下有自己的角色不同
|
||||
roleSqlString
|
||||
.append(ScopeKeyWord.OR.getCode())
|
||||
.append(StringUtils.format("{}.dept_id IN ({}) ",
|
||||
"r", String.join(",",scope.getDeptIds())));
|
||||
}
|
||||
// 仅本人
|
||||
else if (ScopeConstants.DATA_SCOPE_SELF.equals(dataScope)) {
|
||||
if (StringUtils.isNotBlank(userAlias)) {
|
||||
dataScopeSqlString.append(StringUtils.format(" OR {}.user_id = {} ", userAlias, userId));
|
||||
dataScopeSqlString
|
||||
.append(ScopeKeyWord.OR.getCode())
|
||||
.append(StringUtils.format("{}.user_id = {} ", userAlias, userId));
|
||||
} else {
|
||||
// 数据权限为仅本人且没有userAlias别名不查询任何数据
|
||||
dataScopeSqlString.append(StringUtils.format(" OR {}.dept_id = {} ", deptAlias, scope.getDeptId()));
|
||||
dataScopeSqlString
|
||||
.append(ScopeKeyWord.OR.getCode())
|
||||
.append(StringUtils.format("{}.dept_id = {} ", deptAlias, scope.getDeptId()));
|
||||
}
|
||||
}
|
||||
conditions.add(dataScope);
|
||||
}
|
||||
if (StringUtils.isNotBlank(dataScopeSqlString.toString())) {
|
||||
String v = " AND (" + dataScopeSqlString.substring(4) + ")";
|
||||
String v;
|
||||
if (scopeKeyWord.equals(ScopeKeyWord.AND)){
|
||||
v = ScopeKeyWord.AND.getCode();
|
||||
}else {
|
||||
v = ScopeKeyWord.OR.getCode();
|
||||
}
|
||||
// 会将 最前面的 OR 去除
|
||||
v += "(" + dataScopeSqlString.substring(4) + ")";
|
||||
Map<String, Object> sqlParam = getSqlParam(joinPoint);
|
||||
if (null == sqlParam) return;
|
||||
sqlParam.put(DATA_SCOPE, v);
|
||||
sqlParam.put(ROLE_SCOPE, roleSqlString.toString());
|
||||
sqlParam.put(ScopeConstants.DATA_SCOPE, v);
|
||||
sqlParam.put(ScopeConstants.ROLE_SCOPE, roleSqlString.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -158,8 +157,8 @@ public class DataScopeAspect {
|
|||
private void clearDataScope(final JoinPoint joinPoint) {
|
||||
Map<String, Object> sqlParam = getSqlParam(joinPoint);
|
||||
if (null == sqlParam) return;
|
||||
sqlParam.put(DATA_SCOPE, "");
|
||||
sqlParam.put(ROLE_SCOPE, "");
|
||||
sqlParam.put(ScopeConstants.DATA_SCOPE, "");
|
||||
sqlParam.put(ScopeConstants.ROLE_SCOPE, "");
|
||||
}
|
||||
|
||||
private Map<String, Object> getSqlParam(final JoinPoint joinPoint){
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
package com.chushang.datascope.constants;
|
||||
|
||||
public interface ScopeConstants {
|
||||
/**
|
||||
* 全部数据权限
|
||||
*/
|
||||
String DATA_SCOPE_ALL = "1";
|
||||
/**
|
||||
* 自定数据权限
|
||||
*/
|
||||
String DATA_SCOPE_CUSTOM = "2";
|
||||
/**
|
||||
* 部门数据权限
|
||||
*/
|
||||
String DATA_SCOPE_DEPT = "3";
|
||||
/**
|
||||
* 部门及以下数据权限
|
||||
*/
|
||||
String DATA_SCOPE_DEPT_AND_CHILD = "4";
|
||||
/**
|
||||
* 仅本人数据权限
|
||||
*/
|
||||
String DATA_SCOPE_SELF = "5";
|
||||
|
||||
/**
|
||||
* 数据权限过滤关键字
|
||||
*/
|
||||
String DATA_SCOPE = "dataScope";
|
||||
/**
|
||||
* 部门角色权限过滤
|
||||
*/
|
||||
String ROLE_SCOPE = "roleScope";
|
||||
}
|
||||
|
|
@ -25,5 +25,14 @@ public class DataScopeEntity{
|
|||
private String scope;
|
||||
|
||||
private Set<String> permissions;
|
||||
/**
|
||||
* 用于权限控制 -- 部门及以下
|
||||
* DATA_SCOPE_DEPT_AND_CHILD
|
||||
* SELECT dept_id FROM sys_dept WHERE dept_id = {} or find_in_set( {} , ancestors )
|
||||
*
|
||||
* DATA_SCOPE_CUSTOM
|
||||
* SELECT dept_id FROM sys_role_dept WHERE role_id = {}
|
||||
*/
|
||||
private Set<String> deptIds;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
package com.chushang.datascope.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* scope 关键词连接
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum ScopeKeyWord {
|
||||
AND(" AND "),
|
||||
OR(" OR "),
|
||||
;
|
||||
|
||||
private final String code;
|
||||
}
|
||||
|
|
@ -80,27 +80,6 @@ public class MybatisAutoConfiguration implements WebMvcConfigurer {
|
|||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
/**
|
||||
* 新多租户插件配置,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存万一出现问题
|
||||
*/
|
||||
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
String tenantId = ServletUtils.getRequest().getHeader(SecurityConstants.TENANT_ID);
|
||||
// 租户id, 应当就是其
|
||||
return new LongValue(tenantId);
|
||||
}
|
||||
|
||||
// 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
|
||||
@Override
|
||||
public boolean ignoreTable(String tableName) {
|
||||
log.info("tableName {}", tableName);
|
||||
String tenantId = ServletUtils.getRequest().getHeader(SecurityConstants.TENANT_ID);
|
||||
// 租户id 为空不拼接租户, 否则拼接租户id
|
||||
return StringUtils.isEmpty(tenantId);
|
||||
}
|
||||
}));
|
||||
|
||||
// 分页插件, 对于单一数据库类型来说,都建议配置该值,避免每次分页都去抓取数据库类型
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
// 乐观锁
|
||||
|
|
|
|||
|
|
@ -32,10 +32,8 @@ public class InnerAuthAspect implements Ordered
|
|||
|
||||
String userid = ServletUtils.getRequest().getHeader(SecurityConstants.DETAILS_USER_ID);
|
||||
String username = ServletUtils.getRequest().getHeader(SecurityConstants.DETAILS_USERNAME);
|
||||
// 设置租户id
|
||||
String tenantId = ServletUtils.getRequest().getHeader(SecurityConstants.TENANT_ID);
|
||||
// 用户信息验证
|
||||
if (innerAuth.isUser() && (StringUtils.isEmpty(userid) || StringUtils.isEmpty(username) || StringUtils.isEmpty(tenantId)))
|
||||
if (innerAuth.isUser() && (StringUtils.isEmpty(userid) || StringUtils.isEmpty(username)))
|
||||
{
|
||||
throw new InnerAuthException("没有设置用户信息,不允许访问 ");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -96,11 +96,4 @@ public class SecurityContextHolder
|
|||
{
|
||||
set(SecurityConstants.ROLE_PERMISSION, permissions);
|
||||
}
|
||||
|
||||
public static void setTenantId(String tenantId){
|
||||
set(SecurityConstants.TENANT_ID, tenantId);
|
||||
}
|
||||
public static String getTenantId(){
|
||||
return get(SecurityConstants.TENANT_ID);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,10 +62,6 @@ public class LoginUser<T, D> implements Serializable
|
|||
* 用户信息
|
||||
*/
|
||||
private T sysUser;
|
||||
/**
|
||||
* 租户ID
|
||||
*/
|
||||
private Long tenantId;
|
||||
/**
|
||||
* 当前用户对应的 数据权限
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -47,11 +47,6 @@ public class FeignRequestInterceptor implements RequestInterceptor
|
|||
{
|
||||
requestTemplate.header(SecurityConstants.AUTHORIZATION_HEADER, authentication);
|
||||
}
|
||||
// 租户id
|
||||
String tenantId = headers.get(SecurityConstants.TENANT_ID);
|
||||
if (StringUtils.isNotEmpty(tenantId)){
|
||||
requestTemplate.header(SecurityConstants.TENANT_ID, tenantId);
|
||||
}
|
||||
// 配置客户端IP
|
||||
requestTemplate.header("X-Forwarded-For", IPUtils.clientIp(ServletUtils.getRequest()));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ public class HeaderInterceptor implements AsyncHandlerInterceptor
|
|||
SecurityContextHolder.setUserId(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USER_ID));
|
||||
SecurityContextHolder.setUserName(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USERNAME));
|
||||
SecurityContextHolder.setUserKey(ServletUtils.getHeader(request, SecurityConstants.USER_KEY));
|
||||
SecurityContextHolder.setTenantId(ServletUtils.getHeader(request, SecurityConstants.TENANT_ID));
|
||||
|
||||
String token = SecurityUtils.getToken();
|
||||
if (StringUtils.isNotEmpty(token))
|
||||
|
|
|
|||
|
|
@ -44,7 +44,6 @@ public class TokenService
|
|||
String token = IdUtils.getId(31);
|
||||
Long userId = loginUser.getUserId();
|
||||
String username = loginUser.getUsername();
|
||||
Long tenantId = loginUser.getTenantId();
|
||||
String tokenKey = token + "#" + userId;
|
||||
loginUser.setToken(tokenKey);
|
||||
loginUser.setIpaddr(IPUtils.clientIp(ServletUtils.getRequest()));
|
||||
|
|
@ -56,8 +55,6 @@ public class TokenService
|
|||
claimsMap.put(SecurityConstants.USER_KEY, tokenKey);
|
||||
claimsMap.put(SecurityConstants.DETAILS_USER_ID, userId);
|
||||
claimsMap.put(SecurityConstants.DETAILS_USERNAME, username);
|
||||
// 租户id
|
||||
claimsMap.put(SecurityConstants.TENANT_ID, tenantId);
|
||||
|
||||
// 接口返回信息
|
||||
Map<String, Object> rspMap = new HashMap<>();
|
||||
|
|
|
|||
|
|
@ -65,7 +65,6 @@ public class UserService {
|
|||
}
|
||||
recordLoginInfo(username, LoginStatusEnum.LOGIN_SUCCESS, "登录成功");
|
||||
loginUser.setSysUser(sysUser);
|
||||
loginUser.setTenantId(sysUser.getTenantId());
|
||||
return loginUser;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -84,7 +84,6 @@ public class AuthFilter implements GlobalFilter, Ordered
|
|||
}
|
||||
String userid = JwtUtils.getUserId(claims);
|
||||
String username = JwtUtils.getUserName(claims);
|
||||
String tenantId = JwtUtils.getTenantId(claims);
|
||||
if (StringUtils.isEmpty(userid) || StringUtils.isEmpty(username))
|
||||
{
|
||||
return unauthorizedResponse(exchange, "令牌验证失败");
|
||||
|
|
@ -94,8 +93,6 @@ public class AuthFilter implements GlobalFilter, Ordered
|
|||
addHeader(mutate, SecurityConstants.USER_KEY, userkey);
|
||||
addHeader(mutate, SecurityConstants.DETAILS_USER_ID, userid);
|
||||
addHeader(mutate, SecurityConstants.DETAILS_USERNAME, username);
|
||||
// 租户Id
|
||||
addHeader(mutate, SecurityConstants.TENANT_ID, tenantId);
|
||||
// 内部请求来源参数清除
|
||||
removeHeader(mutate);
|
||||
|
||||
|
|
|
|||
|
|
@ -70,9 +70,4 @@ public class SysConfig extends BaseEntity
|
|||
@ExcelProperty(value = "修改人", index = 6)
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 租户Id
|
||||
*/
|
||||
@TableField(value = "tenant_id")
|
||||
private Long tenantId;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -73,9 +73,4 @@ public class SysDept extends BaseEntity
|
|||
)
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 租户Id
|
||||
*/
|
||||
@TableField(value = "tenant_id")
|
||||
private Long tenantId;
|
||||
}
|
||||
|
|
@ -120,10 +120,4 @@ public class SysRole extends BaseEntity {
|
|||
this.roleId = roleId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 租户Id
|
||||
*/
|
||||
@TableField(value = "tenant_id")
|
||||
private Long tenantId;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,55 +0,0 @@
|
|||
package com.chushang.system.entity.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.chushang.common.mybatis.base.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* 租户表
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
@TableName("sys_tenant")
|
||||
public class SysTenant extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 租户id
|
||||
*/
|
||||
@TableId(value = "tenant_id", type = IdType.ASSIGN_ID)
|
||||
private Long tenantId;
|
||||
/**
|
||||
* 租户名称
|
||||
*/
|
||||
private String tenantName;
|
||||
/**
|
||||
* 租户状态
|
||||
*/
|
||||
private Boolean status;
|
||||
/**
|
||||
* 租户套餐Id
|
||||
*/
|
||||
private Long packageId;
|
||||
|
||||
/**
|
||||
* 租户排序
|
||||
*/
|
||||
private Integer orderNum;
|
||||
/**
|
||||
* 创建人角色
|
||||
*/
|
||||
@TableField(updateStrategy = FieldStrategy.NOT_NULL)
|
||||
private String createBy;
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
@TableField(updateStrategy = FieldStrategy.NOT_NULL)
|
||||
private String updateBy;
|
||||
/**
|
||||
* 租户套餐
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private SysTenantPackage tenantPackage;
|
||||
}
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
package com.chushang.system.entity.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.chushang.common.mybatis.base.BaseEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 租户套餐表
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
@TableName("sys_tenant_package")
|
||||
public class SysTenantPackage extends BaseEntity {
|
||||
/**
|
||||
* 套餐Id
|
||||
*/
|
||||
@TableId(value = "package_id", type = IdType.ASSIGN_ID)
|
||||
private Long packageId;
|
||||
/**
|
||||
* 套餐名称
|
||||
*/
|
||||
private String packageName;
|
||||
/**
|
||||
* 备注信息
|
||||
*/
|
||||
private String remark;
|
||||
/**
|
||||
* 套餐开始时间
|
||||
*/
|
||||
private LocalDate startDate;
|
||||
/**
|
||||
* 套餐结束时间
|
||||
*/
|
||||
private LocalDate endDate;
|
||||
/**
|
||||
* 套餐排序
|
||||
*/
|
||||
private Integer orderNum;
|
||||
|
||||
/**
|
||||
* 创建人角色
|
||||
*/
|
||||
@TableField(updateStrategy = FieldStrategy.NOT_NULL)
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
@TableField(updateStrategy = FieldStrategy.NOT_NULL)
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 套餐状态
|
||||
*/
|
||||
private Boolean status;
|
||||
|
||||
/**
|
||||
* 判断租户是否在有效期
|
||||
*/
|
||||
public boolean isValid(){
|
||||
LocalDate now = LocalDate.now();
|
||||
return (this.startDate.isAfter(now) && this.endDate.isBefore(now) && status);
|
||||
}
|
||||
}
|
||||
|
|
@ -134,10 +134,4 @@ public class SysUser extends BaseEntity {
|
|||
this.userId = userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 租户Id
|
||||
*/
|
||||
@TableField(value = "tenant_id")
|
||||
private Long tenantId;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|||
import com.chushang.system.entity.dto.ListDeptDTO;
|
||||
import com.chushang.system.entity.po.SysDept;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -23,4 +24,10 @@ public interface SysDeptMapper extends BaseMapper<SysDept> {
|
|||
@Param("deptCheckStrictly") boolean deptCheckStrictly);
|
||||
|
||||
void updateDeptChildren(@Param("depts") List<SysDept> depts);
|
||||
|
||||
@Select("SELECT dept_id FROM sys_role_dept WHERE role_id = #{roleId}")
|
||||
List<String> listScopeDeptIdByRoleId(@Param("roleId") Long roleId);
|
||||
|
||||
@Select("SELECT dept_id FROM sys_dept WHERE dept_id = #{deptId} or find_in_set( #{deptId} , ancestors )")
|
||||
List<String> listScopeDeptIdByDeptId(@Param("deptId") Long deptId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
package com.chushang.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.chushang.system.entity.po.SysTenant;
|
||||
|
||||
/**
|
||||
* 租户
|
||||
*/
|
||||
public interface SysTenantMapper extends BaseMapper<SysTenant> {
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
package com.chushang.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.chushang.system.entity.po.SysTenantPackage;
|
||||
|
||||
/**
|
||||
* 租户套餐
|
||||
*/
|
||||
public interface SysTenantPackageMapper extends BaseMapper<SysTenantPackage> {
|
||||
}
|
||||
|
|
@ -6,11 +6,16 @@ import com.chushang.common.core.exception.ResultException;
|
|||
import com.chushang.common.core.text.Convert;
|
||||
import com.chushang.common.core.util.StringUtils;
|
||||
import com.chushang.common.mybatis.enums.Operator;
|
||||
import com.chushang.datascope.constants.ScopeConstants;
|
||||
import com.chushang.datascope.enums.ScopeKeyWord;
|
||||
import com.chushang.system.entity.dto.ListDeptDTO;
|
||||
import com.chushang.system.entity.po.SysDept;
|
||||
import com.chushang.system.entity.vo.TreeSelect;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
|
@ -119,4 +124,8 @@ public interface ISysDeptService extends IService<SysDept> {
|
|||
}
|
||||
|
||||
void updateDeptChildren(List<SysDept> children);
|
||||
|
||||
List<String> listScopeDeptIdByRoleId(Long roleId);
|
||||
|
||||
List<String> listScopeDeptIdByDeptId(Long deptId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,21 +17,5 @@ public interface ISysPermissionService {
|
|||
|
||||
Set<String> getMenuPermission(SysUser sysUser);
|
||||
|
||||
default List<DataScopeEntity> getDataScopes(SysUser sysUser){
|
||||
List<SysRole> roles = sysUser.getRoles();
|
||||
return roles.stream().map(role -> {
|
||||
String dataScope = role.getDataScope();
|
||||
Long roleId = role.getRoleId();
|
||||
Long deptId = sysUser.getDeptId();
|
||||
Long userId = sysUser.getUserId();
|
||||
Set<String> permissions = role.getPermissions();
|
||||
return DataScopeEntity.builder()
|
||||
.scope(dataScope)
|
||||
.roleId(roleId)
|
||||
.userId(userId)
|
||||
.deptId(deptId)
|
||||
.permissions(permissions)
|
||||
.build();
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
List<DataScopeEntity> getDataScopes(SysUser sysUser);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
package com.chushang.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.chushang.system.entity.po.SysTenantPackage;
|
||||
|
||||
public interface SysTenantPackageService extends IService<SysTenantPackage> {
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
package com.chushang.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.chushang.system.entity.po.SysTenant;
|
||||
|
||||
public interface SysTenantService extends IService<SysTenant> {
|
||||
}
|
||||
|
|
@ -106,6 +106,16 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||
baseMapper.updateDeptChildren(children);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> listScopeDeptIdByRoleId(Long roleId) {
|
||||
return baseMapper.listScopeDeptIdByRoleId(roleId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> listScopeDeptIdByDeptId(Long deptId) {
|
||||
return baseMapper.listScopeDeptIdByDeptId(deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归列表
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,29 +1,33 @@
|
|||
package com.chushang.system.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.chushang.datascope.constants.ScopeConstants;
|
||||
import com.chushang.datascope.entity.DataScopeEntity;
|
||||
import com.chushang.security.auth.AuthUtil;
|
||||
import com.chushang.system.entity.po.SysRole;
|
||||
import com.chushang.system.entity.po.SysUser;
|
||||
import com.chushang.system.service.ISysDeptService;
|
||||
import com.chushang.system.service.ISysMenuService;
|
||||
import com.chushang.system.service.ISysPermissionService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author by zhaowenyuan create 2022/8/19 09:43
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class SysPermissionServiceImpl implements ISysPermissionService {
|
||||
|
||||
@Autowired
|
||||
private ISysMenuService menuService;
|
||||
private final ISysMenuService menuService;
|
||||
private final ISysDeptService deptService;
|
||||
|
||||
@Override
|
||||
public Set<String> getRolePermission(SysUser sysUser) {
|
||||
|
|
@ -70,4 +74,39 @@ public class SysPermissionServiceImpl implements ISysPermissionService {
|
|||
return perms;
|
||||
}
|
||||
|
||||
/**
|
||||
* 这个会不会慢? 因为是 for 循环
|
||||
*/
|
||||
public List<DataScopeEntity> getDataScopes(SysUser sysUser){
|
||||
List<SysRole> roles = sysUser.getRoles();
|
||||
return roles.parallelStream().map(role -> {
|
||||
// 用于角色 控制,
|
||||
String dataScope = role.getDataScope();
|
||||
Long roleId = role.getRoleId();
|
||||
Long deptId = sysUser.getDeptId();
|
||||
Long userId = sysUser.getUserId();
|
||||
Set<String> permissions = role.getPermissions();
|
||||
Set<String> deptIds = new HashSet<>();
|
||||
// 需要roleId
|
||||
if (ScopeConstants.DATA_SCOPE_CUSTOM.equals(dataScope)) {
|
||||
List<String> longs = deptService.listScopeDeptIdByRoleId(roleId);
|
||||
deptIds.addAll(longs);
|
||||
}
|
||||
// 部门及以下
|
||||
else if (ScopeConstants.DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope)) {
|
||||
List<String> longs = deptService.listScopeDeptIdByDeptId(deptId);
|
||||
deptIds.addAll(longs);
|
||||
}
|
||||
// 部门 为空时, 不让其看到全部的, -1 的部门id 必定不存在
|
||||
if (CollectionUtil.isEmpty(deptIds)) deptIds.add("-1");
|
||||
return DataScopeEntity.builder()
|
||||
.scope(dataScope)
|
||||
.roleId(roleId)
|
||||
.userId(userId)
|
||||
.deptId(deptId)
|
||||
.permissions(permissions)
|
||||
.deptIds(deptIds)
|
||||
.build();
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
package com.chushang.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.chushang.system.entity.po.SysTenantPackage;
|
||||
import com.chushang.system.mapper.SysTenantPackageMapper;
|
||||
import com.chushang.system.service.SysTenantPackageService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SysTenantPackageServiceImpl extends ServiceImpl<SysTenantPackageMapper, SysTenantPackage> implements SysTenantPackageService {
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
package com.chushang.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.chushang.system.entity.po.SysTenant;
|
||||
import com.chushang.system.mapper.SysTenantMapper;
|
||||
import com.chushang.system.service.SysTenantService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant> implements SysTenantService {
|
||||
}
|
||||
Loading…
Reference in New Issue