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