View Javadoc

1   package net.sourceforge.blogentis.slide;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: SecurityAdapterStore.java,v 1.2 2004/10/31 07:16:23 tassos Exp $
23  //
24  
25  import java.util.Enumeration;
26  import java.util.Hashtable;
27  import java.util.Iterator;
28  import java.util.Vector;
29  
30  import javax.transaction.xa.XAException;
31  import javax.transaction.xa.XAResource;
32  import javax.transaction.xa.Xid;
33  
34  import net.sourceforge.blogentis.utils.BlogConstants;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.apache.slide.common.AbstractXAService;
39  import org.apache.slide.common.ServiceAccessException;
40  import org.apache.slide.common.ServiceConnectionFailedException;
41  import org.apache.slide.common.ServiceDisconnectionFailedException;
42  import org.apache.slide.common.ServiceParameterErrorException;
43  import org.apache.slide.common.ServiceParameterMissingException;
44  import org.apache.slide.common.ServiceResetFailedException;
45  import org.apache.slide.common.Uri;
46  import org.apache.slide.common.UriPath;
47  import org.apache.slide.security.NodePermission;
48  import org.apache.slide.store.SecurityStore;
49  import org.apache.torque.util.Criteria;
50  import org.apache.turbine.om.security.User;
51  import org.apache.turbine.services.security.TurbineSecurity;
52  
53  /***
54   * @author abas
55   */
56  public class SecurityAdapterStore
57          extends AbstractXAService
58          implements SecurityStore {
59      private static final Log log = LogFactory.getLog(SecurityAdapterStore.class);
60  
61      private static final String BLOG_PARAMETER_NAME = "blogs";
62      private static final String USERS_PARAMETER_NAME = "users";
63  
64      private static final class EmptyEnumeration
65              implements Enumeration {
66          public boolean hasMoreElements() {
67              return false;
68          }
69  
70          public Object nextElement() {
71              return null;
72          }
73      }
74  
75      private String blogPrefix = null;
76      private String userPrefix = null;
77  
78      public void setParameters(Hashtable parameters)
79              throws ServiceParameterErrorException,
80              ServiceParameterMissingException {
81          blogPrefix = (String)parameters.get(BLOG_PARAMETER_NAME);
82          if (blogPrefix == null)
83              blogPrefix = "/blog";
84          blogPrefix = blogPrefix + "/";
85          userPrefix = (String)parameters.get(USERS_PARAMETER_NAME);
86          if (userPrefix == null)
87              userPrefix = "/users";
88          userPrefix = userPrefix + "/";
89      }
90  
91      public void connect()
92              throws ServiceConnectionFailedException {}
93  
94      public void disconnect()
95              throws ServiceDisconnectionFailedException {}
96  
97      public void reset()
98              throws ServiceResetFailedException {}
99  
100     public boolean isConnected()
101             throws ServiceAccessException {
102         return true;
103     }
104 
105     public int getTransactionTimeout()
106             throws XAException {
107         return 0;
108     }
109 
110     public boolean setTransactionTimeout(int arg0)
111             throws XAException {
112         return false;
113     }
114 
115     public boolean isSameRM(XAResource arg0)
116             throws XAException {
117         return false;
118     }
119 
120     public Xid[] recover(int arg0)
121             throws XAException {
122         return null;
123     }
124 
125     public int prepare(Xid arg0)
126             throws XAException {
127         return 0;
128     }
129 
130     public void forget(Xid arg0)
131             throws XAException {}
132 
133     public void rollback(Xid arg0)
134             throws XAException {}
135 
136     public void end(Xid arg0, int arg1)
137             throws XAException {}
138 
139     public void start(Xid arg0, int arg1)
140             throws XAException {}
141 
142     public void commit(Xid arg0, boolean arg1)
143             throws XAException {}
144 
145     public void grantPermission(Uri uri, NodePermission permission)
146             throws ServiceAccessException {}
147 
148     public void revokePermission(Uri uri, NodePermission permission)
149             throws ServiceAccessException {}
150 
151     public void revokePermissions(Uri uri)
152             throws ServiceAccessException {}
153 
154     public Enumeration enumeratePermissions(Uri uri)
155             throws ServiceAccessException {
156         if (log.isDebugEnabled())
157             log.debug("Enumerating permissions for " + uri.toString());
158         Vector v = new Vector();
159         if (uri.isStoreRoot()) {
160             v.add(new NodePermission(uri.toString(), "all", "/actions/read",
161                 true));
162             return v.elements();
163         }
164 
165         int count = 0;
166         for(Enumeration e = uri.getScopes(); e.hasMoreElements(); e.nextElement())
167             count++;
168 
169         if (count != 3)
170             return new EmptyEnumeration();
171         if (!uri.toString().startsWith(blogPrefix))
172             return new EmptyEnumeration();
173 
174         String blogName = new UriPath(uri.toString()).lastSegment();
175 
176         try {
177             Iterator i = TurbineSecurity.getUserList(new Criteria()).iterator();
178             while (i.hasNext()) {
179                 User u = (User)i.next();
180                 if (TurbineSecurity.getACL(u)
181                     .hasPermission(BlogConstants.PERM_ADMIN_BLOG,
182                                                             blogName))
183                     v.add(new NodePermission(uri.toString(), userPrefix
184                             + u.getName(), "all", true));
185             }
186         } catch (Exception e) {
187         }
188         return v.elements();
189     }
190 }