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