KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > permissions > impl > hibernate > HibernatePermissionsDAO


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.security.permissions.impl.hibernate;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.alfresco.repo.cache.SimpleCache;
25 import org.alfresco.repo.domain.NodeKey;
26 import org.alfresco.repo.security.permissions.NodePermissionEntry;
27 import org.alfresco.repo.security.permissions.PermissionEntry;
28 import org.alfresco.repo.security.permissions.PermissionReference;
29 import org.alfresco.repo.security.permissions.impl.PermissionsDAO;
30 import org.alfresco.repo.security.permissions.impl.SimpleNodePermissionEntry;
31 import org.alfresco.repo.security.permissions.impl.SimplePermissionEntry;
32 import org.alfresco.repo.security.permissions.impl.SimplePermissionReference;
33 import org.alfresco.service.cmr.repository.NodeRef;
34 import org.alfresco.service.cmr.security.AccessStatus;
35 import org.alfresco.service.namespace.QName;
36 import org.hibernate.ObjectDeletedException;
37 import org.hibernate.Query;
38 import org.hibernate.Session;
39 import org.springframework.dao.DataAccessException;
40 import org.springframework.orm.hibernate3.HibernateCallback;
41 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
42
43 /**
44  * Support for accessing persisted permission information.
45  *
46  * This class maps between persisted objects and the external API defined in the
47  * PermissionsDAO interface.
48  *
49  * @author andyh
50  */

51 public class HibernatePermissionsDAO extends HibernateDaoSupport implements PermissionsDAO
52 {
53     private SimpleCache<NodeRef, SimpleNodePermissionEntry> nullPermissionCache;
54
55     public HibernatePermissionsDAO()
56     {
57         super();
58
59     }
60
61     public void setNullPermissionCache(SimpleCache<NodeRef, SimpleNodePermissionEntry> nullPermissionCache)
62     {
63         this.nullPermissionCache = nullPermissionCache;
64     }
65
66     public NodePermissionEntry getPermissions(NodeRef nodeRef)
67     {
68         // Create the object if it is not found.
69
// Null objects are not cached in hibernate
70
// If the object does not exist it will repeatedly query to check its
71
// non existence.
72

73         NodePermissionEntry npe = nullPermissionCache.get(nodeRef);
74         if (npe != null)
75         {
76             return npe;
77         }
78
79         npe = createSimpleNodePermissionEntry(getHibernateNodePermissionEntry(nodeRef, false));
80         if (npe == null)
81         {
82             SimpleNodePermissionEntry snpe = new SimpleNodePermissionEntry(nodeRef, true, Collections
83                     .<SimplePermissionEntry> emptySet());
84             npe = snpe;
85             nullPermissionCache.put(nodeRef, snpe);
86         }
87         return npe;
88     }
89
90     /**
91      * Get the persisted NodePermissionEntry
92      *
93      * @param nodeRef
94      * @param create -
95      * create the object if it is missing
96      * @return
97      */

98     private org.alfresco.repo.security.permissions.impl.hibernate.NodePermissionEntry getHibernateNodePermissionEntry(
99             NodeRef nodeRef, boolean create)
100     {
101         // Build the key
102
NodeKey nodeKey = getNodeKey(nodeRef);
103         try
104         {
105             Object JavaDoc obj = getHibernateTemplate().get(NodePermissionEntryImpl.class, nodeKey);
106             // Create if required
107
if ((obj == null) && create)
108             {
109                 NodePermissionEntryImpl entry = new NodePermissionEntryImpl();
110                 entry.setNodeKey(nodeKey);
111                 entry.setInherits(true);
112                 getHibernateTemplate().save(entry);
113                 nullPermissionCache.remove(nodeRef);
114                 return entry;
115             }
116             return (org.alfresco.repo.security.permissions.impl.hibernate.NodePermissionEntry) obj;
117         }
118         catch (DataAccessException e)
119         {
120             if (e.contains(ObjectDeletedException.class))
121             {
122                 // the object no loner exists
123
if (create)
124                 {
125                     NodePermissionEntryImpl entry = new NodePermissionEntryImpl();
126                     entry.setNodeKey(nodeKey);
127                     entry.setInherits(true);
128                     getHibernateTemplate().save(entry);
129                     nullPermissionCache.remove(nodeRef);
130                     return entry;
131                 }
132                 else
133                 {
134                     return null;
135                 }
136             }
137             throw e;
138         }
139     }
140
141     /**
142      * Get a node key from a node reference
143      *
144      * @param nodeRef
145      * @return
146      */

147     private NodeKey getNodeKey(NodeRef nodeRef)
148     {
149         NodeKey nodeKey = new NodeKey(nodeRef.getStoreRef().getProtocol(), nodeRef.getStoreRef().getIdentifier(),
150                 nodeRef.getId());
151         return nodeKey;
152     }
153
154     public void deletePermissions(NodeRef nodeRef)
155     {
156         org.alfresco.repo.security.permissions.impl.hibernate.NodePermissionEntry found = getHibernateNodePermissionEntry(
157                 nodeRef, false);
158         if (found != null)
159         {
160             deleteHibernateNodePermissionEntry(found);
161         }
162     }
163
164     private void deleteHibernateNodePermissionEntry(
165             org.alfresco.repo.security.permissions.impl.hibernate.NodePermissionEntry hibernateNodePermissionEntry)
166     {
167         deleteHibernatePermissionEntries(hibernateNodePermissionEntry.getPermissionEntries());
168         getHibernateTemplate().delete(hibernateNodePermissionEntry);
169     }
170
171     private void deleteHibernatePermissionEntries(
172             Set JavaDoc<org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> permissionEntries)
173     {
174         // Avoid concurrent access problems during deletion
175
Set JavaDoc<org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> copy = new HashSet JavaDoc<org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry>();
176         for (org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry permissionEntry : copy)
177         {
178             deleteHibernatePermissionEntry(permissionEntry);
179         }
180     }
181
182     private void deleteHibernatePermissionEntry(
183             org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry permissionEntry)
184     {
185         // Unhook bidirectoinal relationships
186
permissionEntry.delete();
187         getHibernateTemplate().delete(permissionEntry);
188     }
189
190     public void deletePermissions(NodePermissionEntry nodePermissionEntry)
191     {
192         deletePermissions(nodePermissionEntry.getNodeRef());
193     }
194
195     public void deletePermissions(PermissionEntry permissionEntry)
196     {
197         org.alfresco.repo.security.permissions.impl.hibernate.NodePermissionEntry found = getHibernateNodePermissionEntry(
198                 permissionEntry.getNodeRef(), false);
199         if (found != null)
200         {
201             Set JavaDoc<org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> deletable = new HashSet JavaDoc<org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry>();
202
203             for (org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry current : found
204                     .getPermissionEntries())
205             {
206                 if (permissionEntry.equals(createSimplePermissionEntry(current)))
207                 {
208                     deletable.add(current);
209                 }
210             }
211
212             for (org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry current : deletable)
213             {
214                 deleteHibernatePermissionEntry(current);
215             }
216         }
217     }
218
219     public void clearPermission(NodeRef nodeRef, String JavaDoc authority)
220     {
221         org.alfresco.repo.security.permissions.impl.hibernate.NodePermissionEntry found = getHibernateNodePermissionEntry(
222                 nodeRef, false);
223         if (found != null)
224         {
225             Set JavaDoc<org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> deletable = new HashSet JavaDoc<org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry>();
226
227             for (org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry current : found
228                     .getPermissionEntries())
229             {
230                 if (createSimplePermissionEntry(current).getAuthority().equals(authority))
231                 {
232                     deletable.add(current);
233                 }
234             }
235
236             for (org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry current : deletable)
237             {
238                 deleteHibernatePermissionEntry(current);
239             }
240         }
241     }
242
243     public void deletePermissions(NodeRef nodeRef, String JavaDoc authority, PermissionReference perm, boolean allow)
244     {
245         SimplePermissionEntry spe = new SimplePermissionEntry(nodeRef, perm == null ? null
246                 : new SimplePermissionReference(perm.getQName(), perm.getName()), authority,
247                 allow ? AccessStatus.ALLOWED : AccessStatus.DENIED);
248         deletePermissions(spe);
249     }
250
251     public void setPermission(NodeRef nodeRef, String JavaDoc authority, PermissionReference perm, boolean allow)
252     {
253         deletePermissions(nodeRef, authority, perm, allow);
254         PermissionEntryImpl entry = PermissionEntryImpl.create(getHibernateNodePermissionEntry(nodeRef, true),
255                 getHibernatePermissionReference(perm, true), getHibernateAuthority(authority, true), allow);
256         getHibernateTemplate().save(entry);
257         nullPermissionCache.remove(nodeRef);
258     }
259
260     /**
261      * Utility method to find or create a persisted authority
262      *
263      * @param authority
264      * @param create
265      * @return
266      */

267     private Recipient getHibernateAuthority(String JavaDoc authority, boolean create)
268     {
269         Recipient key = new RecipientImpl();
270         key.setRecipient(authority);
271
272         Recipient found = (Recipient) getHibernateTemplate().get(RecipientImpl.class, key);
273         if ((found == null) && create)
274         {
275             getHibernateTemplate().save(key);
276             return key;
277         }
278         else
279         {
280             return found;
281         }
282
283     }
284
285     /**
286      * Utility method to find and optionally create a persisted permission
287      * reference.
288      *
289      * @param perm
290      * @param create
291      * @return
292      */

293     private org.alfresco.repo.security.permissions.impl.hibernate.PermissionReference getHibernatePermissionReference(
294             PermissionReference perm, boolean create)
295     {
296         org.alfresco.repo.security.permissions.impl.hibernate.PermissionReference key = new PermissionReferenceImpl();
297         key.setTypeUri(perm.getQName().getNamespaceURI());
298         key.setTypeName(perm.getQName().getLocalName());
299         key.setName(perm.getName());
300
301         org.alfresco.repo.security.permissions.impl.hibernate.PermissionReference found;
302
303         found = (org.alfresco.repo.security.permissions.impl.hibernate.PermissionReference) getHibernateTemplate().get(
304                 PermissionReferenceImpl.class, key);
305         if ((found == null) && create)
306         {
307             getHibernateTemplate().save(key);
308             return key;
309         }
310         else
311         {
312             return found;
313         }
314
315     }
316
317     public void setPermission(PermissionEntry permissionEntry)
318     {
319         setPermission(permissionEntry.getNodeRef(), permissionEntry.getAuthority(), permissionEntry
320                 .getPermissionReference(), permissionEntry.isAllowed());
321     }
322
323     public void setPermission(NodePermissionEntry nodePermissionEntry)
324     {
325         deletePermissions(nodePermissionEntry);
326         NodePermissionEntryImpl entry = new NodePermissionEntryImpl();
327         entry.setInherits(nodePermissionEntry.inheritPermissions());
328         entry.setNodeKey(getNodeKey(nodePermissionEntry.getNodeRef()));
329         getHibernateTemplate().save(entry);
330         nullPermissionCache.remove(nodePermissionEntry.getNodeRef());
331         for (PermissionEntry pe : nodePermissionEntry.getPermissionEntries())
332         {
333             setPermission(pe);
334         }
335     }
336
337     public void setInheritParentPermissions(NodeRef nodeRef, boolean inheritParentPermissions)
338     {
339         getHibernateNodePermissionEntry(nodeRef, true).setInherits(inheritParentPermissions);
340     }
341     
342     public boolean getInheritParentPermissions(NodeRef nodeRef)
343     {
344         return getHibernateNodePermissionEntry(nodeRef, true).getInherits();
345     }
346
347     @SuppressWarnings JavaDoc("unchecked")
348     public void deleteAllPermissionsForAuthority(final String JavaDoc authority)
349     {
350         HibernateCallback callback = new HibernateCallback()
351         {
352             public Object JavaDoc doInHibernate(Session session)
353             {
354                 Query query = session.getNamedQuery("permission.GetPermissionsForRecipient");
355                 query.setString("recipientKey", authority);
356                 return query.list();
357             }
358         };
359         List JavaDoc<org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> queryResults = (List JavaDoc) getHibernateTemplate().execute(callback);
360         for (org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry current : queryResults)
361         {
362             deleteHibernatePermissionEntry(current);
363         }
364         
365     }
366
367     // Utility methods to create simple detached objects for the outside world
368
// We do not pass out the hibernate objects
369

370     private static SimpleNodePermissionEntry createSimpleNodePermissionEntry(
371             org.alfresco.repo.security.permissions.impl.hibernate.NodePermissionEntry npe)
372     {
373         if (npe == null)
374         {
375             return null;
376         }
377         SimpleNodePermissionEntry snpe = new SimpleNodePermissionEntry(npe.getNodeRef(), npe.getInherits(),
378                 createSimplePermissionEntries(npe.getPermissionEntries()));
379         return snpe;
380     }
381
382     private static Set JavaDoc<SimplePermissionEntry> createSimplePermissionEntries(
383             Set JavaDoc<org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> nes)
384     {
385         if (nes == null)
386         {
387             return null;
388         }
389         HashSet JavaDoc<SimplePermissionEntry> spes = new HashSet JavaDoc<SimplePermissionEntry>(nes.size(), 1.0f);
390         if (nes.size() != 0)
391         {
392             for (org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry pe : nes)
393             {
394                 spes.add(createSimplePermissionEntry(pe));
395             }
396         }
397         return spes;
398     }
399
400     private static SimplePermissionEntry createSimplePermissionEntry(
401             org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry pe)
402     {
403         if (pe == null)
404         {
405             return null;
406         }
407         return new SimplePermissionEntry(pe.getNodePermissionEntry().getNodeRef(), createSimplePermissionReference(pe
408                 .getPermissionReference()), pe.getRecipient().getRecipient(), pe.isAllowed() ? AccessStatus.ALLOWED
409                 : AccessStatus.DENIED);
410     }
411
412     private static SimplePermissionReference createSimplePermissionReference(
413             org.alfresco.repo.security.permissions.impl.hibernate.PermissionReference pr)
414     {
415         if (pr == null)
416         {
417             return null;
418         }
419         return new SimplePermissionReference(QName.createQName(pr.getTypeUri(), pr.getTypeName()), pr.getName());
420     }
421
422 }
423
Popular Tags