1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.test.service;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.kuali.rice.kim.bo.impl.RoleImpl;
26 import org.kuali.rice.kim.bo.role.dto.KimRoleInfo;
27 import org.kuali.rice.kim.bo.role.dto.RoleMembershipInfo;
28 import org.kuali.rice.kim.bo.role.impl.KimPermissionImpl;
29 import org.kuali.rice.kim.bo.role.impl.KimPermissionTemplateImpl;
30 import org.kuali.rice.kim.bo.role.impl.RoleMemberImpl;
31 import org.kuali.rice.kim.bo.role.impl.RolePermissionImpl;
32 import org.kuali.rice.kim.service.KIMServiceLocator;
33 import org.kuali.rice.kim.service.PermissionService;
34 import org.kuali.rice.kim.service.RoleService;
35 import org.kuali.rice.kim.test.KIMTestCase;
36 import org.kuali.rice.kns.service.KNSServiceLocator;
37
38
39
40
41
42
43
44 public class AuthorizationServiceImplTest extends KIMTestCase {
45
46 private PermissionService permissionService;
47 private RoleService roleService;
48
49 private String principal1Id = "p1";
50 private String principal2Id = "p2";
51 private String principal3Id = "p3";
52
53 private String group1Id = "g1";
54
55 private String role1Id = "r1";
56 private String role1NamespaceCode = "AUTH_SVC_TEST1";
57 private String role1Description = "Role 1 Description";
58 private String role1Name = "RoleOne";
59
60 private String role2Id = "r2";
61 private String role2NamespaceCode = "AUTH_SVC_TEST2";
62 private String role2Description = "Role 2 Description";
63 private String role2Name = "RoleTwo";
64
65 private String permission1Name = "perm1";
66 private String permission1NamespaceCode = "KR-NS";
67 private String permission1Id = "p1";
68
69 private String permission2Name = "perm2";
70 private String permission2NamespaceCode = "KR-NS";
71 private String permission2Id = "p2";
72
73 private String permission3Name = "perm3";
74 private String permission3NamespaceCode = "KR-NS";
75 private String permission3Id = "p3";
76
77
78 @Before
79 public void setUp() throws Exception {
80 super.setUp();
81
82 permissionService = KIMServiceLocator.getPermissionService();
83 roleService = KIMServiceLocator.getRoleService();
84
85 if (true) return;
86
87
88 RoleImpl role1 = new RoleImpl();
89 role1.setRoleId(role1Id);
90 role1.setActive(true);
91 role1.setKimTypeId(getDefaultKimType().getKimTypeId());
92 role1.setNamespaceCode(role1NamespaceCode);
93 role1.setRoleDescription(role1Description);
94 role1.setRoleName(role1Name);
95 List<RoleMemberImpl> members1 = new ArrayList<RoleMemberImpl>();
96 role1.setMembers(members1);
97 RoleMemberImpl p1Member = new RoleMemberImpl();
98 p1Member.setMemberId(principal1Id);
99 p1Member.setMemberTypeCode("P");
100 p1Member.setRoleId(role1Id);
101 p1Member.setRoleMemberId(getNewRoleMemberId());
102 members1.add(p1Member);
103 KNSServiceLocator.getBusinessObjectService().save(role1);
104
105
106 RoleImpl role2 = new RoleImpl();
107 role2.setRoleId(role2Id);
108 role2.setActive(true);
109 role2.setKimTypeId(getDefaultKimType().getKimTypeId());
110 role2.setNamespaceCode(role2NamespaceCode);
111 role2.setRoleDescription(role2Description);
112 role2.setRoleName(role2Name);
113 List<RoleMemberImpl> members2 = new ArrayList<RoleMemberImpl>();
114 role2.setMembers(members2);
115 RoleMemberImpl p3Member = new RoleMemberImpl();
116 p3Member.setMemberId(principal3Id);
117 p3Member.setMemberTypeCode("P");
118 p3Member.setRoleId(role2Id);
119 p3Member.setRoleMemberId(getNewRoleMemberId());
120 members2.add(p3Member);
121 RoleMemberImpl g1Member = new RoleMemberImpl();
122 g1Member.setMemberId(group1Id);
123 g1Member.setMemberTypeCode("G");
124 g1Member.setRoleId(role2Id);
125 g1Member.setRoleMemberId(getNewRoleMemberId());
126 members2.add(g1Member);
127 RoleMemberImpl r1Member = new RoleMemberImpl();
128 r1Member.setMemberId(role1Id);
129 r1Member.setMemberTypeCode("R");
130 r1Member.setRoleId(role2Id);
131 r1Member.setRoleMemberId(getNewRoleMemberId());
132 members2.add(r1Member);
133 KNSServiceLocator.getBusinessObjectService().save(role2);
134
135
136
137 KimPermissionTemplateImpl defaultTemplate = getDefaultPermissionTemplate();
138
139 KimPermissionImpl permission1 = new KimPermissionImpl();
140 permission1.setActive(true);
141 permission1.setDescription("permission1");
142 permission1.setName(permission1Name);
143 permission1.setNamespaceCode(permission1NamespaceCode);
144 permission1.setPermissionId(permission1Id);
145 permission1.setTemplateId(defaultTemplate.getPermissionTemplateId());
146 permission1.setTemplate(defaultTemplate);
147 KNSServiceLocator.getBusinessObjectService().save(permission1);
148
149 KimPermissionImpl permission2 = new KimPermissionImpl();
150 permission2.setActive(true);
151 permission2.setDescription("permission2");
152 permission2.setName(permission2Name);
153 permission2.setNamespaceCode(permission2NamespaceCode);
154 permission2.setPermissionId(permission2Id);
155 permission2.setTemplateId(defaultTemplate.getPermissionTemplateId());
156 permission2.setTemplate(defaultTemplate);
157 KNSServiceLocator.getBusinessObjectService().save(permission2);
158
159 KimPermissionImpl permission3 = new KimPermissionImpl();
160 permission3.setActive(true);
161 permission3.setDescription("permission3");
162 permission3.setName(permission3Name);
163 permission3.setNamespaceCode(permission3NamespaceCode);
164 permission3.setPermissionId(permission3Id);
165 permission3.setTemplateId(defaultTemplate.getPermissionTemplateId());
166 permission3.setTemplate(defaultTemplate);
167 KNSServiceLocator.getBusinessObjectService().save(permission3);
168
169
170
171
172
173
174 RolePermissionImpl role1Perm1 = new RolePermissionImpl();
175 role1Perm1.setActive(true);
176 role1Perm1.setRoleId(role1Id);
177 role1Perm1.setPermissionId(permission1Id);
178 role1Perm1.setRolePermissionId(getNewRolePermissionId());
179 KNSServiceLocator.getBusinessObjectService().save(role1Perm1);
180
181 RolePermissionImpl role1Perm2 = new RolePermissionImpl();
182 role1Perm2.setActive(true);
183 role1Perm2.setRoleId(role1Id);
184 role1Perm2.setPermissionId(permission2Id);
185 role1Perm2.setRolePermissionId(getNewRolePermissionId());
186 KNSServiceLocator.getBusinessObjectService().save(role1Perm2);
187
188 RolePermissionImpl role2Perm3 = new RolePermissionImpl();
189 role2Perm3.setActive(true);
190 role2Perm3.setRoleId(role2Id);
191 role2Perm3.setPermissionId(permission3Id);
192 role2Perm3.setRolePermissionId(getNewRolePermissionId());
193 KNSServiceLocator.getBusinessObjectService().save(role2Perm3);
194 }
195
196 @Test
197 public void testRoleMembership() {
198 KimRoleInfo role = roleService.getRole( role2Id );
199 assertNotNull( "r2 must exist", role );
200 ArrayList<String> roleList = new ArrayList<String>( 1 );
201 roleList.add( role2Id );
202
203 Collection<String> memberPrincipalIds = roleService.getRoleMemberPrincipalIds(role2NamespaceCode, role2Name, null);
204 assertNotNull(memberPrincipalIds);
205 assertEquals("RoleTwo should have 6 principal ids", 6, memberPrincipalIds.size());
206 assertTrue( "p3 must belong to role", memberPrincipalIds.contains(principal3Id) );
207 assertTrue( "p2 must belong to role (assigned via group)", memberPrincipalIds.contains(principal2Id) );
208 assertTrue( "p1 must belong to r2 (via r1)", memberPrincipalIds.contains(principal1Id) );
209
210 Collection<RoleMembershipInfo> members = roleService.getRoleMembers( roleList, null );
211 assertNotNull( "returned list may not be null", members );
212 assertFalse( "list must not be empty", members.isEmpty() );
213 assertEquals("Returned list must have 4 members.", 4, members.size());
214 boolean foundP3 = false;
215 boolean foundG1 = false;
216 boolean foundR1 = false;
217 for (RoleMembershipInfo member : members) {
218 if (member.getMemberId().equals(principal3Id) && member.getMemberTypeCode().equals("P")) {
219 foundP3 = true;
220 } else if (member.getMemberId().equals(group1Id) && member.getMemberTypeCode().equals("G")) {
221 foundG1 = true;
222 } else if (member.getMemberId().equals(principal1Id) && member.getMemberTypeCode().equals("P")) {
223 foundR1 = true;
224 assertEquals("Should have r1 embedded role id.", role1Id, member.getEmbeddedRoleId());
225 }
226 }
227 assertTrue("Failed to find p3 principal member", foundP3);
228 assertTrue("Failed to find g1 group member", foundG1);
229 assertTrue("Failed to find r1 role member", foundR1);
230
231 role = roleService.getRole( role1Id );
232 assertNotNull( "r1 must exist", role );
233 roleList.clear();
234 roleList.add( role1Id );
235 members = roleService.getRoleMembers( roleList, null );
236 assertNotNull( "returned list may not be null", members );
237 assertEquals("Should have 2 members", 2, members.size());
238 Iterator<RoleMembershipInfo> iter = members.iterator();
239 assertTrue("One of those members should be p1.", principal1Id.equals(iter.next().getMemberId()) || principal1Id.equals(iter.next().getMemberId()));
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 @Test
257 public void testHasPermission() {
258
259 assertTrue( "p1 must have perm1 (via r1)", permissionService.hasPermission( "p1", "KR-NS", "perm1", null ) );
260 assertTrue( "p1 must have perm2 (via r1)", permissionService.hasPermission( "p1", "KR-NS", "perm2", null ) );
261 assertTrue( "p1 must have perm3 (via r2)", permissionService.hasPermission( "p1", "KR-NS", "perm3", null ) );
262 assertTrue( "p3 must have perm3 (via r2)", permissionService.hasPermission( "p3", "KR-NS", "perm3", null ) );
263 assertFalse( "p3 must not have perm1", permissionService.hasPermission( "p3", "KR-NS", "perm1", null ) );
264 assertFalse( "p3 must not have perm2", permissionService.hasPermission( "p3", "KR-NS", "perm2", null ) );
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 }