1 17 package org.alfresco.repo.security.permissions.impl.hibernate; 18 19 import java.util.Collections ; 20 import java.util.HashSet ; 21 import java.util.List ; 22 import java.util.Set ; 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 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 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 98 private org.alfresco.repo.security.permissions.impl.hibernate.NodePermissionEntry getHibernateNodePermissionEntry( 99 NodeRef nodeRef, boolean create) 100 { 101 NodeKey nodeKey = getNodeKey(nodeRef); 103 try 104 { 105 Object obj = getHibernateTemplate().get(NodePermissionEntryImpl.class, nodeKey); 106 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 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 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 <org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> permissionEntries) 173 { 174 Set <org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> copy = new HashSet <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 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 <org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> deletable = new HashSet <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 authority) 220 { 221 org.alfresco.repo.security.permissions.impl.hibernate.NodePermissionEntry found = getHibernateNodePermissionEntry( 222 nodeRef, false); 223 if (found != null) 224 { 225 Set <org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> deletable = new HashSet <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 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 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 267 private Recipient getHibernateAuthority(String 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 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 ("unchecked") 348 public void deleteAllPermissionsForAuthority(final String authority) 349 { 350 HibernateCallback callback = new HibernateCallback() 351 { 352 public Object doInHibernate(Session session) 353 { 354 Query query = session.getNamedQuery("permission.GetPermissionsForRecipient"); 355 query.setString("recipientKey", authority); 356 return query.list(); 357 } 358 }; 359 List <org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> queryResults = (List ) getHibernateTemplate().execute(callback); 360 for (org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry current : queryResults) 361 { 362 deleteHibernatePermissionEntry(current); 363 } 364 365 } 366 367 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 <SimplePermissionEntry> createSimplePermissionEntries( 383 Set <org.alfresco.repo.security.permissions.impl.hibernate.PermissionEntry> nes) 384 { 385 if (nes == null) 386 { 387 return null; 388 } 389 HashSet <SimplePermissionEntry> spes = new HashSet <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 |