Search in sources :

Example 21 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class RepositoryTenantManager method createTenantFolder.

private RepositoryFile createTenantFolder(final ITenant parentTenant, final String tenantName, final String tenantCreatorId) {
    return (RepositoryFile) jcrTemplate.execute(new JcrCallback() {

        @Override
        public Object doInJcr(final Session session) throws RepositoryException {
            Tenant tenant = null;
            RepositoryFile parentFolder = null;
            if (parentTenant == null) {
                tenant = new Tenant("/" + tenantName, true);
            } else {
                tenant = new Tenant(parentTenant.getRootFolderAbsolutePath() + "/" + tenantName, true);
                String folderPath = parentTenant.getRootFolderAbsolutePath();
                parentFolder = repositoryFileDao.getFileByAbsolutePath(folderPath);
            }
            RepositoryFileAcl acl = new RepositoryFileAcl.Builder(tenantCreatorId).entriesInheriting(false).build();
            RepositoryFile systemTenantFolder = repositoryFileDao.createFolder(parentFolder != null ? parentFolder.getId() : null, new RepositoryFile.Builder(tenant.getName()).folder(true).build(), acl, "");
            repositoryFileDao.getFileByAbsolutePath(tenant.getId());
            Map<String, Serializable> fileMeta = repositoryFileDao.getFileMetadata(systemTenantFolder.getId());
            fileMeta.put(ITenantManager.TENANT_ROOT, true);
            fileMeta.put(ITenantManager.TENANT_ENABLED, true);
            JcrRepositoryFileUtils.setFileMetadata(session, systemTenantFolder.getId(), fileMeta);
            createRuntimeRolesFolderNode(session, new PentahoJcrConstants(session), tenant);
            return systemTenantFolder;
        }
    });
}
Also used : Serializable(java.io.Serializable) Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) PentahoJcrConstants(org.pentaho.platform.repository2.unified.jcr.PentahoJcrConstants) Builder(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl.Builder) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Example 22 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class RepositoryTenantManager method createTenant.

/*
   * (non-Javadoc)
   * 
   * @see org.pentaho.platform.api.repository2.unified.ITenantManager#createTenant(java.lang.String,
   * java.lang.String)
   */
@Override
public ITenant createTenant(final ITenant parentTenant, final String tenantName, final String tenantAdminRoleName, final String authenticatedRoleName, final String anonymousRoleName) {
    Tenant newTenant;
    String parentTenantFolder;
    if (parentTenant == null) {
        if (repositoryFileDao.getFileByAbsolutePath("/" + tenantName) != null) {
            return null;
        }
    } else {
        if (repositoryFileDao.getFileByAbsolutePath(parentTenant.getRootFolderAbsolutePath() + "/" + tenantName) != null) {
            return null;
        }
    }
    if (parentTenant == null) {
        newTenant = new Tenant(RepositoryFile.SEPARATOR + tenantName, true);
        parentTenantFolder = "/";
    } else {
        newTenant = new Tenant(parentTenant.getRootFolderAbsolutePath() + RepositoryFile.SEPARATOR + tenantName, true);
        parentTenantFolder = parentTenant.getRootFolderAbsolutePath();
    }
    String tenantCreatorId = PentahoSessionHolder.getSession().getName();
    RepositoryFile tenantRootFolder = createTenantFolder(parentTenant, tenantName, tenantCreatorId);
    userRoleDao.createRole(newTenant, tenantAdminRoleName, "", new String[0]);
    userRoleDao.createRole(newTenant, authenticatedRoleName, "", new String[0]);
    userRoleDao.createRole(newTenant, anonymousRoleName, "", new String[0]);
    roleBindingDao.setRoleBindings(newTenant, authenticatedRoleName, singleTenantAuthenticatedAuthorityRoleBindingList);
    String tenantAdminRoleId = tenantedRoleNameResolver.getPrincipleId(newTenant, tenantAdminRoleName);
    RepositoryFileSid tenantAdminRoleSid = new RepositoryFileSid(tenantAdminRoleId, Type.ROLE);
    this.jcrTemplate.save();
    // tenant admin permissions on the root folder.
    if (parentTenant == null) {
        repositoryFileAclDao.addAce(tenantRootFolder.getId(), tenantAdminRoleSid, EnumSet.of(RepositoryFilePermission.ALL));
    } else {
        RepositoryFileAcl acl = repositoryFileAclDao.getAcl(tenantRootFolder.getId());
        Builder aclBuilder = new RepositoryFileAcl.Builder(acl).ace(tenantAdminRoleSid, EnumSet.of(RepositoryFilePermission.ALL));
        IPentahoSession origPentahoSession = PentahoSessionHolder.getSession();
        Authentication origAuthentication = SecurityContextHolder.getContext().getAuthentication();
        login(repositoryAdminUsername, tenantAdminRoleId);
        try {
            // Give all to Tenant Admin of all ancestors
            while (!parentTenantFolder.equals("/")) {
                ITenant tenant = new Tenant(parentTenantFolder, true);
                String parentTenantAdminRoleId = tenantedRoleNameResolver.getPrincipleId(tenant, tenantAdminRoleName);
                RepositoryFileSid parentTenantAdminSid = new RepositoryFileSid(parentTenantAdminRoleId, Type.ROLE);
                aclBuilder.ace(parentTenantAdminSid, EnumSet.of(RepositoryFilePermission.ALL));
                parentTenantFolder = FilenameUtils.getFullPathNoEndSeparator(parentTenantFolder);
            }
            repositoryFileAclDao.updateAcl(aclBuilder.build());
        } catch (Throwable th) {
            th.printStackTrace();
        } finally {
            PentahoSessionHolder.setSession(origPentahoSession);
            SecurityContextHolder.getContext().setAuthentication(origAuthentication);
        }
    }
    try {
        RepositoryFileSid fileOwnerSid = new RepositoryFileSid(tenantCreatorId);
        createInitialTenantFolders(newTenant, tenantRootFolder, fileOwnerSid);
    } catch (Exception ex) {
        throw new RuntimeException("Error creating initial tenant folders", ex);
    }
    return newTenant;
}
Also used : IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Builder(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl.Builder) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) RepositoryFileSid(org.pentaho.platform.api.repository2.unified.RepositoryFileSid) Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) ITenant(org.pentaho.platform.api.mt.ITenant) Builder(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl.Builder) Authentication(org.springframework.security.core.Authentication) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)

Example 23 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class DirectoryResourceIT method testCreateDir_RootLevel.

@Test
public void testCreateDir_RootLevel() {
    loginAsRepositoryAdmin();
    ITenant systemTenant = tenantManager.createTenant(null, ServerRepositoryPaths.getPentahoRootFolderName(), adminAuthorityName, authenticatedAuthorityName, "Anonymous");
    userRoleDao.createUser(systemTenant, sysAdminUserName, "password", "", new String[] { adminAuthorityName });
    ITenant mainTenant_1 = tenantManager.createTenant(systemTenant, MAIN_TENANT_1, adminAuthorityName, authenticatedAuthorityName, "Anonymous");
    userRoleDao.createUser(mainTenant_1, "admin", "password", "", new String[] { adminAuthorityName });
    try {
        login("admin", mainTenant_1, new String[] { authenticatedAuthorityName });
        // set object in PentahoSystem
        mp.defineInstance(IUnifiedRepository.class, repo);
        WebResource webResource = resource();
        String rootLevelDirPathId = ":testRootLevelDir";
        // Create duplicate directory. FORBIDDEN (403) is expected.
        try {
            webResource.path("repo/dirs/" + rootLevelDirPathId).put();
            fail("FORBIDDEN is expected");
        } catch (UniformInterfaceException e) {
            assertEquals(Response.Status.FORBIDDEN.getStatusCode(), e.getResponse().getStatus());
        }
    } catch (AssertionError assertion) {
        throw assertion;
    } catch (Throwable ex) {
        TestCase.fail(ex.getMessage());
    } finally {
        cleanupUserAndRoles(mainTenant_1);
        cleanupUserAndRoles(systemTenant);
        logout();
    }
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) WebResource(com.sun.jersey.api.client.WebResource) JerseyTest(com.sun.jersey.test.framework.JerseyTest) Test(org.junit.Test)

Example 24 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class DirectoryResourceIT method testCreateDir.

@Test
public void testCreateDir() {
    loginAsRepositoryAdmin();
    ITenant systemTenant = tenantManager.createTenant(null, ServerRepositoryPaths.getPentahoRootFolderName(), adminAuthorityName, authenticatedAuthorityName, "Anonymous");
    userRoleDao.createUser(systemTenant, sysAdminUserName, "password", "", new String[] { adminAuthorityName });
    ITenant mainTenant_1 = tenantManager.createTenant(systemTenant, MAIN_TENANT_1, adminAuthorityName, authenticatedAuthorityName, "Anonymous");
    userRoleDao.createUser(mainTenant_1, "admin", "password", "", new String[] { adminAuthorityName });
    try {
        login("admin", mainTenant_1, new String[] { authenticatedAuthorityName });
        // set object in PentahoSystem
        mp.defineInstance(IUnifiedRepository.class, repo);
        WebResource webResource = resource();
        String publicFolderPath = ClientRepositoryPaths.getPublicFolderPath();
        String newDirPathId = publicFolderPath.replaceAll("/", ":") + ":testDir";
        // Create directory. Success is expected.
        webResource.path("repo/dirs/" + newDirPathId).put();
        // Create duplicate directory. CONFLICT (409) is expected.
        try {
            webResource.path("repo/dirs/" + newDirPathId).put();
            fail("CONFLICT is expected");
        } catch (UniformInterfaceException e) {
            assertEquals(Response.Status.CONFLICT.getStatusCode(), e.getResponse().getStatus());
        }
    } catch (AssertionError assertion) {
        throw assertion;
    } catch (Throwable ex) {
        TestCase.fail(ex.getMessage());
    } finally {
        cleanupUserAndRoles(mainTenant_1);
        cleanupUserAndRoles(systemTenant);
        logout();
    }
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) WebResource(com.sun.jersey.api.client.WebResource) JerseyTest(com.sun.jersey.test.framework.JerseyTest) Test(org.junit.Test)

Example 25 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class DirectoryResourceIT method testCreateDir_ServerError.

@Test
public void testCreateDir_ServerError() {
    loginAsRepositoryAdmin();
    ITenant systemTenant = tenantManager.createTenant(null, ServerRepositoryPaths.getPentahoRootFolderName(), adminAuthorityName, authenticatedAuthorityName, "Anonymous");
    userRoleDao.createUser(systemTenant, sysAdminUserName, "password", "", new String[] { adminAuthorityName });
    ITenant mainTenant_1 = tenantManager.createTenant(systemTenant, MAIN_TENANT_1, adminAuthorityName, authenticatedAuthorityName, "Anonymous");
    userRoleDao.createUser(mainTenant_1, "admin", "password", "", new String[] { adminAuthorityName });
    try {
        login("admin", mainTenant_1, new String[] { authenticatedAuthorityName });
        // set object in PentahoSystem
        mp.defineInstance(IUnifiedRepository.class, repo);
        WebResource webResource = resource();
        String invalidPathId = "/////";
        // Invalid path id. INTERNAL_SERVER_ERROR (500) is expected.
        try {
            webResource.path("repo/dirs/" + invalidPathId).put();
            fail("INTERNAL_SERVER_ERROR is expected");
        } catch (UniformInterfaceException e) {
            assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getResponse().getStatus());
        }
    } catch (AssertionError assertion) {
        throw assertion;
    } catch (Throwable ex) {
        TestCase.fail(ex.getMessage());
    } finally {
        cleanupUserAndRoles(mainTenant_1);
        cleanupUserAndRoles(systemTenant);
        logout();
    }
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) WebResource(com.sun.jersey.api.client.WebResource) JerseyTest(com.sun.jersey.test.framework.JerseyTest) Test(org.junit.Test)

Aggregations

ITenant (org.pentaho.platform.api.mt.ITenant)174 Test (org.junit.Test)120 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)86 Matchers.anyString (org.mockito.Matchers.anyString)47 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)27 ArrayList (java.util.ArrayList)21 Tenant (org.pentaho.platform.core.mt.Tenant)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)17 RepositoryFileSid (org.pentaho.platform.api.repository2.unified.RepositoryFileSid)15 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)14 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)14 IPentahoUser (org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)12 WebResource (com.sun.jersey.api.client.WebResource)11 JerseyTest (com.sun.jersey.test.framework.JerseyTest)11 ITenantedPrincipleNameResolver (org.pentaho.platform.api.mt.ITenantedPrincipleNameResolver)10 SampleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.sample.SampleRepositoryFileData)10 Serializable (java.io.Serializable)9 Date (java.util.Date)9 HashMap (java.util.HashMap)9