KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > hippo > cms > workflow > TaxonomyWebsitePermissionsBasedAuthorizer


1 /*
2  * Copyright 2004 Hippo Webworks.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 nl.hippo.cms.workflow;
17
18 import com.opensymphony.module.propertyset.PropertySet;
19 import com.opensymphony.workflow.loader.ActionDescriptor;
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
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 JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36
37 public class TaxonomyWebsitePermissionsBasedAuthorizer implements WorkflowActionAuthorizer
38 {
39
40     public TaxonomyWebsitePermissionsBasedAuthorizer()
41     {
42         super();
43     }
44
45     public List JavaDoc authorizeActions(long workflowId, ActionDescriptor[] actions, WorkflowActionAuthorizerContext context) throws IOException JavaDoc
46     {
47         List JavaDoc result = new ArrayList JavaDoc(actions.length);
48         
49         Element JavaDoc 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 JavaDoc name = actionDescriptor.getName();
57                 String JavaDoc namespace = (String JavaDoc) actionDescriptor.getMetaAttributes().get(
58                         "namespaceUri");
59                 if (namespace != null)
60                 {
61                     NodeList JavaDoc 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 JavaDoc namespace, String JavaDoc name, WorkflowActionAuthorizerContext context) throws IOException JavaDoc
75     {
76         boolean result = false;
77         
78         Element JavaDoc currentUserPrivileges = getCurrentUserPrivileges(workflowId, context);
79         
80         if (currentUserPrivileges != null)
81         {
82             NodeList JavaDoc 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 JavaDoc getCurrentUserPrivileges(long workflowId, WorkflowActionAuthorizerContext context) throws IOException JavaDoc
94     {
95         Element JavaDoc result = null;
96         
97         Project project = context.getProject();
98         PersistenceManager persistenceManager = null;
99         try
100         {
101             String JavaDoc databaseName = project.getProperties().getProperty("taxonomy.databaseName");
102             String JavaDoc 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 JavaDoc queryParameters = new HashMap JavaDoc();
113             queryParameters.put("name", organisationName);
114             Object JavaDoc resultAsObject = q.executeWithMap(queryParameters);
115             if (resultAsObject instanceof Collection JavaDoc)
116             {
117                 Collection JavaDoc queryResult = (Collection JavaDoc) resultAsObject;
118                 Iterator JavaDoc resultIterator = queryResult.iterator();
119                 if (resultIterator.hasNext())
120                 {
121                     Object JavaDoc 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 JavaDoc websiteName = secondaryPlacementProperties.getString("websiteName");
129                         if (websiteName != null && !websiteName.equals(""))
130                         {
131                             Website website = organisation.getWebsite(websiteName);
132                             if (website != null)
133                             {
134                                 String JavaDoc websiteRepositoryPrefix = website.getRepositoryPrefix();
135                                 
136                                 String JavaDoc articleLocation = secondaryPlacementProperties.getString("articleLocation");
137                                 int indexOfEditorSuffix = articleLocation.indexOf(".editor/");
138                                 if (indexOfEditorSuffix >= 0)
139                                 {
140                                     String JavaDoc repositoryRoot = articleLocation.substring(0, indexOfEditorSuffix + ".editor/".length());
141                                     String JavaDoc 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