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