|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package org.kuali.rice.kew.impl.document.security;  | 
  |  17 |     | 
     | 
  |  18 |     | 
   import org.apache.commons.lang.StringUtils;  | 
  |  19 |     | 
   import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;  | 
  |  20 |     | 
   import org.kuali.rice.kew.api.document.Document;  | 
  |  21 |     | 
   import org.kuali.rice.kew.api.extension.ExtensionDefinition;  | 
  |  22 |     | 
   import org.kuali.rice.kew.api.extension.ExtensionRepositoryService;  | 
  |  23 |     | 
   import org.kuali.rice.kew.api.extension.ExtensionUtils;  | 
  |  24 |     | 
   import org.kuali.rice.kew.framework.document.security.DocumentSecurityDirective;  | 
  |  25 |     | 
   import org.kuali.rice.kew.framework.document.security.DocumentSecurityHandlerService;  | 
  |  26 |     | 
   import org.kuali.rice.kew.framework.document.security.DocumentSecurityAttribute;  | 
  |  27 |     | 
     | 
  |  28 |     | 
   import java.util.ArrayList;  | 
  |  29 |     | 
   import java.util.Collections;  | 
  |  30 |     | 
   import java.util.HashMap;  | 
  |  31 |     | 
   import java.util.List;  | 
  |  32 |     | 
   import java.util.Map;  | 
  |  33 |     | 
     | 
  |  34 |     | 
     | 
  |  35 |     | 
     | 
  |  36 |     | 
     | 
  |  37 |     | 
     | 
  |  38 |     | 
     | 
  |  39 |    0 |    public class DocumentSecurityHandlerServiceImpl implements DocumentSecurityHandlerService { | 
  |  40 |     | 
     | 
  |  41 |     | 
       private ExtensionRepositoryService extensionRepositoryService;  | 
  |  42 |     | 
     | 
  |  43 |     | 
       @Override  | 
  |  44 |     | 
       public List<String> getAuthorizedDocumentIds(String principalId, List<DocumentSecurityDirective> documentSecurityDirectives ) { | 
  |  45 |    0 |            if (StringUtils.isBlank(principalId)) { | 
  |  46 |    0 |                throw new RiceIllegalArgumentException("principalId was null or blank"); | 
  |  47 |     | 
           }  | 
  |  48 |    0 |            if (documentSecurityDirectives == null) { | 
  |  49 |    0 |                documentSecurityDirectives = Collections.emptyList();  | 
  |  50 |     | 
           }  | 
  |  51 |    0 |            List<String> authorizedDocumentIds = new ArrayList<String>();  | 
  |  52 |    0 |            Map<String, DocumentSecurityAttribute> securityAttributeCache = new HashMap<String, DocumentSecurityAttribute>();  | 
  |  53 |    0 |            for (DocumentSecurityDirective documentSecurityDirective : documentSecurityDirectives) { | 
  |  54 |    0 |                List<DocumentSecurityAttribute> attributesToApply = loadSecurityAttributes(documentSecurityDirective, securityAttributeCache);  | 
  |  55 |    0 |                for (Document document : documentSecurityDirective.getDocuments()) { | 
  |  56 |     | 
                     | 
  |  57 |    0 |                    if (!authorizedDocumentIds.contains(document.getDocumentId())) { | 
  |  58 |    0 |                        for (DocumentSecurityAttribute securityAttribute : attributesToApply) { | 
  |  59 |    0 |                            if (securityAttribute.isAuthorizedForDocument(principalId, document)) { | 
  |  60 |    0 |                                authorizedDocumentIds.add(document.getDocumentId());  | 
  |  61 |    0 |                                break;  | 
  |  62 |     | 
                           }  | 
  |  63 |     | 
                       }  | 
  |  64 |     | 
                   }  | 
  |  65 |     | 
               }  | 
  |  66 |    0 |            }  | 
  |  67 |    0 |            return authorizedDocumentIds;  | 
  |  68 |     | 
       }  | 
  |  69 |     | 
     | 
  |  70 |     | 
       protected List<DocumentSecurityAttribute> loadSecurityAttributes(DocumentSecurityDirective documentSecurityDirective,  | 
  |  71 |     | 
               Map<String, DocumentSecurityAttribute> securityAttributeCache) { | 
  |  72 |    0 |            List<DocumentSecurityAttribute> securityAttributes = new ArrayList<DocumentSecurityAttribute>();  | 
  |  73 |    0 |            for (String documentSecurityAttributeName : documentSecurityDirective.getDocumentSecurityAttributeNames()) { | 
  |  74 |    0 |                securityAttributes.add(loadAndCacheSecurityAttribute(documentSecurityAttributeName, securityAttributeCache));  | 
  |  75 |     | 
           }  | 
  |  76 |    0 |            return securityAttributes;  | 
  |  77 |     | 
       }  | 
  |  78 |     | 
     | 
  |  79 |     | 
       protected DocumentSecurityAttribute loadAndCacheSecurityAttribute(String securityAttributeName, Map<String, DocumentSecurityAttribute> securityAttributeCache) { | 
  |  80 |    0 |            if (securityAttributeCache.containsKey(securityAttributeName)) { | 
  |  81 |    0 |                return securityAttributeCache.get(securityAttributeName);  | 
  |  82 |     | 
           }  | 
  |  83 |    0 |            ExtensionDefinition extensionDefinition = extensionRepositoryService.getExtensionByName(securityAttributeName);  | 
  |  84 |    0 |            if (extensionDefinition == null) { | 
  |  85 |    0 |                throw new RiceIllegalArgumentException("Failed to locate a SecurityAttribute with the given name: " + securityAttributeName); | 
  |  86 |     | 
           }  | 
  |  87 |    0 |            DocumentSecurityAttribute securityAttribute = loadSecurityAttribute(extensionDefinition);  | 
  |  88 |    0 |            securityAttributeCache.put(securityAttributeName, securityAttribute);  | 
  |  89 |    0 |            return securityAttribute;  | 
  |  90 |     | 
       }  | 
  |  91 |     | 
     | 
  |  92 |     | 
       protected DocumentSecurityAttribute loadSecurityAttribute(ExtensionDefinition extensionDefinition) { | 
  |  93 |    0 |            Object securityAttribute = ExtensionUtils.loadExtension(extensionDefinition);  | 
  |  94 |    0 |            if (securityAttribute == null) { | 
  |  95 |    0 |                throw new RiceIllegalArgumentException("Failed to load SecurityAttribute for: " + extensionDefinition); | 
  |  96 |     | 
           }  | 
  |  97 |    0 |            return (DocumentSecurityAttribute)securityAttribute;  | 
  |  98 |     | 
       }  | 
  |  99 |     | 
     | 
  |  100 |     | 
       public ExtensionRepositoryService getExtensionRepositoryService() { | 
  |  101 |    0 |            return extensionRepositoryService;  | 
  |  102 |     | 
       }  | 
  |  103 |     | 
     | 
  |  104 |     | 
       public void setExtensionRepositoryService(ExtensionRepositoryService extensionRepositoryService) { | 
  |  105 |    0 |            this.extensionRepositoryService = extensionRepositoryService;  | 
  |  106 |    0 |        }  | 
  |  107 |     | 
     | 
  |  108 |     | 
   }  |