¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: Chill åºéª (smallchill@163.com) |
| | | */ |
| | | package org.springblade.core.tenant; |
| | | |
| | | import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfo; |
| | | import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; |
| | | import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import net.sf.jsqlparser.expression.Expression; |
| | | import net.sf.jsqlparser.expression.StringValue; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tenant.annotation.TableExclude; |
| | | import org.springblade.core.tool.constant.BladeConstant; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.core.tool.utils.SpringUtil; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.springframework.beans.factory.SmartInitializingSingleton; |
| | | import org.springframework.context.ApplicationContext; |
| | | |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * ç§æ·ä¿¡æ¯å¤çå¨ |
| | | * |
| | | * @author Chill, L.cm |
| | | */ |
| | | @Slf4j |
| | | @RequiredArgsConstructor |
| | | public class BladeTenantHandler implements TenantLineHandler, SmartInitializingSingleton { |
| | | /** |
| | | * å¹é
çå¤ç§æ·è¡¨ |
| | | */ |
| | | private final List<String> tenantTableList = new ArrayList<>(); |
| | | /** |
| | | * éè¦æé¤è¿è¡èªå®ä¹çå¤ç§æ·è¡¨ |
| | | */ |
| | | private final List<String> excludeTableList = Arrays.asList("blade_user", "blade_dept", "blade_role", "blade_tenant", "act_de_model"); |
| | | /** |
| | | * å¤ç§æ·é
ç½® |
| | | */ |
| | | private final BladeTenantProperties tenantProperties; |
| | | |
| | | /** |
| | | * è·åç§æ·ID |
| | | * |
| | | * @return ç§æ·ID |
| | | */ |
| | | @Override |
| | | public Expression getTenantId() { |
| | | return new StringValue(Func.toStr(AuthUtil.getTenantId(), BladeConstant.ADMIN_TENANT_ID)); |
| | | } |
| | | |
| | | /** |
| | | * è·åç§æ·å段åç§° |
| | | * |
| | | * @return ç§æ·å段åç§° |
| | | */ |
| | | @Override |
| | | public String getTenantIdColumn() { |
| | | return tenantProperties.getColumn(); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®è¡¨å夿æ¯å¦å¿½ç¥æ¼æ¥å¤ç§æ·æ¡ä»¶ |
| | | * é»è®¤é½è¦è¿è¡è§£æå¹¶æ¼æ¥å¤ç§æ·æ¡ä»¶ |
| | | * |
| | | * @param tableName 表å |
| | | * @return æ¯å¦å¿½ç¥, true:表示忽ç¥ï¼false:éè¦è§£æå¹¶æ¼æ¥å¤ç§æ·æ¡ä»¶ |
| | | */ |
| | | @Override |
| | | public boolean ignoreTable(String tableName) { |
| | | if (BladeTenantHolder.isIgnore()) { |
| | | return true; |
| | | } |
| | | return !(tenantTableList.contains(tableName) && StringUtil.isNotBlank(AuthUtil.getTenantId())); |
| | | } |
| | | |
| | | @Override |
| | | public void afterSingletonsInstantiated() { |
| | | ApplicationContext context = SpringUtil.getContext(); |
| | | if (tenantProperties.getAnnotationExclude() && context != null) { |
| | | Map<String, Object> tables = context.getBeansWithAnnotation(TableExclude.class); |
| | | List<String> excludeTables = tenantProperties.getExcludeTables(); |
| | | for (Object o : tables.values()) { |
| | | TableExclude annotation = o.getClass().getAnnotation(TableExclude.class); |
| | | String value = annotation.value(); |
| | | excludeTables.add(value); |
| | | } |
| | | } |
| | | List<TableInfo> tableInfos = TableInfoHelper.getTableInfos(); |
| | | tableFor: |
| | | for (TableInfo tableInfo : tableInfos) { |
| | | String tableName = tableInfo.getTableName(); |
| | | if (tenantProperties.getExcludeTables().contains(tableName) || |
| | | excludeTableList.contains(tableName.toLowerCase()) || |
| | | excludeTableList.contains(tableName.toUpperCase())) { |
| | | continue; |
| | | } |
| | | List<TableFieldInfo> fieldList = tableInfo.getFieldList(); |
| | | for (TableFieldInfo fieldInfo : fieldList) { |
| | | String column = fieldInfo.getColumn(); |
| | | if (tenantProperties.getColumn().equals(column)) { |
| | | tenantTableList.add(tableName); |
| | | continue tableFor; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |