优化关系

This commit is contained in:
zhouhao
2017-07-28 16:54:06 +08:00
parent 0f8be44afc
commit bd7a77d119
8 changed files with 173 additions and 53 deletions

View File

@@ -109,11 +109,11 @@
<version>${project.version}</version>
</dependency>
<!--使用shiro实现权限控制-->
<!--<dependency>-->
<!--<groupId>org.hswebframework.web</groupId>-->
<!--<artifactId>hsweb-authorization-shiro</artifactId>-->
<!--<version>${project.version}</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.hswebframework.web</groupId>
<artifactId>hsweb-authorization-shiro</artifactId>
<version>${project.version}</version>
</dependency>
<!--组织架构-->
<dependency>

View File

@@ -35,10 +35,7 @@ import org.hswebframework.web.service.authorization.AuthorizationSettingService;
import org.hswebframework.web.service.authorization.PermissionService;
import org.hswebframework.web.service.authorization.RoleService;
import org.hswebframework.web.service.authorization.UserService;
import org.hswebframework.web.service.organizational.DepartmentService;
import org.hswebframework.web.service.organizational.OrganizationalService;
import org.hswebframework.web.service.organizational.PersonService;
import org.hswebframework.web.service.organizational.PositionService;
import org.hswebframework.web.service.organizational.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
@@ -143,6 +140,9 @@ public class SpringBootExample
@Autowired
AuthorizationSettingService authorizationSettingService;
@Autowired
RelationInfoService relationInfoService;
public static void main(String[] args) {
SpringApplication.run(SpringBootExample.class);
}
@@ -269,5 +269,16 @@ public class SpringBootExample
personEntity.setPersonUser(personUserEntity);
personService.insert(personEntity);
RelationInfoEntity relationInfo = relationInfoService.createEntity();
relationInfo.setRelationFrom(personEntity.getId());
relationInfo.setRelationTo("zhangsan");
relationInfo.setRelationTypeFrom("person");
relationInfo.setRelationTypeTo("person");
relationInfo.setStatus(DataStatus.STATUS_ENABLED);
relationInfo.setRelationId("leader");
relationInfoService.insert(relationInfo);
}
}

View File

@@ -37,7 +37,12 @@ public class TestController implements QueryController<UserEntity, String, Query
@GetMapping("/test1")
@Authorize(action = "query")
public ResponseMessage testSimple(Authentication authentication) {
return ResponseMessage.ok(authentication).exclude(Authentication.class, "attributes");
return ResponseMessage.ok(
PersonnelAuthorization.current()
//查找人员关系
.map(PersonnelAuthorization::getRelations)
.map(relations -> relations.findPos("leader"))
.orElse(null));
}
@GetMapping("/test2")

View File

@@ -35,7 +35,29 @@ public interface PersonnelAuthorization extends Serializable {
Personnel getPersonnel();
/**
* 获取人员的关系信息
* <pre>
* boolean isLeader = PersonnelAuthorization
* .current().get()
* .getRelations()
* // 和张三的人员为leader关系, 我是张三的leader
* .has("leader","人员","张三");
* //我是开发部的leader
* //.has("leader","部门","开发部");
* //反转关系: 张三是我的leader
* //.has("leader","人员","张三","PRE");
* </pre>
* <pre>
* List<Relation> relations= PersonnelAuthorization.current()
* //查找用户关系
* .map(PersonnelAuthorization::getRelations)
* .map(relations -> relations.findAll("leader"))
* .orElse(null)
* </pre>
*
* @return 人员关系信息
* @see Relations
* @see org.hswebframework.web.organizational.authorization.relation.Relation
*/
Relations getRelations();

View File

@@ -35,13 +35,27 @@ public interface Relations extends Serializable {
* @return 是否存在关系
*/
default boolean has(String relation, String type, String to, Relation.Direction direction) {
return getAllRelations().stream().anyMatch(rel ->
return getAll().stream().anyMatch(rel ->
rel.getRelation().equals(relation)
&& rel.getType().equals(type)
&& rel.getTarget().equals(to)
&& rel.matchDirection(direction));
}
default boolean has(String relation, String type, Relation.Direction direction) {
return getAll().stream().anyMatch(rel ->
rel.getRelation().equals(relation)
&& rel.getType().equals(type)
&& rel.matchDirection(direction));
}
default boolean has(String relation, Relation.Direction direction) {
return getAll().stream().anyMatch(rel ->
rel.getRelation().equals(relation)
&& rel.matchDirection(direction));
}
/**
* @see this#has(String, String, String, Relation.Direction)
*/
@@ -54,19 +68,63 @@ public interface Relations extends Serializable {
*
* @see this#has(String, String, String, Relation.Direction)
*/
default boolean has(String relation, String type, String to) {
default boolean has(String relation) {
return !findAll(relation).isEmpty();
}
default boolean hasRev(String relation, String type, String to) {
return has(relation, type, to, Relation.Direction.REVERSE);
}
default boolean hasPos(String relation, String type, String to) {
return has(relation, type, to, Relation.Direction.POSITIVE);
}
default boolean hasRev(String relation, String type) {
return has(relation, type, Relation.Direction.REVERSE);
}
default boolean hasPos(String relation, String type) {
return has(relation, type, Relation.Direction.POSITIVE);
}
default boolean hasPos(String relation) {
return has(relation, Relation.Direction.POSITIVE);
}
default boolean hasRev(String relation) {
return has(relation, Relation.Direction.REVERSE);
}
/**
* 获取指定关系的全部关系信息
*
* @param relation 关系标识
* @param relation 关系标识,如: leader
* @return 关系信息集合,如果关系不存在,返回空集合
* @see this#findRelations(Predicate)
* @see this#find(Predicate)
*/
default List<Relation> getRelations(String relation) {
return findRelations(rel -> rel.getRelation().equals(relation));
default List<Relation> findAll(String relation) {
return find(rel -> rel.getRelation().equals(relation));
}
/**
* 获取正向关系,如: 我是xxx的relation
*
* @param relation 关系标识,如: leader
* @return 关系信息集合,如果关系不存在,返回空集合
*/
default List<Relation> findRev(String relation) {
return find(relation, Relation.Direction.REVERSE);
}
/**
* 获取反向关系,如: xxx是我的relation
*
* @param relation 关系标识,如: leader
* @return 关系信息集合,如果关系不存在,返回空集合
*/
default List<Relation> findPos(String relation) {
return find(relation, Relation.Direction.REVERSE);
}
/**
@@ -76,8 +134,8 @@ public interface Relations extends Serializable {
* @param direction 关系方向
* @return 关系信息集合,如果关系不存在,返回空集合
*/
default List<Relation> getRelations(String relation, Relation.Direction direction) {
return findRelations(rel -> rel.getRelation().equals(relation) && rel.matchDirection(direction));
default List<Relation> find(String relation, Relation.Direction direction) {
return find(rel -> rel.getRelation().equals(relation) && rel.matchDirection(direction));
}
/**
@@ -86,10 +144,34 @@ public interface Relations extends Serializable {
* @param relation 关系标识,例如: leader
* @param type 关系类型,例如person
* @return 关系信息集合,如果关系不存在,返回空集合
* @see this#findRelations(Predicate)
* @see this#find(Predicate)
*/
default List<Relation> getRelations(String relation, String type) {
return findRelations(rel -> rel.getRelation().equals(relation) && rel.getType().equals(type));
default List<Relation> findAll(String relation, String type) {
return find(rel -> rel.getRelation().equals(relation) && rel.getType().equals(type));
}
/**
* 获取指定关系和类型以及方向<b>反向关系</b>
*
* @param relation 关系标识,例如: leader
* @param type 关系类型,例如person
* @return 关系信息集合,如果关系不存在,返回空集合
* @see this#find(String, String, Relation.Direction)
*/
default List<Relation> findRev(String relation, String type) {
return find(relation, type, Relation.Direction.REVERSE);
}
/**
* 获取指定关系和类型以及方向<b>正向关系</b>
*
* @param relation 关系标识,例如: leader
* @param type 关系类型,例如person
* @return 关系信息集合,如果关系不存在,返回空集合
* @see this#find(String, String, Relation.Direction)
*/
default List<Relation> findPos(String relation, String type) {
return find(relation, type, Relation.Direction.POSITIVE);
}
/**
@@ -99,38 +181,37 @@ public interface Relations extends Serializable {
* @param type 关系类型,例如person
* @param direction 关系方向
* @return 关系信息集合,如果关系不存在,返回空集合
* @see this#findRelations(Predicate)
* @see this#find(Predicate)
*/
default List<Relation> getRelations(String relation, String type, Relation.Direction direction) {
return findRelations(rel ->
default List<Relation> find(String relation, String type, Relation.Direction direction) {
return find(rel ->
rel.getRelation().equals(relation)
&& rel.getType().equals(type)
&& rel.matchDirection(direction));
}
/**
* @see this#getRelations(String, String, Relation.Direction)
* @see this#find(String, String, Relation.Direction)
*/
default List<Relation> getRelations(String relation, String type,String direction) {
return getRelations(relation,type, Relation.Direction.fromString(direction));
default List<Relation> find(String relation, String type, String direction) {
return find(relation, type, Relation.Direction.fromString(direction));
}
/**
* 查找关系
* <pre>
* findRelations(rel->rel.getType().equals("person"))
* findAll(rel->rel.getType().equals("person"))
* </pre>
*
* @param predicate 查找的判断逻辑
* @return 满足条件的关系信息集合,如果全部不满足则返回空集合
*/
default List<Relation> findRelations(Predicate<Relation> predicate) {
return getAllRelations().stream().filter(predicate).collect(Collectors.toList());
default List<Relation> find(Predicate<Relation> predicate) {
return getAll().stream().filter(predicate).collect(Collectors.toList());
}
/**
*
* @return 全部关系信息,如果一个也没有返回空集合
*/
List<Relation> getAllRelations();
List<Relation> getAll();
}

View File

@@ -8,23 +8,23 @@ import java.util.Objects;
* @author zhouhao
*/
public class SimpleRelations implements Relations {
private List<Relation> relations;
private List<Relation> all;
@Override
public List<Relation> getAllRelations() {
if (null == relations) relations = new ArrayList<>();
return relations;
public List<Relation> getAll() {
if (null == all) all = new ArrayList<>();
return all;
}
public void setRelations(List<Relation> relations) {
Objects.requireNonNull(relations);
this.relations = relations;
public void setAll(List<Relation> all) {
Objects.requireNonNull(all);
this.all = all;
}
public SimpleRelations() {
}
public SimpleRelations(List<Relation> relations) {
this.relations = relations;
public SimpleRelations(List<Relation> all) {
this.all = all;
}
}

View File

@@ -3,14 +3,14 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.hswebframework.web.dao.organizational.RelationInfoDao">
<resultMap id="RelationInfoResultMap" type="org.hswebframework.web.entity.organizational.SimpleRelationInfoEntity">
<id property="id" column="u_id" javaType="string" jdbcType="VARCHAR"/>
<result property="relationFrom" column="relation_from" javaType="String" jdbcType="VARCHAR"/>
<result property="relationId" column="relation_id" javaType="String" jdbcType="VARCHAR"/>
<result property="relationTo" column="relation_to" javaType="String" jdbcType="VARCHAR"/>
<result property="relationTypeFrom" column="relation_type_from" javaType="String" jdbcType="VARCHAR"/>
<result property="relationTypeTo" column="relation_type_to" javaType="String" jdbcType="VARCHAR"/>
<result property="status" column="status" javaType="Byte" jdbcType="DECIMAL"/>
<resultMap id="RelationInfoResultMap" type="org.hswebframework.web.entity.organizational.RelationInfoEntity">
<id property="id" column="u_id" javaType="string" jdbcType="VARCHAR"/>
<result property="relationFrom" column="relation_from" javaType="String" jdbcType="VARCHAR"/>
<result property="relationId" column="relation_id" javaType="String" jdbcType="VARCHAR"/>
<result property="relationTo" column="relation_to" javaType="String" jdbcType="VARCHAR"/>
<result property="relationTypeFrom" column="relation_type_from" javaType="String" jdbcType="VARCHAR"/>
<result property="relationTypeTo" column="relation_type_to" javaType="String" jdbcType="VARCHAR"/>
<result property="status" column="status" javaType="Byte" jdbcType="DECIMAL"/>
</resultMap>
<!--用于动态生成sql所需的配置-->
@@ -18,8 +18,8 @@
<bind name="resultMapId" value="'RelationInfoResultMap'"/>
<bind name="tableName" value="'s_relation_info'"/>
</sql>
<insert id="insert" parameterType="org.hswebframework.web.entity.organizational.SimpleRelationInfoEntity" >
<insert id="insert" parameterType="org.hswebframework.web.entity.organizational.RelationInfoEntity">
<include refid="config"/>
<include refid="BasicMapper.buildInsertSql"/>
</insert>

View File

@@ -31,6 +31,7 @@ import org.hswebframework.web.organizational.authorization.TreeNode;
import org.hswebframework.web.organizational.authorization.relation.Relation;
import org.hswebframework.web.organizational.authorization.relation.SimpleRelation;
import org.hswebframework.web.organizational.authorization.relation.SimpleRelations;
import org.hswebframework.web.organizational.authorization.simple.SimplePersonnel;
import org.hswebframework.web.organizational.authorization.simple.SimplePersonnelAuthorization;
import org.hswebframework.web.service.DefaultDSLQueryService;
import org.hswebframework.web.service.EnableCacheGenericEntityService;
@@ -225,7 +226,7 @@ public class SimplePersonService extends EnableCacheGenericEntityService<PersonE
PersonEntity entity = selectByPk(personId);
assertNotNull(entity);
Personnel personnel = entityFactory.newInstance(Personnel.class, SimplePositionEntity.class);
Personnel personnel = entityFactory.newInstance(Personnel.class, SimplePersonnel.class);
entityFactory.copyProperties(entity, personnel);
authorization.setPersonnel(personnel);
@@ -268,7 +269,7 @@ public class SimplePersonService extends EnableCacheGenericEntityService<PersonE
relation.setType(info.getRelationTypeFrom());
relation.setTarget(info.getRelationTo());
//info.getRelationId() ->
//relation.setName(info.getRelationId());
relation.setRelation(info.getRelationId());
if (personId.equals(info.getRelationFrom())) {
relation.setDirection(Relation.Direction.POSITIVE);
} else {