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