Coverage Report - org.kuali.rice.kim.impl.jaxb.RoleXmlUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
RoleXmlUtil
0%
0/148
0%
0/148
13.333
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl1.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * Helper class containing static methods for aiding in parsing role XML.
 38  
  * 
 39  
  * <p>All non-private methods are package-private so that only the KIM-parsing-related code can make use of them. (TODO: Is that necessary?)
 40  
  * 
 41  
  * <p>TODO: Should this be converted into a service instead?
 42  
  * 
 43  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 44  
  */
 45  
 public final class RoleXmlUtil {
 46  
     // Do not allow outside code to instantiate this class.
 47  0
     private RoleXmlUtil() {}
 48  
 
 49  
     /**
 50  
      * Performs the necessary validation on the new role, then saves it.
 51  
      * 
 52  
      * @param newRole The role to persist.
 53  
      * @return The ID of the persisted role.
 54  
      * @throws IllegalArgumentException if newRole is null.
 55  
      * @throws UnmarshalException if newRole contains invalid data.
 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  
         // Validate the role and (if applicable) retrieve the ID from an existing matching role.
 63  0
         validateAndPrepareRole(newRole);
 64  
         
 65  
         // If necessary, assign a new role ID.
 66  0
         if (StringUtils.isBlank(newRole.getRoleId())) {
 67  0
             newRole.setRoleId(KimApiServiceLocator.getRoleUpdateService().getNextAvailableRoleId());
 68  
         }
 69  
         
 70  
         // Save the role.
 71  0
         KimApiServiceLocator.getRoleUpdateService().saveRole(newRole.getRoleId(), newRole.getRoleName(), newRole.getRoleDescription(), newRole.getActive().booleanValue(),
 72  
                 newRole.getKimTypeId(), newRole.getNamespaceCode());
 73  
         
 74  
         // Set a flag on the role to indicate that it has now been persisted so that the unmarshalling process will not save this role more than once.
 75  0
         newRole.setAlreadyPersisted(true);
 76  
         
 77  0
         return newRole.getRoleId();
 78  
     }
 79  
     
 80  
     /**
 81  
      * Performs the necessary validation on the new role member, then saves it.
 82  
      * 
 83  
      * @param newRoleMember The role member to save.
 84  
      * @return The ID of the persisted role member.
 85  
      * @throws IllegalArgumentException if newRoleMember is null.
 86  
      * @throws UnmarshalException if newRoleMember contains invalid data.
 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  
         // Validate role ID and role name/namespace.
 95  0
         validateRoleIdAndRoleNameForMember(newRoleMember);
 96  
         
 97  
         // Validate member type, member ID, and member name/namespace.
 98  0
         validateMemberIdentity(newRoleMember);
 99  
         
 100  
         // Validate the from/to dates, if defined.
 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  
         // Define defaults as needed.
 107  0
         if (newRoleMember.getQualifications() == null) {
 108  0
             newRoleMember.setQualifications(new HashMap<String, String>());
 109  
         }
 110  
         
 111  
         // Save the role member.
 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  
      * Performs the necessary validation on the role permission, then saves it.
 123  
      * 
 124  
      * @param newRolePermission The role permission to save.
 125  
      * @throws IllegalArgumentException if newRolePermission is null
 126  
      * @throws UnmarshalException if newRolePermission contains invalid data.
 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  
         // Validate the role permission, and prepare its role ID if necessary.
 134  0
         validateAndPrepareRolePermission(newRolePermission);
 135  
         
 136  
         // Save the role permission.
 137  0
         KimApiServiceLocator.getRoleUpdateService().assignPermissionToRole(newRolePermission.getPermissionId(), newRolePermission.getRoleId());
 138  0
     }
 139  
     
 140  
     /**
 141  
      * Removes any role members for a given role whose IDs are not listed in a given role member ID set.
 142  
      * 
 143  
      * @param roleId The ID of the role containing the role members.
 144  
      * @param existingMemberIds The IDs of the role members that should not be removed.
 145  
      * @throws IllegalArgumentException if roleId is blank or refers to a non-existent role, or if existingRoleMemberIds is null.
 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  
         // Remove any role members whose IDs are not in the set.
 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  
                     // If the role member needs to be removed, use the member type code to determine which removal method to call.
 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  
      * Validates a new role's name, namespace, KIM type, and description, and sets the role's ID if the name and namespace match an existing role.
 183  
      */
 184  
     private static void validateAndPrepareRole(RoleXmlDTO newRole) throws UnmarshalException {
 185  
         // Ensure that the role name, role namespace, KIM type, and description have all been specified.
 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  
         // Attempt to find an existing matching role, and assign its ID to the validated role if it exists.
 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  
      * Validates a new role member's role ID, role name, and role namespace.
 203  
      */
 204  
     private static void validateRoleIdAndRoleNameForMember(RoleMemberXmlDTO newRoleMember) throws UnmarshalException {
 205  
         // If the "roleMember" tag was not a descendant of a "role" tag, derive and validate its role information accordingly.
 206  0
         if (newRoleMember instanceof RoleMemberXmlDTO.OutsideOfRole) {
 207  0
             RoleMemberXmlDTO.OutsideOfRole standaloneMember = (RoleMemberXmlDTO.OutsideOfRole) newRoleMember;
 208  0
             if (standaloneMember.getRoleNameAndNamespace() != null) {
 209  
                 // If a name + namespace combo is given, verify that the combo maps to an existing role.
 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  
                 // If the role member defines its own role ID, verify that it's the same as the one from the existing role; otherwise, assign the member's role ID.
 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  
         // Ensure that a role ID was explicitly defined or was derived from a name + namespace combo.
 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  
      * Validates a new role member's member type, member ID, member name, and (if applicable) member namespace code.
 239  
      */
 240  
     private static void validateMemberIdentity(RoleMemberXmlDTO newRoleMember) throws UnmarshalException {
 241  
         // Ensure that sufficient and non-conflicting membership info has been set. (The getMemberTypeCode() method performs such validation.)
 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  
         // Ensure that a valid member ID was specified, if present.
 248  0
         if (StringUtils.isNotBlank(newRoleMember.getMemberId())) {
 249  0
             if (KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(memberTypeCode)) {
 250  
                 // If the member is a principal, ensure that the principal exists.
 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  
                 // If the member is a group, ensure that the group exists.
 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  
                 // If the member is another role, ensure that the role exists and that the role is not trying to become a member of itself.
 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  
         // Ensure that a valid member name (and namespace, if applicable) was specified, if present.
 273  0
         if (StringUtils.isNotBlank(newRoleMember.getMemberName())) {
 274  0
             if (KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(memberTypeCode)) {
 275  
                 //If the member is a principal, ensure that the principal exists and does not conflict with any existing principal ID information.
 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  
                     // If no principal ID was given, assign one from the retrieved principal.
 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  
                 // If the member is a group, ensure that the group exists and does not conflict with any existing group ID information.
 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  
                     // If no group ID was given, assign one from the retrieved group.
 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  
                 // If the member is another role, ensure that the role exists, does not conflict with any existing role ID information, and is not the member's role.
 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  
                     // If no role ID was given, assign one from the retrieved role.
 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  
         // Ensure that a member ID was either explicitly defined or was derived from the member name (and namespace, if applicable).
 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  
      * Validates a role permission's role and permission identification information, and assigns its role ID if needed.
 336  
      */
 337  
     private static void validateAndPrepareRolePermission(RolePermissionXmlDTO newRolePermission) throws UnmarshalException {
 338  
         
 339  
         // If this is a standalone role permission, derive and validate its role information accordingly.
 340  0
         if (newRolePermission instanceof RolePermissionXmlDTO.OutsideOfRole) {
 341  0
             RolePermissionXmlDTO.OutsideOfRole standaloneRolePerm = (RolePermissionXmlDTO.OutsideOfRole) newRolePermission;
 342  0
             if (standaloneRolePerm.getRoleNameAndNamespace() != null) {
 343  
                 // If a role name + namespace is given, assign or validate the role ID accordingly.
 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  
                     // If no role ID was given, assign one from the retrieved role.
 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  
         // Ensure that a role ID was explicitly defined or was derived from a name + namespace combo.
 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  
         // If the permission is being identified by name and namespace, derive or validate its permission ID accordingly.
 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  
                 // If no permission ID was given, assign one from the retrieved permission.
 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  
 }