📄 jdbcmutableaclservice.java
字号:
return classId; } /** * Retrieves the primary key from acl_sid, creating a new row if needed and the allowCreate property is * true. * * @param sid to find or create * @param allowCreate true if creation is permitted if not found * * @return the primary key or null if not found * * @throws IllegalArgumentException DOCUMENT ME! */ protected Long createOrRetrieveSidPrimaryKey(Sid sid, boolean allowCreate) { Assert.notNull(sid, "Sid required"); String sidName = null; boolean principal = true; if (sid instanceof PrincipalSid) { sidName = ((PrincipalSid) sid).getPrincipal(); } else if (sid instanceof GrantedAuthoritySid) { sidName = ((GrantedAuthoritySid) sid).getGrantedAuthority(); principal = false; } else { throw new IllegalArgumentException("Unsupported implementation of Sid"); } List sidIds = jdbcTemplate.queryForList(selectSidPrimaryKey, new Object[] {new Boolean(principal), sidName}, Long.class); Long sidId = null; if (sidIds.isEmpty()) { if (allowCreate) { sidId = null; jdbcTemplate.update(insertSid, new Object[] {new Boolean(principal), sidName}); Assert.isTrue(TransactionSynchronizationManager.isSynchronizationActive(), "Transaction must be running"); sidId = new Long(jdbcTemplate.queryForLong(identityQuery)); } } else { sidId = (Long) sidIds.iterator().next(); } return sidId; } public void deleteAcl(ObjectIdentity objectIdentity, boolean deleteChildren) throws ChildrenExistException { Assert.notNull(objectIdentity, "Object Identity required"); Assert.notNull(objectIdentity.getIdentifier(), "Object Identity doesn't provide an identifier"); // Recursively call this method for children, or handle children if they don't want automatic recursion ObjectIdentity[] children = findChildren(objectIdentity); if (deleteChildren) { for (int i = 0; i < children.length; i++) { deleteAcl(children[i], true); } } else if (children.length > 0) { throw new ChildrenExistException("Cannot delete '" + objectIdentity + "' (has " + children.length + " children)"); } // Delete this ACL's ACEs in the acl_entry table deleteEntries(objectIdentity); // Delete this ACL's acl_object_identity row deleteObjectIdentityAndOptionallyClass(objectIdentity); // Clear the cache aclCache.evictFromCache(objectIdentity); } /** * Deletes all ACEs defined in the acl_entry table belonging to the presented ObjectIdentity * * @param oid the rows in acl_entry to delete */ protected void deleteEntries(ObjectIdentity oid) { jdbcTemplate.update(deleteEntryByObjectIdentityForeignKey, new Object[] {retrieveObjectIdentityPrimaryKey(oid)}); } /** * Deletes a single row from acl_object_identity that is associated with the presented ObjectIdentity. In * addition, deletes the corresponding row from acl_class if there are no more entries in acl_object_identity that * use that particular acl_class. This keeps the acl_class table reasonably small. * * @param oid to delete the acl_object_identity (and clean up acl_class for that class name if appropriate) */ protected void deleteObjectIdentityAndOptionallyClass(ObjectIdentity oid) { // Delete the acl_object_identity row jdbcTemplate.update(deleteObjectIdentityByPrimaryKey, new Object[] {retrieveObjectIdentityPrimaryKey(oid)}); // Delete the acl_class row, assuming there are no other references to it in acl_object_identity Object[] className = {oid.getJavaType().getName()}; long numObjectIdentities = jdbcTemplate.queryForLong(selectCountObjectIdentityRowsForParticularClassNameString, className); if (numObjectIdentities == 0) { // No more rows jdbcTemplate.update(deleteClassByClassNameString, className); } } /** * Retrieves the primary key from the acl_object_identity table for the passed ObjectIdentity. Unlike some * other methods in this implementation, this method will NOT create a row (use {@link * #createObjectIdentity(ObjectIdentity, Sid)} instead). * * @param oid to find * * @return the object identity or null if not found */ protected Long retrieveObjectIdentityPrimaryKey(ObjectIdentity oid) { try { return new Long(jdbcTemplate.queryForLong(selectObjectIdentityPrimaryKey, new Object[] {oid.getJavaType().getName(), oid.getIdentifier()})); } catch (DataAccessException notFound) { return null; } } /** * This implementation will simply delete all ACEs in the database and recreate them on each invocation of * this method. A more comprehensive implementation might use dirty state checking, or more likely use ORM * capabilities for create, update and delete operations of {@link MutableAcl}. * * @param acl DOCUMENT ME! * * @return DOCUMENT ME! * * @throws NotFoundException DOCUMENT ME! */ public MutableAcl updateAcl(MutableAcl acl) throws NotFoundException { Assert.notNull(acl.getId(), "Object Identity doesn't provide an identifier"); // Delete this ACL's ACEs in the acl_entry table deleteEntries(acl.getObjectIdentity()); // Create this ACL's ACEs in the acl_entry table createEntries(acl); // Change the mutable columns in acl_object_identity updateObjectIdentity(acl); // Clear the cache aclCache.evictFromCache(acl.getObjectIdentity()); // Retrieve the ACL via superclass (ensures cache registration, proper retrieval etc) return (MutableAcl) super.readAclById(acl.getObjectIdentity()); } /** * Updates an existing acl_object_identity row, with new information presented in the passed MutableAcl * object. Also will create an acl_sid entry if needed for the Sid that owns the MutableAcl. * * @param acl to modify (a row must already exist in acl_object_identity) * * @throws NotFoundException DOCUMENT ME! */ protected void updateObjectIdentity(MutableAcl acl) { Long parentId = null; if (acl.getParentAcl() != null) { Assert.isInstanceOf(ObjectIdentityImpl.class, acl.getParentAcl().getObjectIdentity(), "Implementation only supports ObjectIdentityImpl"); ObjectIdentityImpl oii = (ObjectIdentityImpl) acl.getParentAcl().getObjectIdentity(); parentId = retrieveObjectIdentityPrimaryKey(oii); } Assert.notNull(acl.getOwner(), "Owner is required in this implementation"); Long ownerSid = createOrRetrieveSidPrimaryKey(acl.getOwner(), true); int count = jdbcTemplate.update(updateObjectIdentity, new Object[] {parentId, ownerSid, new Boolean(acl.isEntriesInheriting()), acl.getId()}); if (count != 1) { throw new NotFoundException("Unable to locate ACL to update"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -