| 1 16 package nl.hippo.cms.workflow; 17 18 import com.opensymphony.module.propertyset.PropertySet; 19 import com.opensymphony.workflow.loader.ActionDescriptor; 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 import javax.jdo.PersistenceManager; 28 import javax.jdo.Query; 29 import javax.jdo.Transaction; 30 import nl.hippo.cocoon.webdav.WebDAVHelper; 31 import nl.hippo.servermanager.Project; 32 import nl.hippo.sites.taxonomy.Organisation; 33 import nl.hippo.sites.taxonomy.Website; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.NodeList ; 36 37 public class TaxonomyWebsitePermissionsBasedAuthorizer implements WorkflowActionAuthorizer 38 { 39 40 public TaxonomyWebsitePermissionsBasedAuthorizer() 41 { 42 super(); 43 } 44 45 public List authorizeActions(long workflowId, ActionDescriptor[] actions, WorkflowActionAuthorizerContext context) throws IOException  46 { 47 List result = new ArrayList (actions.length); 48 49 Element currentUserPrivileges = getCurrentUserPrivileges(workflowId, context); 50 51 if (currentUserPrivileges != null) 52 { 53 for (int i = 0; i < actions.length; i++) 54 { 55 ActionDescriptor actionDescriptor = actions[i]; 56 String name = actionDescriptor.getName(); 57 String namespace = (String ) actionDescriptor.getMetaAttributes().get( 58 "namespaceUri"); 59 if (namespace != null) 60 { 61 NodeList privilegeElements = currentUserPrivileges.getElementsByTagNameNS( 62 namespace, name); 63 if (privilegeElements != null && privilegeElements.getLength() > 0) 64 { 65 result.add(actionDescriptor); 66 } 67 } 68 } 69 } 70 71 return result; 72 } 73 74 public boolean authorizeAction(long workflowId, String namespace, String name, WorkflowActionAuthorizerContext context) throws IOException  75 { 76 boolean result = false; 77 78 Element currentUserPrivileges = getCurrentUserPrivileges(workflowId, context); 79 80 if (currentUserPrivileges != null) 81 { 82 NodeList privilegeElements = currentUserPrivileges.getElementsByTagNameNS( 83 namespace, name); 84 if (privilegeElements != null && privilegeElements.getLength() > 0) 85 { 86 result = true; 87 } 88 } 89 90 return result; 91 } 92 93 private Element getCurrentUserPrivileges(long workflowId, WorkflowActionAuthorizerContext context) throws IOException  94 { 95 Element result = null; 96 97 Project project = context.getProject(); 98 PersistenceManager persistenceManager = null; 99 try 100 { 101 String databaseName = project.getProperties().getProperty("taxonomy.databaseName"); 102 String organisationName = project.getProperties().getProperty( 103 "taxonomy.organisationName"); 104 105 persistenceManager = project.getDatabaseConnectionFactory().getPersistenceManager( 106 databaseName); 107 Transaction t = persistenceManager.currentTransaction(); 108 t.begin(); 109 110 Query q = persistenceManager.newQuery(Organisation.class, "m_name == name"); 111 q.declareParameters("String name"); 112 Map queryParameters = new HashMap (); 113 queryParameters.put("name", organisationName); 114 Object resultAsObject = q.executeWithMap(queryParameters); 115 if (resultAsObject instanceof Collection ) 116 { 117 Collection queryResult = (Collection ) resultAsObject; 118 Iterator resultIterator = queryResult.iterator(); 119 if (resultIterator.hasNext()) 120 { 121 Object organisationAsObject = resultIterator.next(); 122 if (organisationAsObject instanceof Organisation) 123 { 124 Organisation organisation = (Organisation) organisationAsObject; 125 126 PropertySet secondaryPlacementProperties = context.getWorkflow().getPropertySet(workflowId); 127 128 String websiteName = secondaryPlacementProperties.getString("websiteName"); 129 if (websiteName != null && !websiteName.equals("")) 130 { 131 Website website = organisation.getWebsite(websiteName); 132 if (website != null) 133 { 134 String websiteRepositoryPrefix = website.getRepositoryPrefix(); 135 136 String articleLocation = secondaryPlacementProperties.getString("articleLocation"); 137 int indexOfEditorSuffix = articleLocation.indexOf(".editor/"); 138 if (indexOfEditorSuffix >= 0) 139 { 140 String repositoryRoot = articleLocation.substring(0, indexOfEditorSuffix + ".editor/".length()); 141 String websiteUri = repositoryRoot + "content/" + websiteRepositoryPrefix; 142 if (websiteUri.startsWith("webdav://")) 143 { 144 websiteUri = "http://" + websiteUri.substring("webdav://".length()); 145 } 146 result = WebDAVHelper.propfindAsElement(websiteUri, "DAV:", "current-user-privilege-set", context.getHttpState()); 147 148 } 149 } 150 } 151 } 152 } 153 } 154 } 155 finally 156 { 157 if (persistenceManager != null) 158 { 159 project.getDatabaseConnectionFactory().releasePersistenceManager(persistenceManager); 160 } 161 } 162 return result; 163 } 164 165 } 166 | Popular Tags |