首页 > 代码库 > 2017.2.20 《activiti实战第五章--用户与组及部署管理》(一)用户与组

2017.2.20 《activiti实战第五章--用户与组及部署管理》(一)用户与组

学习资料:《Activiti实战》

 

第五章 用户与组及部署管理(一)用户与组

内容概览:讲解activiti中内置的一套用户、组的关系,以及如何通过API添加、删除、查询。

5.1 用户与组

5.1.1 用户

 1 public class IdentityServiceTest{
 2     @Rule
 3     public ActivitiRule ar = new ActivitiRule();//使用默认的acitiviti.cfg.xml作为参数
 4     
 5     @Test
 6     public void testUser() throws Exception(){
 7         IdentityService is = ar.getIdentityService();
 8 
 9         User user = is.newUser("henryyan");
10         user.setFirstName("Henry");
11         user.setLastName("yan");
12         user.setEmail("yanhonglei@gamil.com");
13    
14         is.saveUser(user);
15         
16         User userInDb = is.createUserQuery().userId("henryyan").singleResult();
17         assertNotNull(userInDb);
18         
19         is.deleteUser("henryyan");
20         userInDb = is.createUserQuery().userId("henryyan").singleResult();
21         assertNull(userInDb);
22     }   
23 }

 

5.1.2 组

 1 public class IdentityServiceTest{
 2     @Rule
 3     public ActivitiRule ar = new ActivitiRule();//使用默认的acitiviti.cfg.xml作为参数
 4     
 5     @Test
 6     public void testGroup() throws Exception(){
 7         IdentityService is = ar.getIdentityService();
 8 
 9         Group group = is.newGroup("deptLeader");
10         group .setName("部门领导");
11         group .setType("assignment");
12    
13         is.saveGroup(group);
14         
15         List<Group> groupList = is.createGroupQuery().groupId("deptLeader").list();
16         assertEquals(1,groupList.size());
17         
18         is.deleteGroup("deptLeader");
19         groupList = is.createGroupQuery().groupId("deptLeader").list();
20         assertEquals(0,groupList.size());
21     }   
22 }

 

5.1.3 用户与组的关系

 1 public class IdentityServiceTest{
 2     @Rule
 3     public ActivitiRule ar = new ActivitiRule();//使用默认的acitiviti.cfg.xml作为参数
 4       
 5     @Test
 6     public void testUserAndGroupMembership() throws Exception(){
 7         IdentityService is = ar.getIdentityService();
 8 
 9         Group group = is.newGroup("deptLeader");
10         group .setName("部门领导");
11         group .setType("assignment");
12         is.saveGroup(group);
13         
14         User user = is.newUser("henryyan");
15         user.setFirstName("Henry");
16         user.setLastName("yan");
17         user.setEmail("yanhonglei@gamil.com");
18         is.saveUser(user);
19 
20         is.createMembership("henryyan","deptLeader");
21 
22         //查询组deptLeader所拥有的用户
23         User userInGroup = is.createUserQuery().memberOfGroup("deptLeader").singleResult();
24         assertNotNull(userInGroup);
25         assertEquals("henryyan",userInGroup.getId());
26 
27         //查询用户henryyan所属的组
28         Group groupContainsHenryyan = is.createGroupQuery().groupMember("henryyan").singleResult();
29         assertNotNull(groupContainsHenryyan);
30         assertEquals("deptLeader",groupContainsHenryyan.getId());
31     }   
32  }

 

5.1.4 UserTask中的用户与组

假设一个流程图示意图如下:

开始--->用户任务--->结束

共用的xml文件如下:

1 <process id="userAndGroupInUserTask" name="userAndGroupInUserTask">
2     <startEvent id="startEvent1" name="Start"></startEvent>
3     <userTask id="studyUserAndGroupInUserTask" name="学习用户与组在用户任务中的应用"></userTask>
4     <sequenceFlow id="flow1" name="" sourceRef="startEvent1" targetRef="studyUserAndGroupInUserTask"></sequenceFlow>
5     <endEvent id="endEvent1" name="End"></endEvent>
6     <sequenceFlow id="flow2" name="" sourceRef="studyUserAndGroupInUserTask" targetRef="endEvent1"></sequenceFlow>
7 </process>

 

共用的测试代码如下:

 1 public class UserAndGroupInUserTaskTest{
 2     @Before
 3     public void setup() throws Exception(){
 4         //初始化7个Service
 5         super.setUp();
 6 
 7         Group group = identityService.newGroup("deptLeader");
 8         group .setName("部门领导");
 9         group .setType("assignment");
10         identityService.saveGroup(group);
11         
12         User user = identityService.newUser("henryyan");
13         user.setFirstName("Henry");
14         user.setLastName("yan");
15         user.setEmail("yanhonglei@gmail.com");
16         identityService.saveUser(user);
17 
18         identityService.createMembership("henryyan","deptLeader");
19     }   
20 
21     @After
22     public void afterInvokeTestMethod() throws Exception(){
23         identityService.deleteMembership("henryyan","deptLeader");
24         identityService.deleteGroup("deptLeader");
25         identityService.deleteUser("henryyan");
26     }
27     ...
28  }

 

(1)候选组

修改上面的UserTask如下:属于这个组的用户都可以签收任务。一旦有一个人签收,其他人的用户任务里就没有这个任务了。

1 <userTask id="studyUserAndGroupInUserTask" 
2      name="学习用户与组在用户任务中的应用"
3      activiti:candidateGroups="deptLeader">
4 </userTask>

 

在类userAndGroupTestInUserTask添加方法如下:

 

 

(2)候选人

2017.2.20 《activiti实战第五章--用户与组及部署管理》(一)用户与组