Activiti6.0流程引擎学习——(23)activiti的用户和用户组管理服务(IdentityService)

这篇介绍activiti的身份管理服务——IdentityService

IdentityService:

1、管理用户(User),创建、查询与删除用户;

2、管理用户组(Group);

3、用户与用户组的关系(Membership),使用Membership构建关系。

eg:identityService.createMembership("user1","group1");

 

下面开始测试使用IdentityService:

一、创建IdentityService测试类

在coreapi中创建IdentityServiceTest测试类:

Activiti6.0流程引擎学习——(23)activiti的用户和用户组管理服务(IdentityService)_第1张图片

 

二、测试用户与用户组的关系

在IdentityServiceTest测试类中添加testIdentity测试方法:

package com.jjf.activiti.coreapi;

import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.test.ActivitiRule;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 身份管理服务测试
 */
public class IdentityServiceTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(IdentityServiceTest.class);

    @Rule
    public ActivitiRule activitiRule = new ActivitiRule();

    @Test
    public void testIdentity(){
        IdentityService identityService = activitiRule.getIdentityService();    //这里不依赖流程部署文件,所以不用配置
        User user1 = identityService.newUser("user1");   //初始化用户user1
        user1.setEmail("[email protected]");
        User user2 = identityService.newUser("user2");
        user2.setEmail("[email protected]");
        identityService.saveUser(user1);    //将用户保存
        identityService.saveUser(user2);

        Group group1 = identityService.newGroup("group1");   //初始化用户组group1
        identityService.saveGroup(group1);
        Group group2 = identityService.newGroup("group2");
        identityService.saveGroup(group2);

        identityService.createMembership("user1","group1");    //构建用户与用户组的关系
        identityService.createMembership("user2","group1");
        identityService.createMembership("user1","group2");

        User user1_2 = identityService.createUserQuery().userId("user1").singleResult();   //将用户名修改,版本号改为2
        user1_2.setLastName("user1_2");
        identityService.saveUser(user1_2);

        List userList = identityService.createUserQuery().memberOfGroup("group1").listPage(0, 100);//查询用户列表
        for (User user:userList){    //查询group1组中的所有用户
            LOGGER.info("group1的user : [{}]" , ToStringBuilder.reflectionToString(user, ToStringStyle.JSON_STYLE));   //按照json格式输出
        }

        List groupList = identityService.createGroupQuery().groupMember("user1").listPage(0, 100);
        for (Group group:groupList){    //查询user1用户所属的组
            LOGGER.info("user1的group : [{}]" , ToStringBuilder.reflectionToString(group, ToStringStyle.JSON_STYLE));
        }
    }
}

测试函数中首先创建两个user1与user2用户,然后创建group1与group2用户组,再将user1、user2与group1建立关系,user1与group2建立关系,将user1的LastName设为user1_2,最后输出group1用户组中的所有用户与user1加入的所有用户组。

输出结果:

Loading XML bean definitions from class path resource [activiti.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
group1的user : [{"firstName":null,"lastName":"user1_2","email":"[email protected]","password":null,"pictureByteArrayRef":"ByteArrayRef[id=null, name=null, entity=null]","id":"user1","revision":2,"isInserted":false,"isUpdated":false,"isDeleted":false}]
group1的user : [{"firstName":null,"lastName":null,"email":"[email protected]","password":null,"pictureByteArrayRef":"ByteArrayRef[id=null, name=null, entity=null]","id":"user2","revision":1,"isInserted":false,"isUpdated":false,"isDeleted":false}]
user1的group : [{"name":null,"type":null,"id":"group1","revision":1,"isInserted":false,"isUpdated":false,"isDeleted":false}]
user1的group : [{"name":null,"type":null,"id":"group2","revision":1,"isInserted":false,"isUpdated":false,"isDeleted":false}]

Process finished with exit code 0

可以看到group1中有两个用户,分别为user1与user2,其中user1的lastName为user1_2,并且版本号为2;user1加入的用户组有group1与group2。

 

三、身份管理服务

activiti中保存用户时执行流程API调用:

Activiti6.0流程引擎学习——(23)activiti的用户和用户组管理服务(IdentityService)_第2张图片

保存用户时调用IdentityService中的saveUser方法;然后调用下面命令执行器(CommandExecutor),将saveUser这个用户的对象封装成一个命令对象,也就是将User对象构建成SaveUserCmd对象,把参数组装成一个命令,再执行;这里命令的执行方式是实现了UserEntityManager接口,调用底层数据库的修改;间接执行了UserDataManager:MybatisUserDataManager;最后通过DbSQLSession执行insert操作把数据保存到数据库中。

 

你可能感兴趣的:(activiti)