Search in sources :

Example 26 with JackrabbitSession

use of org.apache.jackrabbit.api.JackrabbitSession in project jackrabbit-oak by apache.

the class AccessControlImporter method init.

//----------------------------------------------< ProtectedItemImporter >---
@Override
public boolean init(@Nonnull Session session, @Nonnull Root root, @Nonnull NamePathMapper namePathMapper, boolean isWorkspaceImport, int uuidBehavior, @Nonnull ReferenceChangeTracker referenceTracker, @Nonnull SecurityProvider securityProvider) {
    if (initialized) {
        throw new IllegalStateException("Already initialized");
    }
    if (!(session instanceof JackrabbitSession)) {
        return false;
    }
    try {
        AuthorizationConfiguration config = securityProvider.getConfiguration(AuthorizationConfiguration.class);
        importBehavior = Util.getImportBehavior(config);
        if (isWorkspaceImport) {
            acMgr = config.getAccessControlManager(root, namePathMapper);
            PrincipalConfiguration pConfig = securityProvider.getConfiguration(PrincipalConfiguration.class);
            principalManager = pConfig.getPrincipalManager(root, namePathMapper);
        } else {
            acMgr = session.getAccessControlManager();
            principalManager = ((JackrabbitSession) session).getPrincipalManager();
        }
        ntMgr = ReadOnlyNodeTypeManager.getInstance(root, namePathMapper);
        initialized = true;
    } catch (RepositoryException e) {
        log.warn("Error while initializing access control importer", e);
    }
    return initialized;
}
Also used : AuthorizationConfiguration(org.apache.jackrabbit.oak.spi.security.authorization.AuthorizationConfiguration) PrincipalConfiguration(org.apache.jackrabbit.oak.spi.security.principal.PrincipalConfiguration) RepositoryException(javax.jcr.RepositoryException) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 27 with JackrabbitSession

use of org.apache.jackrabbit.api.JackrabbitSession in project jackrabbit by apache.

the class UserPerWorkspaceSecurityManagerTest method testUsersArePerWorkspace.

public void testUsersArePerWorkspace() throws Exception {
    String altWsp = getAlternativeWorkspaceName();
    if (altWsp == null) {
        throw new NotExecutableException();
    }
    Session s = getHelper().getSuperuserSession(altWsp);
    User u = null;
    try {
        // other users created in the default workspace...
        u = ((JackrabbitSession) superuser).getUserManager().createUser("testUser", "testUser");
        superuser.save();
        // ... must not be present in the alternate-workspace
        UserManager umgr = ((JackrabbitSession) s).getUserManager();
        assertNull(umgr.getAuthorizable("testUser"));
        try {
            Session us = getHelper().getRepository().login(new SimpleCredentials("testUser", "testUser".toCharArray()), altWsp);
            us.logout();
            fail("testUser must not be able to login to a workspace without this user.");
        } catch (LoginException e) {
        // success
        }
    } finally {
        s.logout();
        if (u != null) {
            u.remove();
            superuser.save();
        }
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) User(org.apache.jackrabbit.api.security.user.User) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) UserManager(org.apache.jackrabbit.api.security.user.UserManager) LoginException(javax.jcr.LoginException) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 28 with JackrabbitSession

use of org.apache.jackrabbit.api.JackrabbitSession in project jackrabbit by apache.

the class UserPerWorkspaceSecurityManagerTest method testUpdateUser.

public void testUpdateUser() throws NotExecutableException, RepositoryException {
    // create the same use in 2 different workspace must make the 'corresponding'
    // and updating must succeed
    String altWsp = getAlternativeWorkspaceName();
    if (altWsp == null) {
        throw new NotExecutableException();
    }
    UserManager uMgr = ((JackrabbitSession) superuser).getUserManager();
    Session s = getHelper().getSuperuserSession(altWsp);
    User u = null;
    try {
        // other users created in the default workspace...
        u = uMgr.createUser("testUser", "testUser");
        superuser.save();
        String userPath = null;
        if (u.getPrincipal() instanceof ItemBasedPrincipal) {
            userPath = ((ItemBasedPrincipal) u.getPrincipal()).getPath();
            assertTrue(superuser.nodeExists(userPath));
        } else {
            throw new NotExecutableException();
        }
        // ... must not be present in the alternate-workspace
        UserManager umgr = ((JackrabbitSession) s).getUserManager();
        assertNull(umgr.getAuthorizable("testUser"));
        assertFalse(s.nodeExists(userPath));
        User u2 = umgr.createUser("testUser", "testUser");
        s.save();
        assertTrue(s.nodeExists(userPath));
        Value value = superuser.getValueFactory().createValue("anyValue");
        u.setProperty(propertyName1, value);
        superuser.save();
        // no automatic sync.
        assertFalse(u2.hasProperty(propertyName1));
        // update nodes
        Node n2 = s.getNode(userPath);
        n2.update(superuser.getWorkspace().getName());
        // now the value must be visible
        assertTrue(u2.hasProperty(propertyName1));
        assertEquals(value.getString(), u2.getProperty(propertyName1)[0].getString());
    } finally {
        // remove the test user in the destination workspace
        Authorizable dest = ((JackrabbitSession) s).getUserManager().getAuthorizable("testUser");
        if (dest != null) {
            dest.remove();
            s.save();
        }
        // logout the session to the destination workspace
        s.logout();
        if (u != null) {
            // and remove it in the default workspace as well
            u.remove();
            superuser.save();
        }
    }
}
Also used : User(org.apache.jackrabbit.api.security.user.User) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) UserManager(org.apache.jackrabbit.api.security.user.UserManager) Node(javax.jcr.Node) ItemBasedPrincipal(org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal) Value(javax.jcr.Value) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Example 29 with JackrabbitSession

use of org.apache.jackrabbit.api.JackrabbitSession in project jackrabbit by apache.

the class UserPerWorkspaceSecurityManagerTest method testSystemUsersAreSaved.

public void testSystemUsersAreSaved() throws Exception {
    String adminId = ((UserPerWorkspaceSecurityManager) secMgr).adminId;
    UserManager umgr = ((JackrabbitSession) superuser).getUserManager();
    Principal p = umgr.getAuthorizable(adminId).getPrincipal();
    if (p instanceof ItemBasedPrincipal) {
        Item item = superuser.getItem(((ItemBasedPrincipal) p).getPath());
        assertFalse(item.isNew());
        assertFalse(item.isModified());
    }
}
Also used : Item(javax.jcr.Item) UserManager(org.apache.jackrabbit.api.security.user.UserManager) ItemBasedPrincipal(org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Principal(java.security.Principal) ItemBasedPrincipal(org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal)

Example 30 with JackrabbitSession

use of org.apache.jackrabbit.api.JackrabbitSession in project jackrabbit by apache.

the class UserPerWorkspaceSecurityManagerTest method testCloneUser.

public void testCloneUser() throws Exception {
    String altWsp = getAlternativeWorkspaceName();
    if (altWsp == null) {
        throw new NotExecutableException();
    }
    UserManager uMgr = ((JackrabbitSession) superuser).getUserManager();
    Session s = getHelper().getSuperuserSession(altWsp);
    User u = null;
    try {
        // other users created in the default workspace...
        u = uMgr.createUser("testUser", "testUser");
        superuser.save();
        String userPath = null;
        if (u.getPrincipal() instanceof ItemBasedPrincipal) {
            userPath = ((ItemBasedPrincipal) u.getPrincipal()).getPath();
            assertTrue(superuser.nodeExists(userPath));
        } else {
            throw new NotExecutableException();
        }
        // ... must not be present in the alternate-workspace
        UserManager umgr = ((JackrabbitSession) s).getUserManager();
        assertNull(umgr.getAuthorizable("testUser"));
        assertFalse(s.nodeExists(userPath));
        String clonePath = userPath;
        String parentPath = Text.getRelativeParent(clonePath, 1);
        while (!s.nodeExists(parentPath)) {
            clonePath = parentPath;
            parentPath = Text.getRelativeParent(parentPath, 1);
        }
        // clone the user into the second workspace
        s.getWorkspace().clone(superuser.getWorkspace().getName(), clonePath, clonePath, true);
        // ... now the user must be visible
        assertNotNull(umgr.getAuthorizable("testUser"));
        if (userPath != null) {
            assertTrue(s.nodeExists(userPath));
        }
        // ... and able to login to that workspace
        Session us = getHelper().getRepository().login(new SimpleCredentials("testUser", "testUser".toCharArray()), altWsp);
        us.logout();
    } finally {
        // remove the test user in the second workspace
        Authorizable dest = ((JackrabbitSession) s).getUserManager().getAuthorizable("testUser");
        if (dest != null) {
            dest.remove();
            s.save();
        }
        // logout the session
        s.logout();
        if (u != null) {
            // remove as well in the first workspace
            u.remove();
            superuser.save();
        }
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) User(org.apache.jackrabbit.api.security.user.User) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) UserManager(org.apache.jackrabbit.api.security.user.UserManager) ItemBasedPrincipal(org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession)

Aggregations

JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)114 UserManager (org.apache.jackrabbit.api.security.user.UserManager)51 Session (javax.jcr.Session)50 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)34 Node (javax.jcr.Node)25 Group (org.apache.jackrabbit.api.security.user.Group)25 User (org.apache.jackrabbit.api.security.user.User)24 Principal (java.security.Principal)19 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)17 Test (org.junit.Test)16 SimpleCredentials (javax.jcr.SimpleCredentials)15 PrincipalImpl (org.apache.jackrabbit.oak.spi.security.principal.PrincipalImpl)11 RepositoryException (javax.jcr.RepositoryException)10 PrincipalIterator (org.apache.jackrabbit.api.security.principal.PrincipalIterator)9 PrincipalManager (org.apache.jackrabbit.api.security.principal.PrincipalManager)9 Privilege (javax.jcr.security.Privilege)8 LoginException (javax.jcr.LoginException)6 Property (javax.jcr.Property)6 ItemBasedPrincipal (org.apache.jackrabbit.api.security.principal.ItemBasedPrincipal)6 Item (javax.jcr.Item)5