use of javax.jcr.security.AccessControlPolicy in project jackrabbit by apache.
the class AccessControlUtils method getAccessControlList.
/**
* Utility that combines {@link AccessControlManager#getApplicablePolicies(String)}
* and {@link AccessControlManager#getPolicies(String)} to retrieve
* a modifiable {@code JackrabbitAccessControlList} for the given path.<br>
*
* Note that the policy must be {@link AccessControlManager#setPolicy(String,
* javax.jcr.security.AccessControlPolicy) reapplied}
* and the changes must be saved in order to make the AC modifications take
* effect.
*
* @param accessControlManager The {@code AccessControlManager} .
* @param absPath The absolute path of the target node.
* @return A modifiable access control list or null if there is none.
* @throws RepositoryException If an error occurs.
*/
public static JackrabbitAccessControlList getAccessControlList(AccessControlManager accessControlManager, String absPath) throws RepositoryException {
// try applicable (new) ACLs
AccessControlPolicyIterator itr = accessControlManager.getApplicablePolicies(absPath);
while (itr.hasNext()) {
AccessControlPolicy policy = itr.nextAccessControlPolicy();
if (policy instanceof JackrabbitAccessControlList) {
return (JackrabbitAccessControlList) policy;
}
}
// try if there is an acl that has been set before
AccessControlPolicy[] pcls = accessControlManager.getPolicies(absPath);
for (AccessControlPolicy policy : pcls) {
if (policy instanceof JackrabbitAccessControlList) {
return (JackrabbitAccessControlList) policy;
}
}
// no policy found
return null;
}
use of javax.jcr.security.AccessControlPolicy in project jackrabbit by apache.
the class AccessControlPolicyTest method testGetPolicyAfterSave.
public void testGetPolicyAfterSave() throws RepositoryException, AccessDeniedException, NotExecutableException {
checkCanReadAc(path);
checkCanModifyAc(path);
AccessControlPolicy policy;
AccessControlPolicyIterator it = acMgr.getApplicablePolicies(path);
if (it.hasNext()) {
policy = it.nextAccessControlPolicy();
acMgr.setPolicy(path, policy);
superuser.save();
// remember for tearDown
addedPolicies.put(path, policy);
} else {
throw new NotExecutableException();
}
Session s2 = null;
try {
s2 = getHelper().getSuperuserSession();
List<AccessControlPolicy> plcs = Arrays.asList(getAccessControlManager(s2).getPolicies(path));
// TODO: check again if policies can be compared with equals!
assertTrue("Policy must be visible to another superuser session.", plcs.contains(policy));
} finally {
if (s2 != null) {
s2.logout();
}
}
}
use of javax.jcr.security.AccessControlPolicy in project jackrabbit by apache.
the class AccessControlPolicyTest method testApplicablePoliciesAreDistinct.
public void testApplicablePoliciesAreDistinct() throws RepositoryException, AccessDeniedException, NotExecutableException {
checkCanReadAc(path);
// call must succeed without exception
AccessControlPolicyIterator it = acMgr.getApplicablePolicies(path);
Set<AccessControlPolicy> acps = new HashSet<AccessControlPolicy>();
while (it.hasNext()) {
AccessControlPolicy policy = it.nextAccessControlPolicy();
if (!acps.add(policy)) {
fail("The applicable policies present should be unique among the choices. Policy " + policy + " occured multiple times.");
}
}
}
use of javax.jcr.security.AccessControlPolicy in project jackrabbit by apache.
the class AccessControlPolicyTest method testSetPolicy.
public void testSetPolicy() throws RepositoryException, AccessDeniedException, NotExecutableException {
checkCanModifyAc(path);
AccessControlPolicyIterator it = acMgr.getApplicablePolicies(path);
if (it.hasNext()) {
AccessControlPolicy policy = it.nextAccessControlPolicy();
acMgr.setPolicy(path, policy);
} else {
throw new NotExecutableException();
}
}
use of javax.jcr.security.AccessControlPolicy in project jackrabbit by apache.
the class AccessControlPolicyTest method testRemovePolicyIsTransient.
public void testRemovePolicyIsTransient() throws RepositoryException, AccessDeniedException, NotExecutableException {
checkCanReadAc(path);
checkCanModifyAc(path);
AccessControlPolicy[] currentPolicies = acMgr.getPolicies(path);
int size = currentPolicies.length;
AccessControlPolicy toRemove;
if (size == 0) {
// no policy to remove ->> apply one
AccessControlPolicyIterator it = acMgr.getApplicablePolicies(path);
if (it.hasNext()) {
AccessControlPolicy policy = it.nextAccessControlPolicy();
acMgr.setPolicy(path, policy);
superuser.save();
// remember for teardown
addedPolicies.put(path, policy);
toRemove = policy;
currentPolicies = acMgr.getPolicies(path);
size = currentPolicies.length;
} else {
throw new NotExecutableException();
}
} else {
toRemove = currentPolicies[0];
}
// test transient behaviour of the removal
acMgr.removePolicy(path, toRemove);
assertEquals("After transient remove AccessControlManager.getPolicies must return less policies.", size - 1, acMgr.getPolicies(path).length);
// revert changes
superuser.refresh(false);
assertEquals("Reverting a Policy removal must restore the original state.", Arrays.asList(currentPolicies), Arrays.asList(acMgr.getPolicies(path)));
}
Aggregations