1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.impl.jaxb; |
17 | |
|
18 | |
import org.apache.commons.lang.StringUtils; |
19 | |
import org.joda.time.DateTime; |
20 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
21 | |
import org.kuali.rice.core.api.membership.MemberType; |
22 | |
import org.kuali.rice.core.util.jaxb.NameAndNamespacePair; |
23 | |
import org.kuali.rice.kim.api.KimConstants.KimUIConstants; |
24 | |
import org.kuali.rice.kim.api.group.GroupContract; |
25 | |
import org.kuali.rice.kim.api.identity.principal.PrincipalContract; |
26 | |
import org.kuali.rice.kim.api.permission.PermissionContract; |
27 | |
import org.kuali.rice.kim.api.role.Role; |
28 | |
import org.kuali.rice.kim.api.role.RoleContract; |
29 | |
import org.kuali.rice.kim.api.role.RoleMember; |
30 | |
import org.kuali.rice.kim.api.role.RoleMemberContract; |
31 | |
import org.kuali.rice.kim.api.role.RoleService; |
32 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
33 | |
|
34 | |
import javax.xml.bind.UnmarshalException; |
35 | |
import java.util.HashMap; |
36 | |
import java.util.List; |
37 | |
import java.util.Set; |
38 | |
|
39 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.equal; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public final class RoleXmlUtil { |
51 | |
|
52 | 0 | private RoleXmlUtil() {} |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
static String validateAndPersistNewRole(RoleXmlDTO newRole) throws UnmarshalException { |
63 | 0 | if (newRole == null) { |
64 | 0 | throw new IllegalArgumentException("Cannot persist a null role"); |
65 | |
} |
66 | |
|
67 | |
|
68 | 0 | validateAndPrepareRole(newRole); |
69 | |
|
70 | 0 | Role.Builder builder = Role.Builder.create(); |
71 | 0 | builder.setActive(newRole.getActive()); |
72 | 0 | builder.setDescription(newRole.getRoleDescription()); |
73 | 0 | builder.setId(newRole.getRoleId()); |
74 | 0 | builder.setKimTypeId(newRole.getKimTypeId()); |
75 | 0 | builder.setName(newRole.getRoleName()); |
76 | 0 | builder.setNamespaceCode(newRole.getNamespaceCode()); |
77 | |
|
78 | |
|
79 | 0 | Role role = KimApiServiceLocator.getRoleService().createRole(builder.build()); |
80 | |
|
81 | |
|
82 | 0 | newRole.setAlreadyPersisted(true); |
83 | |
|
84 | 0 | return role.getId(); |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
static String validateAndPersistNewRoleMember(RoleMemberXmlDTO newRoleMember) throws UnmarshalException { |
96 | |
|
97 | 0 | if (newRoleMember == null) { |
98 | 0 | throw new IllegalArgumentException("Cannot persist a null role member"); |
99 | |
} |
100 | |
|
101 | |
|
102 | 0 | validateRoleIdAndRoleNameForMember(newRoleMember); |
103 | |
|
104 | |
|
105 | 0 | validateMemberIdentity(newRoleMember); |
106 | |
|
107 | |
|
108 | 0 | if (newRoleMember.getActiveFromDate() != null && newRoleMember.getActiveToDate() != null && |
109 | |
newRoleMember.getActiveFromDate().compareTo(newRoleMember.getActiveToDate()) > 0) { |
110 | 0 | throw new UnmarshalException("Cannot create a role member whose activeFromDate occurs after its activeToDate"); |
111 | |
} |
112 | |
|
113 | |
|
114 | 0 | if (newRoleMember.getQualifications() == null) { |
115 | 0 | newRoleMember.setQualifications(new HashMap<String, String>()); |
116 | |
} |
117 | |
|
118 | 0 | RoleMember.Builder builder = RoleMember.Builder.create(newRoleMember.getRoleId(), newRoleMember.getRoleIdAsMember(), |
119 | |
newRoleMember.getMemberId(), newRoleMember.getMemberType(), |
120 | |
newRoleMember.getActiveFromDate() == null ? null : new DateTime(newRoleMember.getActiveFromDate().getMillis()), |
121 | |
newRoleMember.getActiveToDate() == null ? null : new DateTime(newRoleMember.getActiveToDate().getMillis()), |
122 | |
newRoleMember.getQualifications()); |
123 | |
|
124 | |
|
125 | 0 | RoleMemberContract newMember = KimApiServiceLocator.getRoleService().createRoleMember(builder.build()); |
126 | |
|
127 | 0 | return newMember.getRoleMemberId(); |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
static void validateAndPersistNewRolePermission(RolePermissionXmlDTO newRolePermission) throws UnmarshalException { |
138 | 0 | if (newRolePermission == null) { |
139 | 0 | throw new IllegalArgumentException("newRolePermission cannot be null"); |
140 | |
} |
141 | |
|
142 | |
|
143 | 0 | validateAndPrepareRolePermission(newRolePermission); |
144 | |
|
145 | |
|
146 | 0 | KimApiServiceLocator.getRoleService().assignPermissionToRole(newRolePermission.getPermissionId(), newRolePermission.getRoleId()); |
147 | 0 | } |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
static void removeRoleMembers(String roleId, Set<String> existingRoleMemberIds) { |
157 | 0 | if (StringUtils.isBlank(roleId)) { |
158 | 0 | throw new IllegalArgumentException("roleId cannot be blank"); |
159 | 0 | } else if (existingRoleMemberIds == null) { |
160 | 0 | throw new IllegalArgumentException("existingRoleMemberIds cannot be null"); |
161 | |
} |
162 | 0 | RoleService roleUpdateService = KimApiServiceLocator.getRoleService(); |
163 | 0 | RoleContract role = KimApiServiceLocator.getRoleService().getRole(roleId); |
164 | 0 | if (role == null) { |
165 | 0 | throw new IllegalArgumentException("Cannot remove role members for role with ID \"" + roleId + "\" because that role does not exist"); |
166 | |
} |
167 | |
|
168 | |
|
169 | 0 | List<RoleMember> roleMembers = KimApiServiceLocator.getRoleService().findRoleMembers( |
170 | |
QueryByCriteria.Builder.fromPredicates(equal("roleId", roleId))).getResults(); |
171 | 0 | if (roleMembers != null && !roleMembers.isEmpty()) { |
172 | 0 | for (RoleMemberContract roleMember : roleMembers) { |
173 | 0 | if (!existingRoleMemberIds.contains(roleMember.getRoleMemberId())) { |
174 | |
|
175 | 0 | MemberType memberType = roleMember.getMemberType(); |
176 | 0 | if (MemberType.PRINCIPAL.equals(memberType)) { |
177 | 0 | roleUpdateService.removePrincipalFromRole(roleMember.getMemberId(), role.getNamespaceCode(), role.getName(), |
178 | |
(roleMember.getAttributes() != null) ? roleMember.getAttributes() : new HashMap<String, String>()); |
179 | 0 | } else if (MemberType.GROUP.equals(memberType)) { |
180 | 0 | roleUpdateService.removeGroupFromRole(roleMember.getMemberId(), role.getNamespaceCode(), role.getName(), |
181 | |
(roleMember.getAttributes() != null) ? roleMember.getAttributes() :new HashMap<String, String>()); |
182 | 0 | } else if (MemberType.ROLE.equals(memberType)) { |
183 | 0 | roleUpdateService.removeRoleFromRole(roleMember.getMemberId(), role.getNamespaceCode(), role.getName(), |
184 | |
(roleMember.getAttributes() != null) ? roleMember.getAttributes() : new HashMap<String, String>()); |
185 | |
} |
186 | 0 | } |
187 | |
} |
188 | |
} |
189 | 0 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
private static void validateAndPrepareRole(RoleXmlDTO newRole) throws UnmarshalException { |
195 | |
|
196 | 0 | if (StringUtils.isBlank(newRole.getRoleName()) || StringUtils.isBlank(newRole.getNamespaceCode())) { |
197 | 0 | throw new UnmarshalException("Cannot create or override a role with a blank name or a blank namespace"); |
198 | 0 | } else if (StringUtils.isBlank(newRole.getKimTypeId())) { |
199 | 0 | throw new UnmarshalException("Cannot create or override a role without specikfying a KIM type"); |
200 | 0 | } else if (StringUtils.isBlank(newRole.getRoleDescription())) { |
201 | 0 | throw new UnmarshalException("Cannot create or override a role with a blank description"); |
202 | |
} |
203 | |
|
204 | |
|
205 | 0 | String matchingId = KimApiServiceLocator.getRoleService().getRoleIdByNameAndNamespaceCode( |
206 | |
newRole.getNamespaceCode(), newRole.getRoleName()); |
207 | 0 | if (StringUtils.isNotBlank(matchingId)) { |
208 | 0 | newRole.setRoleId(matchingId); |
209 | |
} |
210 | 0 | } |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
private static void validateRoleIdAndRoleNameForMember(RoleMemberXmlDTO newRoleMember) throws UnmarshalException { |
216 | |
|
217 | 0 | if (newRoleMember instanceof RoleMemberXmlDTO.OutsideOfRole) { |
218 | 0 | RoleMemberXmlDTO.OutsideOfRole standaloneMember = (RoleMemberXmlDTO.OutsideOfRole) newRoleMember; |
219 | 0 | if (standaloneMember.getRoleNameAndNamespace() != null) { |
220 | |
|
221 | 0 | String existingId = KimApiServiceLocator.getRoleService().getRoleIdByNameAndNamespaceCode( |
222 | |
standaloneMember.getRoleNamespaceCode(), standaloneMember.getRoleName()); |
223 | 0 | if (StringUtils.isBlank(existingId)) { |
224 | 0 | throw new UnmarshalException("Cannot create role member for role with name \"" + standaloneMember.getRoleName() + "\" and namespace \"" + |
225 | |
standaloneMember.getRoleNamespaceCode() + "\" because such a role does not exist"); |
226 | |
} |
227 | |
|
228 | |
|
229 | 0 | if (StringUtils.isBlank(standaloneMember.getRoleId())) { |
230 | 0 | standaloneMember.setRoleId(existingId); |
231 | 0 | } else if (!standaloneMember.getRoleId().equals(existingId)) { |
232 | 0 | throw new UnmarshalException("Cannot create role member for role with ID \"" + standaloneMember.getRoleId() + "\", name \"" + |
233 | |
standaloneMember.getRoleName() + "\", and namespace \"" + standaloneMember.getRoleNamespaceCode() + |
234 | |
"\" because the existing role with the same name and namespace has an ID of \"" + existingId + "\" instead"); |
235 | |
} |
236 | 0 | } else if (StringUtils.isBlank(standaloneMember.getRoleId())) { |
237 | 0 | throw new UnmarshalException("Cannot create role member without providing the role ID or role name + namespace that the member belongs to"); |
238 | 0 | } else if (KimApiServiceLocator.getRoleService().getRole(standaloneMember.getRoleId()) == null) { |
239 | 0 | throw new UnmarshalException("Cannot create role member for the role with ID \"" + standaloneMember.getRoleId() + "\" because that role does not exist"); |
240 | |
} |
241 | |
} |
242 | |
|
243 | |
|
244 | 0 | if (StringUtils.isBlank(newRoleMember.getRoleId())) { |
245 | 0 | throw new UnmarshalException("Cannot create role member without providing the role ID or role name + namespace that the member belongs to"); |
246 | |
} |
247 | 0 | } |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
private static void validateMemberIdentity(RoleMemberXmlDTO newRoleMember) throws UnmarshalException { |
253 | |
|
254 | 0 | MemberType memberType = newRoleMember.getMemberType(); |
255 | 0 | if (memberType == null) { |
256 | 0 | throw new UnmarshalException("Cannot create a role member with no member principal/group/role identification information specified"); |
257 | |
} |
258 | |
|
259 | |
|
260 | 0 | if (StringUtils.isNotBlank(newRoleMember.getMemberId())) { |
261 | 0 | if (MemberType.PRINCIPAL.equals(memberType)) { |
262 | |
|
263 | 0 | if (KimApiServiceLocator.getIdentityService().getPrincipal(newRoleMember.getPrincipalId()) == null) { |
264 | 0 | throw new UnmarshalException("Cannot create principal role member with principal ID \"" + |
265 | |
newRoleMember.getPrincipalId() + "\" because such a person does not exist"); |
266 | |
} |
267 | 0 | } else if (MemberType.GROUP.equals(memberType)) { |
268 | |
|
269 | 0 | if (KimApiServiceLocator.getGroupService().getGroup(newRoleMember.getGroupId()) == null) { |
270 | 0 | throw new UnmarshalException("Cannot create group role member with group ID \"" + |
271 | |
newRoleMember.getGroupId() + "\" because such a group does not exist"); |
272 | |
} |
273 | 0 | } else if (MemberType.ROLE.equals(memberType)) { |
274 | |
|
275 | 0 | if (newRoleMember.getRoleId().equals(newRoleMember.getRoleIdAsMember())) { |
276 | 0 | throw new UnmarshalException("The role with ID \"" + newRoleMember.getRoleIdAsMember() + "\" cannot be made a member of itself"); |
277 | 0 | } else if (KimApiServiceLocator.getRoleService().getRole(newRoleMember.getRoleIdAsMember()) == null) { |
278 | 0 | throw new UnmarshalException("Cannot use role with ID \"" + newRoleMember.getRoleIdAsMember() + |
279 | |
"\" as a role member because such a role does not exist"); |
280 | |
} |
281 | |
} |
282 | |
} |
283 | |
|
284 | |
|
285 | 0 | if (StringUtils.isNotBlank(newRoleMember.getMemberName())) { |
286 | 0 | if (MemberType.PRINCIPAL.equals(memberType)) { |
287 | |
|
288 | 0 | PrincipalContract tempPrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(newRoleMember.getPrincipalName()); |
289 | 0 | if (tempPrincipal == null) { |
290 | 0 | throw new UnmarshalException("Cannot create principal role member with principal name \"" + |
291 | |
newRoleMember.getPrincipalName() + "\" because such a person does not exist"); |
292 | 0 | } else if (StringUtils.isBlank(newRoleMember.getPrincipalId())) { |
293 | |
|
294 | 0 | newRoleMember.setPrincipalId(tempPrincipal.getPrincipalId()); |
295 | 0 | } else if (!newRoleMember.getPrincipalId().equals(tempPrincipal.getPrincipalId())) { |
296 | 0 | throw new UnmarshalException("Cannot create principal role member with principal ID \"" + newRoleMember.getPrincipalId() + |
297 | |
"\" and principal name \"" + newRoleMember.getPrincipalName() + "\" because the principal with that name has an ID of \"" + |
298 | |
tempPrincipal.getPrincipalId() + "\" instead"); |
299 | |
} |
300 | 0 | } else if (MemberType.GROUP.equals(memberType)) { |
301 | |
|
302 | 0 | NameAndNamespacePair groupNameAndNamespace = newRoleMember.getGroupName(); |
303 | 0 | GroupContract tempGroup = KimApiServiceLocator.getGroupService().getGroupByNameAndNamespaceCode( |
304 | |
groupNameAndNamespace.getNamespaceCode(), groupNameAndNamespace.getName()); |
305 | 0 | if (tempGroup == null) { |
306 | 0 | throw new UnmarshalException("Cannot create group role member with namespace \"" + groupNameAndNamespace.getNamespaceCode() + |
307 | |
"\" and name \"" + groupNameAndNamespace.getName() + "\" because such a group does not exist"); |
308 | 0 | } else if (StringUtils.isBlank(newRoleMember.getGroupId())) { |
309 | |
|
310 | 0 | newRoleMember.setGroupId(tempGroup.getId()); |
311 | 0 | } else if (!newRoleMember.getGroupId().equals(tempGroup.getId())) { |
312 | 0 | throw new UnmarshalException("Cannot create group role member with ID \"" + newRoleMember.getGroupId() + "\", namespace \"" + |
313 | |
groupNameAndNamespace.getNamespaceCode() + "\", and name \"" + groupNameAndNamespace.getName() + |
314 | |
"\" because the group with that namespace and name has an ID of \"" + tempGroup.getId() + "\" instead"); |
315 | |
} |
316 | 0 | } else if (MemberType.ROLE.equals(memberType)) { |
317 | |
|
318 | 0 | NameAndNamespacePair roleNameAndNamespace = newRoleMember.getRoleNameAsMember(); |
319 | 0 | RoleContract tempRole = KimApiServiceLocator.getRoleService().getRoleByNameAndNamespaceCode( |
320 | |
roleNameAndNamespace.getNamespaceCode(), roleNameAndNamespace.getName()); |
321 | 0 | if (tempRole == null) { |
322 | 0 | throw new UnmarshalException("Cannot use role with namespace \"" + roleNameAndNamespace.getNamespaceCode() + |
323 | |
"\" and name \"" + roleNameAndNamespace.getName() + "\" as a role member because such a role does not exist"); |
324 | 0 | } else if (newRoleMember.getRoleId().equals(tempRole.getId())) { |
325 | 0 | throw new UnmarshalException("The role with namespace \"" + roleNameAndNamespace.getNamespaceCode() + |
326 | |
"\" and name \"" + roleNameAndNamespace.getName() + "\" cannot be made a member of itself"); |
327 | 0 | } else if (StringUtils.isBlank(newRoleMember.getRoleId())) { |
328 | |
|
329 | 0 | newRoleMember.setRoleIdAsMember(tempRole.getId()); |
330 | 0 | } else if (!newRoleMember.getRoleId().equals(tempRole.getId())) { |
331 | 0 | throw new RuntimeException("Cannot use role with ID \"" + newRoleMember.getRoleId() + "\", namespace \"" + |
332 | |
roleNameAndNamespace.getNamespaceCode() + "\", and name \"" + roleNameAndNamespace.getName() + |
333 | |
"\" as a role member because the role with that namespace and name has an ID of \"" + |
334 | |
tempRole.getId() + "\" instead"); |
335 | |
} |
336 | |
} |
337 | |
} |
338 | |
|
339 | |
|
340 | 0 | if (StringUtils.isBlank(newRoleMember.getMemberId())) { |
341 | 0 | throw new RuntimeException("Cannot create a role member with no member principal/group/role identification information specified"); |
342 | |
} |
343 | |
|
344 | 0 | } |
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
private static void validateAndPrepareRolePermission(RolePermissionXmlDTO newRolePermission) throws UnmarshalException { |
350 | |
|
351 | |
|
352 | 0 | if (newRolePermission instanceof RolePermissionXmlDTO.OutsideOfRole) { |
353 | 0 | RolePermissionXmlDTO.OutsideOfRole standaloneRolePerm = (RolePermissionXmlDTO.OutsideOfRole) newRolePermission; |
354 | 0 | if (standaloneRolePerm.getRoleNameAndNamespace() != null) { |
355 | |
|
356 | 0 | String tempRoleId = KimApiServiceLocator.getRoleService().getRoleIdByNameAndNamespaceCode( |
357 | |
standaloneRolePerm.getRoleNamespaceCode(), standaloneRolePerm.getRoleName()); |
358 | 0 | if (StringUtils.isBlank(tempRoleId)) { |
359 | 0 | throw new UnmarshalException("Cannot assign permission to role with namespace \"" + standaloneRolePerm.getRoleNamespaceCode() + |
360 | |
"\" and name \"" + standaloneRolePerm.getRoleName() + "\" because that role does not exist"); |
361 | 0 | } else if (StringUtils.isBlank(standaloneRolePerm.getRoleId())) { |
362 | |
|
363 | 0 | standaloneRolePerm.setRoleId(standaloneRolePerm.getRoleId()); |
364 | 0 | } else if (!standaloneRolePerm.getRoleId().equals(tempRoleId)) { |
365 | 0 | throw new UnmarshalException("Cannot assign permission to role with ID \"" + standaloneRolePerm.getRoleId() + "\", namespace \"" + |
366 | |
standaloneRolePerm.getRoleNamespaceCode() + "\", and name \"" + standaloneRolePerm.getRoleName() + |
367 | |
"\" because the existing role with that name and namespace has an ID of \"" + tempRoleId + "\" instead"); |
368 | |
} |
369 | 0 | } else if (StringUtils.isBlank(standaloneRolePerm.getRoleId())) { |
370 | 0 | throw new UnmarshalException( |
371 | |
"Cannot assign permission to role without providing the role ID or role name + namespace that the permission is assigned to"); |
372 | 0 | } else if (KimApiServiceLocator.getRoleService().getRole(standaloneRolePerm.getRoleId()) == null) { |
373 | 0 | throw new UnmarshalException("Cannot assign permission to role with ID \"" + standaloneRolePerm.getRoleId() + |
374 | |
"\" because that role does not exist"); |
375 | |
} |
376 | |
} |
377 | |
|
378 | |
|
379 | 0 | if (StringUtils.isBlank(newRolePermission.getRoleId())) { |
380 | 0 | throw new UnmarshalException("Cannot assign permission to role without providing the role ID or role name + namespace that the permission is assigned to"); |
381 | |
} |
382 | |
|
383 | |
|
384 | 0 | if (newRolePermission.getPermissionNameAndNamespace() != null) { |
385 | 0 | PermissionContract permission = KimApiServiceLocator.getPermissionService().findPermByNamespaceCodeAndName( |
386 | |
newRolePermission.getPermissionNamespaceCode(), newRolePermission.getPermissionName()); |
387 | 0 | if (permission == null) { |
388 | 0 | throw new UnmarshalException("Cannot get role assigned to permission with namespace \"" + newRolePermission.getPermissionNamespaceCode() + |
389 | |
"\" and name \"" + newRolePermission.getPermissionName() + "\" because that permission does not exist"); |
390 | 0 | } else if (StringUtils.isBlank(newRolePermission.getPermissionId())) { |
391 | |
|
392 | 0 | newRolePermission.setPermissionId(permission.getId()); |
393 | 0 | } else if (!newRolePermission.getPermissionId().equals(permission.getId())) { |
394 | 0 | throw new UnmarshalException("Cannot get role assigned to permission with ID \"" + newRolePermission.getPermissionId() + "\", namespace \"" + |
395 | |
newRolePermission.getPermissionNamespaceCode() + "\", and name \"" + newRolePermission.getPermissionName() + |
396 | |
"\" because the existing permission with that name and namespace has an ID of \"" + permission.getId() + "\" instead"); |
397 | |
} |
398 | 0 | } else if (StringUtils.isBlank(newRolePermission.getPermissionId())) { |
399 | 0 | throw new UnmarshalException("Cannot assign permission to role without specifying the ID or name and namespace of the permission to assign"); |
400 | 0 | } else if (KimApiServiceLocator.getPermissionService().getPermission(newRolePermission.getPermissionId()) == null) { |
401 | 0 | throw new UnmarshalException("Cannot get role assigned to permission with ID \"" + newRolePermission.getPermissionId() + |
402 | |
"\" because that permission does not exist"); |
403 | |
} |
404 | 0 | } |
405 | |
|
406 | |
} |