Coverage Report - org.kuali.rice.kew.doctype.DocumentTypeSecurity
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentTypeSecurity
0%
0/128
0%
0/80
3.474
 
 1  
 /*
 2  
  * Copyright 2008-2009 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.kew.doctype;
 17  
 
 18  
 import java.io.BufferedReader;
 19  
 import java.io.IOException;
 20  
 import java.io.Serializable;
 21  
 import java.io.StringReader;
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 
 25  
 import javax.xml.parsers.DocumentBuilderFactory;
 26  
 import javax.xml.parsers.ParserConfigurationException;
 27  
 import javax.xml.xpath.XPath;
 28  
 import javax.xml.xpath.XPathConstants;
 29  
 
 30  
 import org.apache.commons.lang.StringUtils;
 31  
 import org.kuali.rice.core.reflect.ObjectDefinition;
 32  
 import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
 33  
 import org.kuali.rice.kew.exception.WorkflowException;
 34  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 35  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 36  
 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
 37  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 38  
 import org.kuali.rice.kew.util.Utilities;
 39  
 import org.kuali.rice.kew.web.KeyValue;
 40  
 import org.kuali.rice.kew.xml.XmlConstants;
 41  
 import org.kuali.rice.kim.bo.Group;
 42  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 43  
 import org.w3c.dom.Element;
 44  
 import org.w3c.dom.NamedNodeMap;
 45  
 import org.w3c.dom.Node;
 46  
 import org.w3c.dom.NodeList;
 47  
 import org.xml.sax.InputSource;
 48  
 import org.xml.sax.SAXException;
 49  
 
 50  
 
 51  
 public class DocumentTypeSecurity implements Serializable {
 52  
 
 53  
   private static final long serialVersionUID = -1886779857180381404L;
 54  
 
 55  0
   private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentTypeSecurity.class);
 56  
   
 57  
   private Boolean active;
 58  
 
 59  
   private Boolean initiatorOk;
 60  
   private Boolean routeLogAuthenticatedOk;
 61  0
   private List<KeyValue> searchableAttributes = new ArrayList<KeyValue>();
 62  0
   private List<Group> workgroups = new ArrayList<Group>();
 63  0
   private List<String> allowedRoles = new ArrayList<String>();
 64  0
   private List<String> disallowedRoles = new ArrayList<String>();
 65  0
   private List<SecurityAttribute> securityAttributes = new ArrayList<SecurityAttribute>();
 66  
 
 67  0
   private static XPath xpath = XPathHelper.newXPath();
 68  
 
 69  0
   public DocumentTypeSecurity() {}
 70  
 
 71  
   /** parse <security> XML to populate security object
 72  
    * @throws ParserConfigurationException
 73  
    * @throws IOException
 74  
    * @throws SAXException */
 75  
   public DocumentTypeSecurity(String standardServiceNamespace, String documentTypeSecurityXml)
 76  0
   {
 77  
     try {
 78  0
       if (Utilities.isEmpty(documentTypeSecurityXml)) {
 79  0
         return;
 80  
       }
 81  
 
 82  0
       InputSource inputSource = new InputSource(new BufferedReader(new StringReader(documentTypeSecurityXml)));
 83  0
       Element securityElement = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource).getDocumentElement();
 84  
 
 85  0
       String active = (String) xpath.evaluate("./@active", securityElement, XPathConstants.STRING);
 86  0
       if (Utilities.isEmpty(active) || "true".equals(active.toLowerCase())) {
 87  
         // true is the default
 88  0
         this.setActive(Boolean.valueOf(true));
 89  
       }
 90  
       else {
 91  0
         this.setActive(Boolean.valueOf(false));
 92  
       }
 93  
 
 94  
       // there should only be one <initiator> tag
 95  0
       NodeList initiatorNodes = (NodeList) xpath.evaluate("./initiator", securityElement, XPathConstants.NODESET);
 96  0
       if (initiatorNodes != null && initiatorNodes.getLength()>0) {
 97  0
         Node initiatorNode = initiatorNodes.item(0);
 98  0
         String value = initiatorNode.getTextContent();
 99  0
         if (Utilities.isEmpty(value) || value.toLowerCase().equals("true")) {
 100  0
           this.setInitiatorOk(Boolean.valueOf(true));
 101  
         }
 102  
         else {
 103  0
           this.initiatorOk = Boolean.valueOf(false);
 104  
         }
 105  
       }
 106  
 
 107  
       // there should only be one <routeLogAuthenticated> tag
 108  0
       NodeList routeLogAuthNodes = (NodeList) xpath.evaluate("./routeLogAuthenticated", securityElement, XPathConstants.NODESET);
 109  0
       if (routeLogAuthNodes != null && routeLogAuthNodes.getLength()>0) {
 110  0
         Node routeLogAuthNode = routeLogAuthNodes.item(0);
 111  0
         String value = routeLogAuthNode.getTextContent();
 112  0
         if (Utilities.isEmpty(value) || value.toLowerCase().equals("true")) {
 113  0
           this.routeLogAuthenticatedOk = Boolean.valueOf(true);
 114  
         }
 115  
         else {
 116  0
           this.routeLogAuthenticatedOk = Boolean.valueOf(false);
 117  
         }
 118  
       }
 119  
 
 120  0
       NodeList searchableAttributeNodes = (NodeList) xpath.evaluate("./searchableAttribute", securityElement, XPathConstants.NODESET);
 121  0
       if (searchableAttributeNodes != null && searchableAttributeNodes.getLength()>0) {
 122  0
         for (int i = 0; i < searchableAttributeNodes.getLength(); i++) {
 123  0
           Node searchableAttributeNode = searchableAttributeNodes.item(i);
 124  0
           String name = (String) xpath.evaluate("./@name", searchableAttributeNode, XPathConstants.STRING);
 125  0
           String idType = (String) xpath.evaluate("./@idType", searchableAttributeNode, XPathConstants.STRING);
 126  0
           if (!Utilities.isEmpty(name) && !Utilities.isEmpty(idType)) {
 127  0
             KeyValue searchableAttribute = new KeyValue(name, idType);
 128  0
             searchableAttributes.add(searchableAttribute);
 129  
           }
 130  
         }
 131  
       }
 132  
 
 133  0
       NodeList workgroupNodes = (NodeList) xpath.evaluate("./workgroup", securityElement, XPathConstants.NODESET);
 134  0
       if (workgroupNodes != null && workgroupNodes.getLength()>0) {
 135  0
             LOG.warn("Document Type Security XML is using deprecated element 'workgroup', please use 'groupName' instead.");
 136  0
         for (int i = 0; i < workgroupNodes.getLength(); i++) {
 137  0
           Node workgroupNode = workgroupNodes.item(i);
 138  0
           String value = workgroupNode.getTextContent().trim();
 139  0
           if (!Utilities.isEmpty(value)) {
 140  0
                 value = Utilities.substituteConfigParameters(value);
 141  0
             String namespaceCode = Utilities.parseGroupNamespaceCode(value);
 142  0
             String groupName = Utilities.parseGroupName(value);
 143  0
                 Group groupObject = KIMServiceLocator.getIdentityManagementService().getGroupByName(namespaceCode, groupName);
 144  0
                 if (groupObject == null) {
 145  0
                         throw new WorkflowException("Could not find group: " + value);
 146  
                 }
 147  0
             workgroups.add(groupObject);
 148  
           }
 149  
         }
 150  
       }
 151  
 
 152  0
       NodeList groupNodes = (NodeList) xpath.evaluate("./groupName", securityElement, XPathConstants.NODESET);
 153  0
       if (groupNodes != null && groupNodes.getLength()>0) {
 154  0
         for (int i = 0; i < groupNodes.getLength(); i++) {
 155  0
           Node groupNode = groupNodes.item(i);
 156  0
           if (groupNode.getNodeType() == Node.ELEMENT_NODE) {
 157  0
                 String groupName = groupNode.getTextContent().trim();
 158  0
             if (!Utilities.isEmpty(groupName)) {
 159  0
               groupName = Utilities.substituteConfigParameters(groupName).trim();
 160  0
               String namespaceCode = Utilities.substituteConfigParameters(((Element) groupNode).getAttribute(XmlConstants.NAMESPACE)).trim();
 161  0
               Group groupObject = KIMServiceLocator.getIdentityManagementService().getGroupByName(namespaceCode, groupName);
 162  
               
 163  
               
 164  0
               if (groupObject != null) {
 165  0
                       workgroups.add(groupObject); 
 166  
               } else {
 167  0
                       LOG.warn("Could not find group with name '" + groupName + "' and namespace '" + namespaceCode + "' which was defined on Document Type security");
 168  
               }
 169  
 //                if (groupObject == null) {
 170  
 //                  throw new WorkflowException("Could not find group with name '" + groupName + "' and namespace '" + namespaceCode + "'");
 171  
 //                }
 172  
          
 173  
               
 174  
             }
 175  
           }
 176  
         }
 177  
       }
 178  
 
 179  0
       NodeList roleNodes = (NodeList) xpath.evaluate("./role", securityElement, XPathConstants.NODESET);
 180  0
       if (roleNodes != null && roleNodes.getLength()>0) {
 181  0
         for (int i = 0; i < roleNodes.getLength(); i++) {
 182  0
           Element roleElement = (Element)roleNodes.item(i);
 183  0
           String value = roleElement.getTextContent().trim();
 184  0
           String allowedValue = roleElement.getAttribute("allowed");
 185  0
           if (StringUtils.isBlank(allowedValue)) {
 186  0
                   allowedValue = "true";
 187  
           }
 188  0
           if (!Utilities.isEmpty(value)) {
 189  0
                   if (Boolean.parseBoolean(allowedValue)) {
 190  0
                           allowedRoles.add(value);
 191  
                   } else {
 192  0
                           disallowedRoles.add(value);
 193  
                   }
 194  
           }
 195  
         }
 196  
       }
 197  
 
 198  0
       NodeList attributeNodes = (NodeList) xpath.evaluate("./securityAttribute", securityElement, XPathConstants.NODESET);
 199  0
       if (attributeNodes != null && attributeNodes.getLength()>0) {
 200  0
           for (int i = 0; i < attributeNodes.getLength(); i++) {
 201  0
             Element attributeElement = (Element)attributeNodes.item(i);
 202  0
             NamedNodeMap elemAttributes = attributeElement.getAttributes();
 203  0
             String className = null;
 204  0
             String serviceNamespace = standardServiceNamespace;
 205  0
             if (elemAttributes.getNamedItem("name") != null) {
 206  
                 // found a name attribute so find the class name
 207  0
                 String ruleAttributeName = elemAttributes.getNamedItem("name").getNodeValue().trim();
 208  0
                 RuleAttribute ruleAttribute = KEWServiceLocator.getRuleAttributeService().findByName(ruleAttributeName);
 209  0
                 if (ruleAttribute == null) {
 210  0
                     throw new WorkflowException("Could not find rule attribute: " + ruleAttributeName);
 211  
                 }
 212  0
                 serviceNamespace = ruleAttribute.getServiceNamespace();
 213  0
                 className = ruleAttribute.getClassName();
 214  0
             } else if (elemAttributes.getNamedItem("class") != null) {
 215  
                 // class name defined
 216  0
                 className = elemAttributes.getNamedItem("class").getNodeValue().trim();
 217  
             } else {
 218  0
                 throw new WorkflowException("Cannot find attribute 'name' or attribute 'class' for securityAttribute Node");
 219  
             }
 220  
           
 221  0
             this.securityAttributes.add(new LazyLoadSecurityAttribute(className, serviceNamespace));
 222  
             
 223  
           }
 224  
         }
 225  0
     } catch (Exception err) {
 226  0
       throw new WorkflowRuntimeException(err);
 227  0
     }
 228  0
   }
 229  
 
 230  
   public List<SecurityAttribute> getSecurityAttributes() {
 231  0
     return this.securityAttributes;
 232  
   }
 233  
 
 234  
   public void setSecurityAttributes(List<SecurityAttribute> securityAttributes) {
 235  0
     this.securityAttributes = securityAttributes;
 236  0
   }
 237  
 
 238  
   public Boolean getInitiatorOk() {
 239  0
     return initiatorOk;
 240  
   }
 241  
   public void setInitiatorOk(Boolean initiatorOk) {
 242  0
     this.initiatorOk = initiatorOk;
 243  0
   }
 244  
 
 245  
   public Boolean getRouteLogAuthenticatedOk() {
 246  0
     return routeLogAuthenticatedOk;
 247  
   }
 248  
   public void setRouteLogAuthenticatedOk(Boolean routeLogAuthenticatedOk) {
 249  0
     this.routeLogAuthenticatedOk = routeLogAuthenticatedOk;
 250  0
   }
 251  
 
 252  
   public List<String> getAllowedRoles() {
 253  0
         return allowedRoles;
 254  
   }
 255  
 
 256  
   public void setAllowedRoles(List<String> allowedRoles) {
 257  0
         this.allowedRoles = allowedRoles;
 258  0
   }
 259  
 
 260  
   public List<String> getDisallowedRoles() {
 261  0
         return disallowedRoles;
 262  
   }
 263  
 
 264  
   public void setDisallowedRoles(List<String> disallowedRoles) {
 265  0
         this.disallowedRoles = disallowedRoles;
 266  0
   }
 267  
 
 268  
   public List<KeyValue> getSearchableAttributes() {
 269  0
         return searchableAttributes;
 270  
   }
 271  
 
 272  
   public void setSearchableAttributes(List<KeyValue> searchableAttributes) {
 273  0
         this.searchableAttributes = searchableAttributes;
 274  0
   }
 275  
 
 276  
   public List<Group> getWorkgroups() {
 277  0
         return workgroups;
 278  
   }
 279  
 
 280  
   public void setWorkgroups(List<Group> workgroups) {
 281  0
         this.workgroups = workgroups;
 282  0
   }
 283  
 
 284  
   public Boolean getActive() {
 285  0
     return active;
 286  
   }
 287  
 
 288  
   public void setActive(Boolean active) {
 289  0
     this.active = active;
 290  0
   }
 291  
 
 292  
   public boolean isActive() {
 293  0
     if (active != null) {
 294  0
       return active.booleanValue();
 295  
     }
 296  
     else {
 297  0
       return false;
 298  
     }
 299  
   }
 300  
 }