一、前言
本章继续学习activiti6.0的核心api之IdentityService
二、IdentityService
/**
*
* 测试IdentityService
* IdentityService的作用:
* 1、管理用户(User),包括用户的增删改查
* 2、管理用户组(Group)包括用户组的增删改查
* 3、管理用户和用户组的关系(Membership)
* @author chaoge
* @since 2018/10/9 08:52
*/
public class IdentityServiceTest {
private static final LoggerLOGGER = LoggerFactory.getLogger(IdentityServiceTest.class);
@Rule
public ActivitiRulerule =new ActivitiRule();
/**
* 测试用户、用户组的创建及关联
*/
@Test
public void testIdentityService(){
IdentityService identityService =rule.getIdentityService();
//用户和用户组创建成功以后会存入数据库中,所以第二次测试的时候因为用户已经存在而报主键重复的错,可以配置
// User user1 = identityService.newUser("user1");
// user1.setEmail("user1@qq.com");
// identityService.saveUser(user1);
// User user2 = identityService.newUser("user2");
// user2.setEmail("user2@qq.com");
// identityService.saveUser(user2);
//
// Group group1 = identityService.newGroup("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 = identityService.createUserQuery().userId("user1").singleResult();
//不能修改id,否则会报错
//user1.setId("user");
user1.setLastName("chaoge");
identityService.saveUser(user1);
//查询组中的用户
List userList = identityService.createUserQuery().memberOfGroup("group1").listPage(0, 100);
userList.forEach(user ->LOGGER.info("user = {}",ToStringBuilder.reflectionToString(user,ToStringStyle.JSON_STYLE)));
//查询用户所在组
List groupList = identityService.createGroupQuery().groupMember("user1").listPage(0, 100);
groupList.forEach(group ->LOGGER.info("group = {}", ToStringBuilder.reflectionToString(group,ToStringStyle.JSON_STYLE)));
}
}