use of org.apache.wiki.api.core.Acl in project jspwiki by apache.
the class DefaultAuthorizationManager method checkPermission.
/**
* {@inheritDoc}
*/
@Override
public boolean checkPermission(final Session session, final Permission permission) {
// A slight sanity check.
if (session == null || permission == null) {
fireEvent(WikiSecurityEvent.ACCESS_DENIED, null, permission);
return false;
}
final Principal user = session.getLoginPrincipal();
// Always allow the action if user has AllPermission
final Permission allPermission = new AllPermission(m_engine.getApplicationName());
final boolean hasAllPermission = checkStaticPermission(session, allPermission);
if (hasAllPermission) {
fireEvent(WikiSecurityEvent.ACCESS_ALLOWED, user, permission);
return true;
}
// If the user doesn't have *at least* the permission granted by policy, return false.
final boolean hasPolicyPermission = checkStaticPermission(session, permission);
if (!hasPolicyPermission) {
fireEvent(WikiSecurityEvent.ACCESS_DENIED, user, permission);
return false;
}
// If this isn't a PagePermission, it's allowed
if (!(permission instanceof PagePermission)) {
fireEvent(WikiSecurityEvent.ACCESS_ALLOWED, user, permission);
return true;
}
// If the page or ACL is null, it's allowed.
final String pageName = ((PagePermission) permission).getPage();
final Page page = m_engine.getManager(PageManager.class).getPage(pageName);
final Acl acl = (page == null) ? null : m_engine.getManager(AclManager.class).getPermissions(page);
if (page == null || acl == null || acl.isEmpty()) {
fireEvent(WikiSecurityEvent.ACCESS_ALLOWED, user, permission);
return true;
}
// Next, iterate through the Principal objects assigned this permission. If the context's subject possesses
// any of these, the action is allowed.
final Principal[] aclPrincipals = acl.findPrincipals(permission);
log.debug("Checking ACL entries...");
log.debug("Acl for this page is: " + acl);
log.debug("Checking for principal: " + Arrays.toString(aclPrincipals));
log.debug("Permission: " + permission);
for (Principal aclPrincipal : aclPrincipals) {
// If the ACL principal we're looking at is unresolved, try to resolve it here & correct the Acl
if (aclPrincipal instanceof UnresolvedPrincipal) {
final AclEntry aclEntry = acl.getAclEntry(aclPrincipal);
aclPrincipal = resolvePrincipal(aclPrincipal.getName());
if (aclEntry != null && !(aclPrincipal instanceof UnresolvedPrincipal)) {
aclEntry.setPrincipal(aclPrincipal);
}
}
if (hasRoleOrPrincipal(session, aclPrincipal)) {
fireEvent(WikiSecurityEvent.ACCESS_ALLOWED, user, permission);
return true;
}
}
fireEvent(WikiSecurityEvent.ACCESS_DENIED, user, permission);
return false;
}
use of org.apache.wiki.api.core.Acl in project jspwiki by apache.
the class DefaultAclManagerTest method testGetPermissions.
@Test
public void testGetPermissions() {
Page page = m_engine.getManager(PageManager.class).getPage("TestDefaultPage");
Acl acl = m_engine.getManager(AclManager.class).getPermissions(page);
Assertions.assertNotNull(page.getAcl());
Assertions.assertTrue(page.getAcl().isEmpty());
page = m_engine.getManager(PageManager.class).getPage("TestAclPage");
acl = m_engine.getManager(AclManager.class).getPermissions(page);
Assertions.assertNotNull(page.getAcl());
Assertions.assertFalse(page.getAcl().isEmpty());
Principal[] p;
// Charlie is an editor; reading is therefore implied
p = acl.findPrincipals(PermissionFactory.getPagePermission(page, "view"));
Assertions.assertEquals(2, p.length);
Assertions.assertTrue(ArrayUtils.contains(p, new WikiPrincipal("Charlie")));
// Charlie should be in the ACL as an editor
p = acl.findPrincipals(PermissionFactory.getPagePermission(page, "edit"));
Assertions.assertEquals(2, p.length);
Assertions.assertTrue(ArrayUtils.contains(p, new WikiPrincipal("Charlie")));
// Charlie should not be able to delete this page
p = acl.findPrincipals(PermissionFactory.getPagePermission(page, "delete"));
Assertions.assertEquals(0, p.length);
// Herman is an unregistered user and editor; reading is implied
p = acl.findPrincipals(PermissionFactory.getPagePermission(page, "view"));
Assertions.assertEquals(2, p.length);
Assertions.assertTrue(ArrayUtils.contains(p, new UnresolvedPrincipal("Herman")));
// Herman should be in the ACL as an editor
p = acl.findPrincipals(PermissionFactory.getPagePermission(page, "edit"));
Assertions.assertEquals(2, p.length);
Assertions.assertTrue(ArrayUtils.contains(p, new UnresolvedPrincipal("Herman")));
// Herman should not be able to delete this page
p = acl.findPrincipals(PermissionFactory.getPagePermission(page, "delete"));
Assertions.assertEquals(0, p.length);
}
use of org.apache.wiki.api.core.Acl in project jspwiki by apache.
the class JSPWikiMarkupParser method handleAccessRule.
private Element handleAccessRule(String ruleLine) {
if (m_wysiwygEditorMode) {
m_currentElement.addContent("[" + ruleLine + "]");
}
if (!m_parseAccessRules) {
return m_currentElement;
}
final Page page = m_context.getRealPage();
if (ruleLine.startsWith("{")) {
ruleLine = ruleLine.substring(1);
}
if (ruleLine.endsWith("}")) {
ruleLine = ruleLine.substring(0, ruleLine.length() - 1);
}
log.debug("page={}, ACL = {}", page.getName(), ruleLine);
try {
final Acl acl = m_engine.getManager(AclManager.class).parseAcl(page, ruleLine);
page.setAcl(acl);
log.debug(acl.toString());
} catch (final WikiSecurityException wse) {
return makeError(wse.getMessage());
}
return m_currentElement;
}
Aggregations