Search in sources :

Example 91 with ITenant

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

the class PentahoSamlAuthenticationSuccessHandler method createUserHomeFolder.

private void createUserHomeFolder(final String username) {
    final ITenant tenantName = JcrTenantUtils.getTenant(username, true);
    final ISecurityHelper securityHelper = PentahoSystem.get(ISecurityHelper.class) != null ? PentahoSystem.get(ISecurityHelper.class) : SecurityHelper.getInstance();
    final IBackingRepositoryLifecycleManager lifecycleManager = PentahoSystem.get(IBackingRepositoryLifecycleManager.class);
    if (tenantName == null || securityHelper == null || lifecycleManager == null) {
        logger.error("null " + (tenantName == null ? "ITenant" : securityHelper == null ? "ISecurityHelper" : "IBackingRepositoryLifecycleManager"));
        return;
    }
    try {
        securityHelper.runAsSystem(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                // Execute new tenant with the tenant id from the logged in user
                lifecycleManager.newTenant(tenantName);
                return null;
            }
        });
    } catch (Exception e) {
        logger.error(e.getLocalizedMessage(), e);
    }
    try {
        securityHelper.runAsSystem(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                // Execute new tenant with the tenant id from the logged in user
                lifecycleManager.newUser(tenantName, username);
                return null;
            }
        });
    } catch (Exception e) {
        logger.error(e.getLocalizedMessage(), e);
    }
    try {
        // The newTenant() call should be executed as the system (or more correctly the tenantAdmin)
        securityHelper.runAsSystem(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                lifecycleManager.newTenant();
                return null;
            }
        });
    } catch (Exception e) {
        logger.error(e.getLocalizedMessage(), e);
    }
    try {
        // run as user to populate SecurityContextHolder and PentahoSessionHolder since Spring Security events are
        // fired before SecurityContextHolder is set
        securityHelper.runAsUser(username, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                lifecycleManager.newUser();
                return null;
            }
        });
    } catch (Exception e) {
        logger.error(e.getLocalizedMessage(), e);
    }
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) ISecurityHelper(org.pentaho.platform.api.engine.ISecurityHelper) ServletException(javax.servlet.ServletException) ProxyException(org.pentaho.platform.proxy.impl.ProxyException) IOException(java.io.IOException) IBackingRepositoryLifecycleManager(org.pentaho.platform.api.repository2.unified.IBackingRepositoryLifecycleManager)

Example 92 with ITenant

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

the class PurRepositoryIT method setUp.

@Before
public void setUp() throws Exception {
    this.testContextManager = new TestContextManager(getClass());
    this.testContextManager.prepareTestInstance(this);
    IRepositoryVersionManager mockRepositoryVersionManager = mock(IRepositoryVersionManager.class);
    when(mockRepositoryVersionManager.isVersioningEnabled(anyString())).thenReturn(true);
    when(mockRepositoryVersionManager.isVersionCommentEnabled(anyString())).thenReturn(false);
    JcrRepositoryFileUtils.setRepositoryVersionManager(mockRepositoryVersionManager);
    loginAsRepositoryAdmin();
    SimpleJcrTestUtils.deleteItem(testJcrTemplate, ServerRepositoryPaths.getPentahoRootFolderPath());
    mp = new MicroPlatform();
    // used by DefaultPentahoJackrabbitAccessControlHelper
    mp.defineInstance("tenantedUserNameUtils", userNameUtils);
    mp.defineInstance("tenantedRoleNameUtils", roleNameUtils);
    mp.defineInstance(IAuthorizationPolicy.class, authorizationPolicy);
    mp.defineInstance(ITenantManager.class, tenantManager);
    mp.defineInstance("roleAuthorizationPolicyRoleBindingDaoTarget", roleBindingDaoTarget);
    mp.defineInstance("repositoryAdminUsername", repositoryAdminUsername);
    mp.defineInstance("RepositoryFileProxyFactory", new RepositoryFileProxyFactory(testJcrTemplate, repositoryFileDao));
    mp.defineInstance("useMultiByteEncoding", new Boolean(false));
    // Start the micro-platform
    mp.start();
    loginAsRepositoryAdmin();
    setAclManagement();
    systemTenant = tenantManager.createTenant(null, ServerRepositoryPaths.getPentahoRootFolderName(), singleTenantAdminRoleName, tenantAuthenticatedRoleName, "Anonymous");
    userRoleDao.createUser(systemTenant, sysAdminUserName, "password", "", new String[] { singleTenantAdminRoleName });
    logout();
    super.setUp();
    KettleEnvironment.init();
    // programmatically register plugins, annotation based plugins do not get loaded unless
    // they are in kettle's plugins folder.
    JobEntryPluginType.getInstance().registerCustom(JobEntryAttributeTesterJobEntry.class, "test", "JobEntryAttributeTester", "JobEntryAttributeTester", "JobEntryAttributeTester", "");
    StepPluginType.getInstance().registerCustom(TransStepAttributeTesterTransStep.class, "test", "StepAttributeTester", "StepAttributeTester", "StepAttributeTester", "");
    repositoryMeta = new PurRepositoryMeta();
    repositoryMeta.setName("JackRabbit");
    repositoryMeta.setDescription("JackRabbit test repository");
    userInfo = new UserInfo(EXP_LOGIN, "password", EXP_USERNAME, "Apache Tomcat user", true);
    repository = new PurRepository();
    repository.init(repositoryMeta);
    login(sysAdminUserName, systemTenant, new String[] { singleTenantAdminRoleName, tenantAuthenticatedRoleName });
    ITenant tenantAcme = tenantManager.createTenant(systemTenant, EXP_TENANT, singleTenantAdminRoleName, tenantAuthenticatedRoleName, "Anonymous");
    userRoleDao.createUser(tenantAcme, EXP_LOGIN, "password", "", new String[] { singleTenantAdminRoleName });
    logout();
    setUpUser();
    PurRepository purRep = (PurRepository) repository;
    purRep.setPurRepositoryConnector(new PurRepositoryConnector(purRep, (PurRepositoryMeta) repositoryMeta, purRep.getRootRef()));
    ((PurRepository) repository).setTest(repo);
    repository.connect(EXP_LOGIN, "password");
    login(EXP_LOGIN, tenantAcme, new String[] { singleTenantAdminRoleName, tenantAuthenticatedRoleName });
    System.out.println("PUR NAME!!!: " + repo.getClass().getCanonicalName());
    RepositoryFile repositoryFile = repo.getFile(ClientRepositoryPaths.getPublicFolderPath());
    Serializable repositoryFileId = repositoryFile.getId();
    List<RepositoryFile> files = repo.getChildren(repositoryFileId);
    StringBuilder buf = new StringBuilder();
    for (RepositoryFile file : files) {
        buf.append("\n").append(file);
    }
    assertTrue("files not deleted: " + buf, files.isEmpty());
}
Also used : Serializable(java.io.Serializable) UserInfo(org.pentaho.di.repository.UserInfo) TestContextManager(org.springframework.test.context.TestContextManager) IRepositoryVersionManager(org.pentaho.platform.api.repository2.unified.IRepositoryVersionManager) ITenant(org.pentaho.platform.api.mt.ITenant) MicroPlatform(org.pentaho.test.platform.engine.core.MicroPlatform) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileProxyFactory(org.pentaho.platform.repository2.unified.jcr.RepositoryFileProxyFactory) Before(org.junit.Before)

Example 93 with ITenant

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

the class PurRepositoryIT method getTenant.

protected ITenant getTenant(String principalId, boolean isUser) {
    ITenant tenant = null;
    ITenantedPrincipleNameResolver nameUtils = isUser ? userNameUtils : roleNameUtils;
    if (nameUtils != null) {
        tenant = nameUtils.getTenant(principalId);
    }
    if (tenant == null || tenant.getId() == null) {
        tenant = getCurrentTenant();
    }
    return tenant;
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) ITenantedPrincipleNameResolver(org.pentaho.platform.api.mt.ITenantedPrincipleNameResolver)

Example 94 with ITenant

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

the class SolutionImportHandler method importRoles.

protected void importRoles(List<RoleExport> roles, Map<String, List<String>> roleToUserMap) {
    IUserRoleDao roleDao = PentahoSystem.get(IUserRoleDao.class);
    ITenant tenant = new Tenant("/pentaho/" + TenantUtils.getDefaultTenant(), true);
    IRoleAuthorizationPolicyRoleBindingDao roleBindingDao = PentahoSystem.get(IRoleAuthorizationPolicyRoleBindingDao.class);
    Set<String> existingRoles = new HashSet<>();
    if (roles != null) {
        for (RoleExport role : roles) {
            log.debug("Importing role: " + role.getRolename());
            try {
                List<String> users = roleToUserMap.get(role.getRolename());
                String[] userarray = users == null ? new String[] {} : users.toArray(new String[] {});
                IPentahoRole role1 = roleDao.createRole(tenant, role.getRolename(), null, userarray);
            } catch (AlreadyExistsException e) {
                existingRoles.add(role.getRolename());
                // it's ok if the role already exists, it is probably a default role
                log.info(Messages.getInstance().getString("ROLE.Already.Exists", role.getRolename()));
            }
            try {
                if (existingRoles.contains(role.getRolename())) {
                    // Only update an existing role if the overwrite flag is set
                    if (isOverwriteFile()) {
                        roleBindingDao.setRoleBindings(tenant, role.getRolename(), role.getPermissions());
                    }
                } else {
                    // Always write a roles permissions that were not previously existing
                    roleBindingDao.setRoleBindings(tenant, role.getRolename(), role.getPermissions());
                }
            } catch (Exception e) {
                log.info(Messages.getInstance().getString("ERROR.SettingRolePermissions", role.getRolename()), e);
            }
        }
    }
}
Also used : IRoleAuthorizationPolicyRoleBindingDao(org.pentaho.platform.security.policy.rolebased.IRoleAuthorizationPolicyRoleBindingDao) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) ITenant(org.pentaho.platform.api.mt.ITenant) Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) RoleExport(org.pentaho.platform.plugin.services.importexport.RoleExport) HashSet(java.util.HashSet)

Example 95 with ITenant

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

the class UserRoleDaoService method assignRolesToUser.

public void assignRolesToUser(String userName, String roleNames) throws NotFoundException, UncategorizedUserRoleDaoException, SecurityException {
    if (canAdminister()) {
        StringTokenizer tokenizer = new StringTokenizer(roleNames, "\t");
        Set<String> assignedRoles = new HashSet<>();
        ITenant tenant = TenantUtils.getCurrentTenant();
        // Build the set of roles the user already contians
        for (IPentahoRole pentahoRole : getRoleDao().getUserRoles(tenant, userName)) {
            assignedRoles.add(pentahoRole.getName());
        }
        // Append the parameter of roles
        while (tokenizer.hasMoreTokens()) {
            assignedRoles.add(tokenizer.nextToken());
        }
        getRoleDao().setUserRoles(tenant, userName, assignedRoles.toArray(new String[assignedRoles.size()]));
    } else {
        throw new SecurityException();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) HashSet(java.util.HashSet)

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