Search in sources :

Example 31 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class JcrTenantUtils method getTenantedUser.

public static String getTenantedUser(String username) {
    if (username != null && !username.equals(getRepositoryAdminUserName()) && getUserNameUtils() != null) {
        ITenant tenant = getUserNameUtils().getTenant(username);
        if (tenant == null || tenant.getId() == null) {
            IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
            String tenantId = (String) pentahoSession.getAttribute(IPentahoSession.TENANT_ID_KEY);
            if (tenantId == null) {
                tenantId = getDefaultTenantPath();
            }
            tenant = new Tenant(tenantId, true);
            return getUserNameUtils().getPrincipleId(tenant, username);
        } else {
            return username;
        }
    } else {
        return username;
    }
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Example 32 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class JcrTenantUtils method getTenantedRole.

public static String getTenantedRole(String principal) {
    if (principal != null && !principal.equals("administrators") && getRoleNameUtils() != null) {
        ITenant tenant = getRoleNameUtils().getTenant(principal);
        if (tenant == null || tenant.getId() == null) {
            IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
            String tenantId = (String) pentahoSession.getAttribute(IPentahoSession.TENANT_ID_KEY);
            if (tenantId == null) {
                tenantId = getDefaultTenantPath();
            }
            tenant = new Tenant(tenantId, true);
            return getRoleNameUtils().getPrincipleId(tenant, principal);
        } else {
            return principal;
        }
    } else {
        return principal;
    }
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Example 33 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class PentahoSessionCredentialsStrategy method getUserId.

private String getUserId() {
    IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
    Assert.state(pentahoSession != null, "this method cannot be called with a null IPentahoSession");
    return JcrTenantUtils.getTenantedUser(pentahoSession.getName());
}
Also used : IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Example 34 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession 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 35 with IPentahoSession

use of org.pentaho.platform.api.engine.IPentahoSession in project pentaho-platform by pentaho.

the class SessionCachingMetadataDomainRepositoryIT method testOnLogout.

public void testOnLogout() throws Exception {
    // $NON-NLS-1$
    final String ID1 = "1";
    // $NON-NLS-1$
    final String ID2 = "2";
    MockSessionAwareMetadataDomainRepository mock = new MockSessionAwareMetadataDomainRepository();
    SessionCachingMetadataDomainRepository repo = new SessionCachingMetadataDomainRepository(mock);
    repo.storeDomain(getTestDomain(ID1), false);
    repo.storeDomain(getTestDomain(ID2), false);
    // $NON-NLS-1$ //$NON-NLS-2$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", "1"));
    repo.getDomain(ID1);
    // $NON-NLS-1$ //$NON-NLS-2$
    IPentahoSession session2 = new StandaloneSession("Standalone Session", "2");
    PentahoSessionHolder.setSession(session2);
    repo.getDomain(ID2);
    assertEquals(2, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // Logging out session 2 should only remove cached domains from session 2
    repo.onLogout(session2);
    assertEquals(1, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) SessionCachingMetadataDomainRepository(org.pentaho.platform.plugin.services.metadata.SessionCachingMetadataDomainRepository)

Aggregations

IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)231 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)76 Test (org.junit.Test)70 Matchers.anyString (org.mockito.Matchers.anyString)40 ArrayList (java.util.ArrayList)32 ITenant (org.pentaho.platform.api.mt.ITenant)22 IOException (java.io.IOException)20 StandaloneObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneObjectFactory)18 File (java.io.File)17 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)16 Before (org.junit.Before)14 OutputStream (java.io.OutputStream)13 HashMap (java.util.HashMap)13 InputStream (java.io.InputStream)12 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)12 ModelInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)12 Domain (org.pentaho.metadata.model.Domain)11 ObjectFactoryException (org.pentaho.platform.api.engine.ObjectFactoryException)11 List (java.util.List)10 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)10