| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kim.document.rule; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.kuali.rice.core.util.AttributeSet; |
| 20 | |
import org.kuali.rice.core.util.RiceKeyConstants; |
| 21 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
| 22 | |
import org.kuali.rice.kim.api.type.KimType; |
| 23 | |
import org.kuali.rice.kim.bo.impl.GenericPermission; |
| 24 | |
import org.kuali.rice.kim.bo.role.dto.KimPermissionTemplateInfo; |
| 25 | |
import org.kuali.rice.kim.service.KIMServiceLocatorInternal; |
| 26 | |
import org.kuali.rice.kim.service.support.KimPermissionTypeService; |
| 27 | |
import org.kuali.rice.kns.document.MaintenanceDocument; |
| 28 | |
import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase; |
| 29 | |
import org.kuali.rice.kns.util.GlobalVariables; |
| 30 | |
|
| 31 | |
import java.util.regex.Matcher; |
| 32 | |
import java.util.regex.Pattern; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 0 | public class GenericPermissionMaintenanceDocumentRule extends |
| 41 | |
MaintenanceDocumentRuleBase { |
| 42 | |
protected static final String DETAIL_VALUES_PROPERTY = "detailValues"; |
| 43 | |
protected static final String ERROR_MESSAGE_PREFIX = "error.document.kim.genericpermission."; |
| 44 | |
protected static final String ERROR_MISSING_TEMPLATE = ERROR_MESSAGE_PREFIX + "missingtemplate"; |
| 45 | |
protected static final String ERROR_UNKNOWN_ATTRIBUTE = ERROR_MESSAGE_PREFIX + "unknownattribute"; |
| 46 | |
protected static final String ERROR_ATTRIBUTE_VALIDATION = ERROR_MESSAGE_PREFIX + "attributevalidation"; |
| 47 | |
|
| 48 | |
@Override |
| 49 | |
protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) { |
| 50 | 0 | boolean rulesPassed = true; |
| 51 | |
try { |
| 52 | 0 | GenericPermission perm = (GenericPermission)getNewBo(); |
| 53 | 0 | validateDetailValuesFormat(perm.getDetailValues()); |
| 54 | |
|
| 55 | |
|
| 56 | 0 | KimPermissionTemplateInfo template = KimApiServiceLocator.getPermissionService().getPermissionTemplate( perm.getTemplateId() ); |
| 57 | 0 | if ( template == null ) { |
| 58 | 0 | GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH ); |
| 59 | 0 | GlobalVariables.getMessageMap().putError( DETAIL_VALUES_PROPERTY, ERROR_MISSING_TEMPLATE, perm.getTemplateId() ); |
| 60 | 0 | GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH ); |
| 61 | 0 | rulesPassed = false; |
| 62 | |
} else { |
| 63 | 0 | KimType kimType = KimApiServiceLocator.getKimTypeInfoService().getKimType(template.getKimTypeId()); |
| 64 | 0 | AttributeSet details = perm.getDetails(); |
| 65 | |
|
| 66 | 0 | for ( String attributeName : details.keySet() ) { |
| 67 | 0 | if ( kimType.getAttributeDefinitionByName(attributeName) == null ) { |
| 68 | 0 | GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH ); |
| 69 | 0 | GlobalVariables.getMessageMap().putError( DETAIL_VALUES_PROPERTY, ERROR_UNKNOWN_ATTRIBUTE, attributeName, template.getNamespaceCode(), template.getName() ); |
| 70 | 0 | GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH ); |
| 71 | 0 | rulesPassed = false; |
| 72 | |
} |
| 73 | |
} |
| 74 | |
|
| 75 | 0 | if ( !GlobalVariables.getMessageMap().hasErrors() ) { |
| 76 | 0 | KimPermissionTypeService service = getPermissionTypeService( kimType.getServiceName() ); |
| 77 | 0 | if ( service != null ) { |
| 78 | 0 | AttributeSet validationErrors = service.validateAttributes( kimType.getId(), details); |
| 79 | 0 | if ( validationErrors != null && !validationErrors.isEmpty() ) { |
| 80 | 0 | for ( String attributeName : validationErrors.keySet() ) { |
| 81 | 0 | GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH ); |
| 82 | 0 | GlobalVariables.getMessageMap().putError( DETAIL_VALUES_PROPERTY, ERROR_ATTRIBUTE_VALIDATION, attributeName, validationErrors.get(attributeName) ); |
| 83 | 0 | GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH ); |
| 84 | |
} |
| 85 | 0 | rulesPassed = false; |
| 86 | |
} |
| 87 | |
} |
| 88 | |
} |
| 89 | |
} |
| 90 | |
|
| 91 | 0 | } catch ( RuntimeException ex ) { |
| 92 | 0 | LOG.error( "Error in processCustomRouteDocumentBusinessRules()", ex ); |
| 93 | 0 | throw ex; |
| 94 | 0 | } |
| 95 | 0 | return rulesPassed; |
| 96 | |
} |
| 97 | |
|
| 98 | |
protected boolean validateDetailValuesFormat(String permissionDetailValues){ |
| 99 | 0 | if(permissionDetailValues != null){ |
| 100 | 0 | String spacesPattern = "[\\s\\t]*"; |
| 101 | 0 | Pattern pattern = Pattern.compile(".+"+"="+".+"); |
| 102 | |
Matcher matcher; |
| 103 | |
|
| 104 | 0 | permissionDetailValues = permissionDetailValues.replace( "\r\n", "\n" ); |
| 105 | 0 | permissionDetailValues = permissionDetailValues.replace( '\r', '\n' ); |
| 106 | 0 | if(StringUtils.isNotBlank(permissionDetailValues)){ |
| 107 | 0 | String[] values = permissionDetailValues.split( "\n" ); |
| 108 | 0 | for(String attrib: values){ |
| 109 | 0 | matcher = pattern.matcher(attrib); |
| 110 | 0 | if(!matcher.matches()){ |
| 111 | 0 | GlobalVariables.getMessageMap().putError(MAINTAINABLE_ERROR_PATH+"."+DETAIL_VALUES_PROPERTY, RiceKeyConstants.ERROR_INVALID_FORMAT, new String[]{"Detail Values", permissionDetailValues}); |
| 112 | 0 | return false; |
| 113 | |
} |
| 114 | |
} |
| 115 | |
} |
| 116 | |
} |
| 117 | 0 | return true; |
| 118 | |
} |
| 119 | |
|
| 120 | |
protected KimPermissionTypeService getPermissionTypeService( String serviceName ) { |
| 121 | 0 | if ( StringUtils.isBlank( serviceName ) ) { |
| 122 | 0 | return null; |
| 123 | |
} |
| 124 | |
try { |
| 125 | 0 | Object service = KIMServiceLocatorInternal.getService(serviceName); |
| 126 | |
|
| 127 | 0 | if ( service == null ) { |
| 128 | 0 | LOG.warn("null returned for permission type service for service name: " + serviceName); |
| 129 | |
} else { |
| 130 | |
|
| 131 | 0 | if ( !(service instanceof KimPermissionTypeService) ) { |
| 132 | 0 | LOG.warn( "Service " + serviceName + " was not a KimPermissionTypeService. Was: " + service.getClass().getName() ); |
| 133 | 0 | service = null; |
| 134 | |
} |
| 135 | |
} |
| 136 | 0 | return (KimPermissionTypeService)service; |
| 137 | 0 | } catch( Exception ex ) { |
| 138 | 0 | LOG.error( "Error retrieving service: " + serviceName + " from the KIMServiceLocatorInternal.", ex ); |
| 139 | |
} |
| 140 | 0 | return null; |
| 141 | |
} |
| 142 | |
|
| 143 | |
} |