增加多数据库支持

This commit is contained in:
zhouhao
2019-04-11 10:25:28 +08:00
parent 7cef088219
commit 03a7cf9e20
8 changed files with 73 additions and 42 deletions

View File

@@ -6,6 +6,7 @@ import lombok.Setter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.core.dsl.Query;
import org.hswebframework.ezorm.rdb.render.dialect.Dialect;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.authorization.Permission;
import org.hswebframework.web.authorization.User;
@@ -15,7 +16,6 @@ import org.hswebframework.web.authorization.access.UserAttachEntity;
import org.hswebframework.web.authorization.define.AuthorizingContext;
import org.hswebframework.web.authorization.define.Phased;
import org.hswebframework.web.authorization.exception.AccessDenyException;
import org.hswebframework.web.authorization.exception.UnAuthorizedException;
import org.hswebframework.web.bean.FastBeanCopier;
import org.hswebframework.web.commons.entity.Entity;
import org.hswebframework.web.commons.entity.PagerResult;
@@ -69,6 +69,10 @@ public class ScopeByUserHandler implements DataAccessHandler {
}
}
protected boolean supportChildSqlTerm() {
return Dialect.H2.isSupportTermType("user-in-org");
}
protected boolean doUpdateAccess(ScopeByUserDataAccessConfig config, AuthorizingContext context) {
//获取id参数
Object id = context.getParamContext()
@@ -132,6 +136,7 @@ public class ScopeByUserHandler implements DataAccessHandler {
return scopeInfo;
}
Consumer<Query<?, QueryParamEntity>> consumer;
boolean supportChildTerm = supportChildSqlTerm();
switch (config.getScopeType()) {
case "OWN_PERSON":
@@ -149,50 +154,50 @@ public class ScopeByUserHandler implements DataAccessHandler {
allScope = scope;
break;
case DataAccessType.ORG_SCOPE:
termType = "user-in-org";
personTermType = "person-in-org";
scope = authentication.getRootOrgId();
termType = supportChildTerm ? "user-in-org" : "in";
personTermType = supportChildTerm ? "person-in-org" : "in";
scope = config.isChildren() && !supportChildTerm ? authentication.getAllOrgId() : authentication.getRootOrgId();
allScope = config.isChildren() ? authentication.getAllOrgId() : scope;
break;
case DataAccessType.DEPARTMENT_SCOPE:
termType = "user-in-department";
personTermType = "person-in-department";
scope = authentication.getRootDepartmentId();
termType = supportChildTerm ? "user-in-department" : "in";
personTermType = supportChildTerm ? "person-in-department" : "in";
scope = config.isChildren() && !supportChildTerm ? authentication.getAllDepartmentId() : authentication.getRootDepartmentId();
allScope = config.isChildren() ? authentication.getAllDepartmentId() : scope;
break;
case DataAccessType.POSITION_SCOPE:
termType = "user-in-position";
personTermType = "person-in-position";
scope = authentication.getRootPositionId();
termType = supportChildTerm ? "user-in-position" : "in";
personTermType = supportChildTerm ? "person-in-position" : "in";
scope = config.isChildren() && !supportChildTerm ? authentication.getAllPositionId() : authentication.getRootPositionId();
allScope = config.isChildren() ? authentication.getAllPositionId() : scope;
break;
case DataAccessType.DISTRICT_SCOPE:
termType = "user-in-dist";
personTermType = "person-in-dist";
scope = authentication.getRootDistrictId();
termType = supportChildTerm ? "user-in-dist" : "in";
personTermType = supportChildTerm ? "person-in-dist" : "in";
scope = config.isChildren() && !supportChildTerm ? authentication.getAllDistrictId() : authentication.getRootDistrictId();
allScope = config.isChildren() ? authentication.getAllDistrictId() : scope;
break;
case "CUSTOM_SCOPE_ORG":
termType = "user-in-org";
personTermType = "person-in-org";
termType = supportChildSqlTerm() ? "user-in-org" : "in";
personTermType = supportChildSqlTerm() ? "person-in-org" : "in";
scope = config.getScope();
allScope = scope;
break;
case "CUSTOM_SCOPE_POSITION":
termType = "user-in-position";
personTermType = "person-in-position";
termType = supportChildSqlTerm() ? "user-in-position" : "in";
personTermType = supportChildSqlTerm() ? "person-in-position" : "in";
scope = config.getScope();
allScope = scope;
break;
case "CUSTOM_SCOPE_DEPT":
termType = "user-in-department";
personTermType = "person-in-department";
termType = supportChildSqlTerm() ? "user-in-department" : "in";
personTermType = supportChildSqlTerm() ? "person-in-department" : "in";
scope = config.getScope();
allScope = scope;
break;
case "CUSTOM_SCOPE_DIST":
termType = "user-in-dist";
personTermType = "person-in-dist";
termType = supportChildSqlTerm() ? "user-in-dist" : "in";
personTermType = supportChildSqlTerm() ? "person-in-dist" : "in";
scope = config.getScope();
allScope = scope;
break;
@@ -275,7 +280,7 @@ public class ScopeByUserHandler implements DataAccessHandler {
};
}
// static Map<Class, String> cache = new ConcurrentHashMap<>();
// static Map<Class, String> cache = new ConcurrentHashMap<>();
protected <T> String getControlProperty(Class type, Function<T, String> function) {
return function.apply((T) entityFactory.newInstance(type));
@@ -334,35 +339,35 @@ public class ScopeByUserHandler implements DataAccessHandler {
String property = getControlProperty(entityClass, OrgAttachEntity::getOrgIdProperty);
controllerCache.targetIdGetter = createGetter(OrgAttachEntity.class, OrgAttachEntity::getOrgId);
controllerCache.queryConsumer = (query, scopeInfo) -> {
query.and(property, children ? "org-child-in" : "in", scopeInfo.scope);
query.and(property, children && supportChildSqlTerm() ? "org-child-in" : "in", scopeInfo.scope);
};
//部门
} else if (key.getType().contains("DEPT") && DepartmentAttachEntity.class.isAssignableFrom(entityClass)) {
String property = getControlProperty(entityClass, DepartmentAttachEntity::getDepartmentIdProperty);
controllerCache.targetIdGetter = createGetter(DepartmentAttachEntity.class, DepartmentAttachEntity::getDepartmentId);
controllerCache.queryConsumer = (query, scopeInfo) -> {
query.and(property, children ? "org-child-in" : "in", scopeInfo.scope);
query.and(property, children && supportChildSqlTerm() ? "dept-child-in" : "in", scopeInfo.scope);
};
//岗位
} else if (key.getType().contains("POS") && PositionAttachEntity.class.isAssignableFrom(entityClass)) {
String property = getControlProperty(entityClass, PositionAttachEntity::getPositionIdProperty);
controllerCache.targetIdGetter = createGetter(PositionAttachEntity.class, PositionAttachEntity::getPositionId);
controllerCache.queryConsumer = (query, scopeInfo) -> {
query.and(property, children ? "pos-child-in" : "in", scopeInfo.scope);
query.and(property, children && supportChildSqlTerm() ? "pos-child-in" : "in", scopeInfo.scope);
};
//行政区划
} else if (key.getType().contains("DIST") && DistrictAttachEntity.class.isAssignableFrom(entityClass)) {
String property = getControlProperty(entityClass, DistrictAttachEntity::getDistrictIdProperty);
controllerCache.targetIdGetter = createGetter(DistrictAttachEntity.class, DistrictAttachEntity::getDistrictId);
controllerCache.queryConsumer = (query, scopeInfo) -> {
query.and(property, children ? "dist-child-in" : "in", scopeInfo.scope);
query.and(property, children && supportChildSqlTerm() ? "dist-child-in" : "in", scopeInfo.scope);
};
//人员
} else if (key.getType().contains("PERSON") && PersonAttachEntity.class.isAssignableFrom(entityClass)) {
String property = getControlProperty(entityClass, PersonAttachEntity::getPersonIdProperty);
controllerCache.targetIdGetter = createGetter(PersonAttachEntity.class, PersonAttachEntity::getPersonId);
controllerCache.queryConsumer = (query, scopeInfo) -> {
query.and(property, scopeInfo.termType, scopeInfo.scope);
query.and(property, scopeInfo.personTermType, scopeInfo.scope);
};
//根据用户控制
} else {

View File

@@ -2,6 +2,7 @@ package org.hswebframework.web.service.organizational.simple.terms;
import org.hswebframework.web.commons.entity.TreeSupportEntity;
import org.hswebframework.web.dao.mybatis.mapper.TreeStructureSqlTermCustomizer;
import org.hswebframework.web.datasource.DataSourceHolder;
import org.hswebframework.web.service.QueryService;
import java.util.List;
@@ -29,6 +30,10 @@ public class InServiceTreeInSqlTerm<PK> extends TreeStructureSqlTermCustomizer {
@Override
protected String getTableName() {
String db = DataSourceHolder.databaseSwitcher().currentDatabase();
if (db != null) {
return db.concat(".").concat(tableName);
}
return tableName;
}

View File

@@ -39,9 +39,12 @@ public class UserInDepartmentSqlTerm extends UserInSqlTerm {
Dialect dialect = column.getTableMetaData().getDatabaseMetaData().getDialect();
SqlAppender appender = new SqlAppender();
appender.addSpc(not ? "not" : "", "exists(select 1 from s_person_position _tmp,s_position _pos,s_person _person");
appender.addSpc(not ? "not" : "", "exists(select 1 from ",
getTableFullName("s_person_position")," _tmp,",
getTableFullName("s_position")," _pos,",
getTableFullName("s_person")," _person");
if (isChild() || isParent()) {
appender.addSpc(",s_department _dept");
appender.addSpc(",",getTableFullName("s_department")," _dept");
}
appender.addSpc("where _person.u_id=_tmp.person_id and _tmp.position_id = _pos.u_id and _person.u_id=_tmp.person_id"
, "and", createColumnName(column, tableAlias), "=", isForPerson() ? "_tmp.person_id" : "_person.user_id");

View File

@@ -32,25 +32,31 @@ public class UserInDistSqlTerm extends UserInSqlTerm {
return "_dist";
}
@Override
public SqlAppender accept(String wherePrefix, Term term, RDBColumnMetaData column, String tableAlias) {
ChangedTermValue termValue = createChangedTermValue(term);
Dialect dialect=column.getTableMetaData().getDatabaseMetaData().getDialect();
Dialect dialect = column.getTableMetaData().getDatabaseMetaData().getDialect();
SqlAppender appender = new SqlAppender();
appender.addSpc(not ? "not" : "", "exists(select 1 from s_person_position _tmp,s_position _pos,s_person _person,s_department _dept,s_organization _org");
if (isChild()||isParent()) {
appender.addSpc(",s_district _dist");
appender.addSpc(not ? "not" : "", "exists(select 1 from ",
getTableFullName("s_person_position"), " _tmp,",
getTableFullName("s_position"), " _pos,",
getTableFullName("s_person"), " _person,",
getTableFullName("s_department"), " _dept,",
getTableFullName("s_organization"), " _org");
if (isChild() || isParent()) {
appender.addSpc(",",getTableFullName("s_district")," _dist");
}
appender.addSpc("where _person.u_id=_tmp.person_id and _tmp.position_id = _pos.u_id and _person.u_id=_tmp.person_id and _dept.u_id=_pos.department_id and _org.u_id=_dept.org_id"
, "and", createColumnName(column, tableAlias), "=", isForPerson() ? "_tmp.person_id" : "_person.user_id");
if (isChild()||isParent()) {
if (isChild() || isParent()) {
appender.addSpc("and _org.district_id=_dist.u_id");
}
List<Object> positionIdList = BoostTermTypeMapper.convertList(column, termValue.getOld());
if (!positionIdList.isEmpty()) {
appender.addSpc("and");
termValue.setValue(appendCondition(positionIdList, wherePrefix, appender, "_org.district_id",dialect));
termValue.setValue(appendCondition(positionIdList, wherePrefix, appender, "_org.district_id", dialect));
}
appender.add(")");

View File

@@ -38,9 +38,13 @@ public class UserInOrgSqlTerm extends UserInSqlTerm {
Dialect dialect = column.getTableMetaData().getDatabaseMetaData().getDialect();
SqlAppender appender = new SqlAppender();
appender.addSpc(not ? "not" : "", "exists(select 1 from s_person_position _tmp,s_position _pos,s_department _dept,s_person _person");
appender.addSpc(not ? "not" : "", "exists(select 1 from ",
getTableFullName("s_person_position")," _tmp,",
getTableFullName("s_position")," _pos,",
getTableFullName("s_department")," _dept,",
getTableFullName("s_person")," _person");
if (isChild()||isParent()) {
appender.addSpc(",s_organization _org");
appender.addSpc(",",getTableFullName("s_organization")," _org");
}
appender.addSpc("where _person.u_id=_tmp.person_id and _tmp.position_id = _pos.u_id and _person.u_id=_tmp.person_id and _dept.u_id=_pos.department_id"
, "and", createColumnName(column, tableAlias), "=", isForPerson() ? "_tmp.person_id" : "_person.user_id");

View File

@@ -38,12 +38,12 @@ public class UserInPositionSqlTerm extends UserInSqlTerm {
Dialect dialect=column.getTableMetaData().getDatabaseMetaData().getDialect();
SqlAppender appender = new SqlAppender();
appender.addSpc(not ? "not" : "", "exists(select 1 from s_person_position _tmp");
appender.addSpc(not ? "not" : "", "exists(select 1 from ",getTableFullName("s_person_position")," _tmp");
if (isChild()||isParent()) {
appender.addSpc(",s_position _pos");
appender.addSpc(",",getTableFullName("s_position")," _pos");
}
if (!isForPerson()) {
appender.addSpc(",s_person _person");
appender.addSpc(",",getTableFullName("s_person")," _person");
}
appender.addSpc("where ",

View File

@@ -9,6 +9,7 @@ import org.hswebframework.ezorm.rdb.render.dialect.RenderPhase;
import org.hswebframework.ezorm.rdb.render.dialect.function.SqlFunction;
import org.hswebframework.web.commons.entity.TreeSupportEntity;
import org.hswebframework.web.dao.mybatis.mapper.AbstractSqlTermCustomizer;
import org.hswebframework.web.datasource.DataSourceHolder;
import org.hswebframework.web.service.QueryService;
import java.util.Arrays;
@@ -64,6 +65,13 @@ public abstract class UserInSqlTerm<PK> extends AbstractSqlTermCustomizer {
public abstract String getTableName();
protected String getTableFullName(String tableName){
String db = DataSourceHolder.databaseSwitcher().currentDatabase();
if (db != null) {
return db.concat(".").concat(tableName);
}
return tableName;
}
protected Object appendCondition(List<Object> values, String wherePrefix, SqlAppender appender, String column, Dialect dialect) {
if (!child&&!parent) {

View File

@@ -95,7 +95,7 @@
<aspectj.version>1.6.12</aspectj.version>
<hibernate.validator.version>5.1.1.Final</hibernate.validator.version>
<hsweb.ezorm.version>3.0.5-SNAPSHOT</hsweb.ezorm.version>
<hsweb.ezorm.version>3.0.5</hsweb.ezorm.version>
<hsweb.utils.version>3.0.2</hsweb.utils.version>
<hsweb.expands.version>3.0.2</hsweb.expands.version>
@@ -364,7 +364,7 @@
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.10.1</version>
<version>3.10.6</version>
</dependency>
<dependency>