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.util.Collections;
 31  
 import java.util.HashMap;
 32  
 import java.util.List;
 33  
 import java.util.Set;
 34  
 
 35  
 /**
 36  
  * Helper class containing static methods for aiding in parsing role XML.
 37  
  * 
 38  
  * <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?)
 39  
  * 
 40  
  * <p>TODO: Should this be converted into a service instead?
 41  
  * 
 42  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 43  
  */
 44  
 public final class RoleXmlUtil {
 45  
     // Do not allow outside code to instantiate this class.
 46  0
     private RoleXmlUtil() {}
 47  
 
 48  
     /**
 49  
      * Performs the necessary validation on the new role, then saves it.
 50  
      * 
 51  
      * @param newRole The role to persist.
 52  
      * @return The ID of the persisted role.
 53  
      * @throws IllegalArgumentException if newRole is null.
 54  
      * @throws UnmarshalException if newRole contains invalid data.
 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  
         // Validate the role and (if applicable) retrieve the ID from an existing matching role.
 62  0
         validateAndPrepareRole(newRole);
 63  
         
 64  
         // If necessary, assign a new role ID.
 65  0
         if (StringUtils.isBlank(newRole.getRoleId())) {
 66  0
             newRole.setRoleId(KimApiServiceLocator.getRoleUpdateService().getNextAvailableRoleId());
 67  
         }
 68  
         
 69  
         // Save the role.
 70  0
         KimApiServiceLocator.getRoleUpdateService().saveRole(newRole.getRoleId(), newRole.getRoleName(), newRole.getRoleDescription(), newRole.getActive().booleanValue(),
 71  
                 newRole.getKimTypeId(), newRole.getNamespaceCode());
 72  
         
 73  
         // 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.
 74  0
         newRole.setAlreadyPersisted(true);
 75  
         
 76  0
         return newRole.getRoleId();
 77  
     }
 78  
     
 79  
     /**
 80  
      * Performs the necessary validation on the new role member, then saves it.
 81  
      * 
 82  
      * @param newRoleMember The role member to save.
 83  
      * @return The ID of the persisted role member.
 84  
      * @throws IllegalArgumentException if newRoleMember is null.
 85  
      * @throws UnmarshalException if newRoleMember contains invalid data.
 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  
         // Validate role ID and role name/namespace.
 94  0
         validateRoleIdAndRoleNameForMember(newRoleMember);
 95  
         
 96  
         // Validate member type, member ID, and member name/namespace.
 97  0
         validateMemberIdentity(newRoleMember);
 98  
         
 99  
         // Validate the from/to dates, if defined.
 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  
         // Define defaults as needed.
 106  0
         if (newRoleMember.getQualifications() == null) {
 107  0
             newRoleMember.setQualifications(new HashMap<String, String>());
 108  
         }
 109  
         
 110  
         // Save the role member.
 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  
      * Performs the necessary validation on the role permission, then saves it.
 122  
      * 
 123  
      * @param newRolePermission The role permission to save.
 124  
      * @throws IllegalArgumentException if newRolePermission is null
 125  
      * @throws UnmarshalException if newRolePermission contains invalid data.
 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  
         // Validate the role permission, and prepare its role ID if necessary.
 133  0
         validateAndPrepareRolePermission(newRolePermission);
 134  
         
 135  
         // Save the role permission.
 136  0
         KimApiServiceLocator.getRoleUpdateService().assignPermissionToRole(newRolePermission.getPermissionId(), newRolePermission.getRoleId());
 137  0
     }
 138  
     
 139  
     /**
 140  
      * Removes any role members for a given role whose IDs are not listed in a given role member ID set.
 141  
      * 
 142  
      * @param roleId The ID of the role containing the role members.
 143  
      * @param existingMemberIds The IDs of the role members that should not be removed.
 144  
      * @throws IllegalArgumentException if roleId is blank or refers to a non-existent role, or if existingRoleMemberIds is null.
 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  
         // Remove any role members whose IDs are not in the set.
 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  
                     // If the role member needs to be removed, use the member type code to determine which removal method to call.
 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  
      * 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.
 182  
      */
 183  
     private static void validateAndPrepareRole(RoleXmlDTO newRole) throws UnmarshalException {
 184  
         // Ensure that the role name, role namespace, KIM type, and description have all been specified.
 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  
         // Attempt to find an existing matching role, and assign its ID to the validated role if it exists.
 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  
      * Validates a new role member's role ID, role name, and role namespace.
 202  
      */
 203  
     private static void validateRoleIdAndRoleNameForMember(RoleMemberXmlDTO newRoleMember) throws UnmarshalException {
 204  
         // If the "roleMember" tag was not a descendant of a "role" tag, derive and validate its role information accordingly.
 205  0
         if (newRoleMember instanceof RoleMemberXmlDTO.OutsideOfRole) {
 206  0
             RoleMemberXmlDTO.OutsideOfRole standaloneMember = (RoleMemberXmlDTO.OutsideOfRole) newRoleMember;
 207  0
             if (standaloneMember.getRoleNameAndNamespace() != null) {
 208  
                 // If a name + namespace combo is given, verify that the combo maps to an existing role.
 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  
                 // 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.
 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  
         // Ensure that a role ID was explicitly defined or was derived from a name + namespace combo.
 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  
      * Validates a new role member's member type, member ID, member name, and (if applicable) member namespace code.
 238  
      */
 239  
     private static void validateMemberIdentity(RoleMemberXmlDTO newRoleMember) throws UnmarshalException {
 240  
         // Ensure that sufficient and non-conflicting membership info has been set. (The getMemberTypeCode() method performs such validation.)
 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  
         // Ensure that a valid member ID was specified, if present.
 247  0
         if (StringUtils.isNotBlank(newRoleMember.getMemberId())) {
 248  0
             if (KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(memberTypeCode)) {
 249  
                 // If the member is a principal, ensure that the principal exists.
 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  
                 // If the member is a group, ensure that the group exists.
 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  
                 // If the member is another role, ensure that the role exists and that the role is not trying to become a member of itself.
 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  
         // Ensure that a valid member name (and namespace, if applicable) was specified, if present.
 272  0
         if (StringUtils.isNotBlank(newRoleMember.getMemberName())) {
 273  0
             if (KimUIConstants.MEMBER_TYPE_PRINCIPAL_CODE.equals(memberTypeCode)) {
 274  
                 //If the member is a principal, ensure that the principal exists and does not conflict with any existing principal ID information.
 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  
                     // If no principal ID was given, assign one from the retrieved principal.
 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  
                 // If the member is a group, ensure that the group exists and does not conflict with any existing group ID information.
 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  
                     // If no group ID was given, assign one from the retrieved group.
 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  
                 // 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.
 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  
                     // If no role ID was given, assign one from the retrieved role.
 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  
         // Ensure that a member ID was either explicitly defined or was derived from the member name (and namespace, if applicable).
 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  
      * Validates a role permission's role and permission identification information, and assigns its role ID if needed.
 335  
      */
 336  
     private static void validateAndPrepareRolePermission(RolePermissionXmlDTO newRolePermission) throws UnmarshalException {
 337  
         
 338  
         // If this is a standalone role permission, derive and validate its role information accordingly.
 339  0
         if (newRolePermission instanceof RolePermissionXmlDTO.OutsideOfRole) {
 340  0
             RolePermissionXmlDTO.OutsideOfRole standaloneRolePerm = (RolePermissionXmlDTO.OutsideOfRole) newRolePermission;
 341  0
             if (standaloneRolePerm.getRoleNameAndNamespace() != null) {
 342  
                 // If a role name + namespace is given, assign or validate the role ID accordingly.
 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  
                     // If no role ID was given, assign one from the retrieved role.
 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  
         // Ensure that a role ID was explicitly defined or was derived from a name + namespace combo.
 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  
         // If the permission is being identified by name and namespace, derive or validate its permission ID accordingly.
 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  
                 // If no permission ID was given, assign one from the retrieved permission.
 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  
 }