Search in sources :

Example 1 with PrincipalManager

use of org.apache.jackrabbit.api.security.principal.PrincipalManager in project jackrabbit by apache.

the class WriteTest method testInvalidPrincipal.

public void testInvalidPrincipal() throws Exception {
    PrincipalManager pMgr = ((JackrabbitSession) superuser).getPrincipalManager();
    String unknown = "unknown";
    while (pMgr.hasPrincipal(unknown)) {
        unknown = unknown + "_";
    }
    Principal principal = new PrincipalImpl(unknown);
    if (acMgr instanceof JackrabbitAccessControlManager) {
        // first try applicable policies
        try {
            AccessControlPolicy[] policies = ((JackrabbitAccessControlManager) acMgr).getApplicablePolicies(principal);
            assertNotNull(policies);
            assertEquals(0, policies.length);
        } catch (AccessControlException e) {
        // success
        }
        // second existing policies
        try {
            AccessControlPolicy[] policies = ((JackrabbitAccessControlManager) acMgr).getPolicies(principal);
            assertNotNull(policies);
            assertEquals(0, policies.length);
        } catch (AccessControlException e) {
        // success
        }
    } else {
        throw new NotExecutableException();
    }
}
Also used : PrincipalManager(org.apache.jackrabbit.api.security.principal.PrincipalManager) JackrabbitAccessControlManager(org.apache.jackrabbit.api.security.JackrabbitAccessControlManager) JackrabbitAccessControlPolicy(org.apache.jackrabbit.api.security.JackrabbitAccessControlPolicy) AccessControlPolicy(javax.jcr.security.AccessControlPolicy) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) AccessControlException(javax.jcr.security.AccessControlException) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) ItemBasedPrincipal(org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal) TestPrincipal(org.apache.jackrabbit.core.security.TestPrincipal) Principal(java.security.Principal) PrincipalImpl(org.apache.jackrabbit.core.security.principal.PrincipalImpl)

Example 2 with PrincipalManager

use of org.apache.jackrabbit.api.security.principal.PrincipalManager in project jackrabbit-oak by apache.

the class GetPrincipalTest method runTest.

@Override
protected void runTest() throws Exception {
    Session s = loginWriter();
    PrincipalManager principalManager = ((JackrabbitSession) s).getPrincipalManager();
    for (int i = 0; i < 1000; i++) {
        Principal p = principalManager.getPrincipal(getUserId());
    }
}
Also used : PrincipalManager(org.apache.jackrabbit.api.security.principal.PrincipalManager) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Principal(java.security.Principal) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 3 with PrincipalManager

use of org.apache.jackrabbit.api.security.principal.PrincipalManager in project jackrabbit by apache.

the class NestedGroupTest method testInheritedMembership.

public void testInheritedMembership() throws NotExecutableException, RepositoryException {
    Group gr1 = null;
    Group gr2 = null;
    Group gr3 = null;
    if (!(superuser instanceof JackrabbitSession)) {
        throw new NotExecutableException();
    }
    try {
        gr1 = createGroup(getTestPrincipal());
        gr2 = createGroup(getTestPrincipal());
        gr3 = createGroup(getTestPrincipal());
        assertTrue(addMember(gr1, gr2));
        assertTrue(addMember(gr2, gr3));
        // NOTE: don't test with Group.isMember for not required to detect
        // inherited membership -> rather with PrincipalManager.
        boolean isMember = false;
        PrincipalManager pmgr = ((JackrabbitSession) superuser).getPrincipalManager();
        for (PrincipalIterator it = pmgr.getGroupMembership(gr3.getPrincipal()); it.hasNext() && !isMember; ) {
            isMember = it.nextPrincipal().equals(gr1.getPrincipal());
        }
        assertTrue(isMember);
    } finally {
        if (gr1 != null && gr1.isMember(gr2)) {
            removeMember(gr1, gr2);
        }
        if (gr2 != null && gr2.isMember(gr3)) {
            removeMember(gr2, gr3);
        }
        if (gr1 != null)
            removeGroup(gr1);
        if (gr2 != null)
            removeGroup(gr2);
        if (gr3 != null)
            removeGroup(gr3);
    }
}
Also used : PrincipalManager(org.apache.jackrabbit.api.security.principal.PrincipalManager) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) PrincipalIterator(org.apache.jackrabbit.api.security.principal.PrincipalIterator) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 4 with PrincipalManager

use of org.apache.jackrabbit.api.security.principal.PrincipalManager in project jackrabbit by apache.

the class AccessControlImporterTest method testImportPrincipalBasedACL.

/**
     * Imports a principal-based ACL containing a single entry mist fail with
     * the default configuration.
     *
     * @throws Exception
     */
public void testImportPrincipalBasedACL() throws Exception {
    JackrabbitAccessControlManager acMgr = (JackrabbitAccessControlManager) sImpl.getAccessControlManager();
    if (acMgr.getApplicablePolicies(EveryonePrincipal.getInstance()).length > 0 || acMgr.getPolicies(EveryonePrincipal.getInstance()).length > 0) {
        // test expects that only resource-based acl is supported
        throw new NotExecutableException();
    }
    PrincipalManager pmgr = sImpl.getPrincipalManager();
    if (!pmgr.hasPrincipal(SecurityConstants.ADMINISTRATORS_NAME)) {
        UserManager umgr = sImpl.getUserManager();
        umgr.createGroup(new PrincipalImpl(SecurityConstants.ADMINISTRATORS_NAME));
        if (!umgr.isAutoSave()) {
            sImpl.save();
        }
        if (pmgr.hasPrincipal(SecurityConstants.ADMINISTRATORS_NAME)) {
            throw new NotExecutableException();
        }
    }
    NodeImpl target;
    NodeImpl root = (NodeImpl) sImpl.getRootNode();
    if (!root.hasNode(AccessControlConstants.N_ACCESSCONTROL)) {
        target = root.addNode(AccessControlConstants.N_ACCESSCONTROL, AccessControlConstants.NT_REP_ACCESS_CONTROL, null);
    } else {
        target = root.getNode(AccessControlConstants.N_ACCESSCONTROL);
        if (!target.isNodeType(AccessControlConstants.NT_REP_ACCESS_CONTROL)) {
            target.setPrimaryType(sImpl.getJCRName(AccessControlConstants.NT_REP_ACCESS_CONTROL));
        }
    }
    try {
        InputStream in = new ByteArrayInputStream(XML_AC_TREE.getBytes("UTF-8"));
        SessionImporter importer = new SessionImporter(target, sImpl, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW, new PseudoConfig());
        ImportHandler ih = new ImportHandler(importer, sImpl);
        new ParsingContentHandler(ih).parse(in);
        fail("Default config only allows resource-based ACL -> protected import must fail");
    } catch (SAXException e) {
        if (e.getException() instanceof ConstraintViolationException) {
        // success
        } else {
            throw e;
        }
    } finally {
        superuser.refresh(false);
    }
}
Also used : JackrabbitAccessControlManager(org.apache.jackrabbit.api.security.JackrabbitAccessControlManager) PrincipalManager(org.apache.jackrabbit.api.security.principal.PrincipalManager) NodeImpl(org.apache.jackrabbit.core.NodeImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) SAXException(org.xml.sax.SAXException) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) ByteArrayInputStream(java.io.ByteArrayInputStream) UserManager(org.apache.jackrabbit.api.security.user.UserManager) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PrincipalImpl(org.apache.jackrabbit.core.security.principal.PrincipalImpl)

Example 5 with PrincipalManager

use of org.apache.jackrabbit.api.security.principal.PrincipalManager in project jackrabbit by apache.

the class ACLTemplateTest method testMultiplePrincipals.

public void testMultiplePrincipals() throws RepositoryException, NotExecutableException {
    PrincipalManager pMgr = ((JackrabbitSession) superuser).getPrincipalManager();
    Principal everyone = pMgr.getEveryone();
    Principal grPrincipal = null;
    PrincipalIterator it = pMgr.findPrincipals("", PrincipalManager.SEARCH_TYPE_GROUP);
    while (it.hasNext()) {
        Group gr = (Group) it.nextPrincipal();
        if (!everyone.equals(gr)) {
            grPrincipal = gr;
        }
    }
    if (grPrincipal == null || grPrincipal.equals(everyone)) {
        throw new NotExecutableException();
    }
    Privilege[] privs = privilegesFromName(Privilege.JCR_READ);
    JackrabbitAccessControlList pt = createEmptyTemplate(getTestPath());
    pt.addAccessControlEntry(testPrincipal, privs);
    assertFalse(pt.addAccessControlEntry(testPrincipal, privs));
    // add same privileges for another principal -> must modify as well.
    assertTrue(pt.addAccessControlEntry(everyone, privs));
    // .. 2 entries must be present.
    assertTrue(pt.getAccessControlEntries().length == 2);
}
Also used : PrincipalManager(org.apache.jackrabbit.api.security.principal.PrincipalManager) Group(java.security.acl.Group) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) PrincipalIterator(org.apache.jackrabbit.api.security.principal.PrincipalIterator) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Privilege(javax.jcr.security.Privilege) JackrabbitAccessControlList(org.apache.jackrabbit.api.security.JackrabbitAccessControlList) Principal(java.security.Principal)

Aggregations

PrincipalManager (org.apache.jackrabbit.api.security.principal.PrincipalManager)27 Principal (java.security.Principal)16 Test (org.junit.Test)11 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)9 PrincipalIterator (org.apache.jackrabbit.api.security.principal.PrincipalIterator)9 HashSet (java.util.HashSet)6 RepositoryException (javax.jcr.RepositoryException)6 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)6 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)4 Session (javax.jcr.Session)3 AccessControlPolicy (javax.jcr.security.AccessControlPolicy)3 JackrabbitAccessControlManager (org.apache.jackrabbit.api.security.JackrabbitAccessControlManager)3 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)3 Group (org.apache.jackrabbit.api.security.user.Group)3 NodeImpl (org.apache.jackrabbit.core.NodeImpl)3 PrincipalImpl (org.apache.jackrabbit.core.security.principal.PrincipalImpl)3 AbstractCompositeConfigurationTest (org.apache.jackrabbit.oak.spi.security.AbstractCompositeConfigurationTest)3 EveryonePrincipal (org.apache.jackrabbit.oak.spi.security.principal.EveryonePrincipal)3 Group (java.security.acl.Group)2 ArrayList (java.util.ArrayList)2