Search in sources :

Example 11 with ITenant

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

the class TenantUtils method getCurrentTenant.

/**
 * Returns the tenant ID of the current user.
 */
public static ITenant getCurrentTenant() {
    IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
    if (pentahoSession == null) {
        throw new IllegalStateException();
    }
    String tenantId = (String) pentahoSession.getAttribute(IPentahoSession.TENANT_ID_KEY);
    if (tenantId == null) {
        ITenantedPrincipleNameResolver tenantedUserNameUtils = PentahoSystem.get(ITenantedPrincipleNameResolver.class, "tenantedUserNameUtils", pentahoSession);
        if (tenantedUserNameUtils != null) {
            ITenant tenant = tenantedUserNameUtils.getTenant(pentahoSession.getId());
            pentahoSession.setAttribute(IPentahoSession.TENANT_ID_KEY, tenant.getId());
            return new Tenant(tenant.getId(), true);
        }
    }
    return new Tenant(tenantId, true);
}
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) ITenantedPrincipleNameResolver(org.pentaho.platform.api.mt.ITenantedPrincipleNameResolver)

Example 12 with ITenant

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

the class SecurityHelper method becomeUser.

/**
 * Hi-jacks the system for the named user. <p/> <p> This is for unit tests only. </p>
 */
@Override
public void becomeUser(final String principalName, final IParameterProvider paramProvider) {
    UserSession session = null;
    tenantedUserNameUtils = getTenantedUserNameUtils();
    if (tenantedUserNameUtils != null) {
        session = new UserSession(principalName, null, false, paramProvider);
        ITenant tenant = tenantedUserNameUtils.getTenant(principalName);
        session.setAttribute(IPentahoSession.TENANT_ID_KEY, tenant.getId());
        session.setAuthenticated(tenant.getId(), principalName);
    } else {
        session = new UserSession(principalName, null, false, paramProvider);
        session.setAuthenticated(principalName);
    }
    PentahoSessionHolder.setSession(session);
    Authentication auth = createAuthentication(principalName);
    // TODO We need to figure out how to inject this
    // Get the tenant id from the principle name and set it as an attribute of the pentaho session
    // Clearing the SecurityContext to force the subsequent call to getContext() to generate a new SecurityContext.
    // This prevents us from modifying the Authentication on a SecurityContext isntance which may be shared between
    // threads.
    PentahoSessionHolder.getSession().setAttribute(IPentahoSession.SESSION_ROLES, auth.getAuthorities());
    SecurityContextHolder.clearContext();
    SecurityContextHolder.getContext().setAuthentication(auth);
    PentahoSystem.sessionStartup(PentahoSessionHolder.getSession(), paramProvider);
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) Authentication(org.springframework.security.core.Authentication) UserSession(org.pentaho.platform.engine.core.system.UserSession)

Example 13 with ITenant

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

the class RepositoryImportResourceTest method setUp.

@Before
public void setUp() throws ObjectFactoryException, PlatformImportException, DomainIdNullException, DomainAlreadyExistsException, DomainStorageException, IOException {
    PentahoSystem.init();
    IPentahoSession session = mock(IPentahoSession.class);
    doReturn("sampleSession").when(session).getName();
    PentahoSessionHolder.setSession(session);
    handler = mock(SolutionImportHandler.class);
    importer = mock(PentahoPlatformImporter.class);
    policy = mock(IAuthorizationPolicy.class);
    ITenant tenat = mock(ITenant.class);
    resolver = mock(ITenantedPrincipleNameResolver.class);
    doReturn(tenat).when(resolver).getTenant(anyString());
    doReturn(REAL_USER).when(resolver).getPrincipleName(anyString());
    policy = mock(IAuthorizationPolicy.class);
    pentahoObjectFactory = mock(IPentahoObjectFactory.class);
    iPlatformMimeResolver = mock(NameBaseMimeResolver.class);
    iRepositoryImportLogger = mock(IRepositoryImportLogger.class);
    catalogService = mock(MondrianCatalogHelper.class);
    doReturn("xml").when(iPlatformMimeResolver).resolveMimeForFileName("");
    doReturn(iRepositoryImportLogger).when(importer).getRepositoryImportLogger();
    // for calling importFile in RepositoryImportResource
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            handler.importFile(any(IPlatformImportBundle.class));
            return null;
        }
    }).when(importer).importFile(any(IPlatformImportBundle.class));
    // for calling importFile in PentahoPlatformImporter
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            handler.getImportSession();
            return null;
        }
    }).when(handler).importFile(any(IPlatformImportBundle.class));
    // for calling getImportSession in SolutionImportHandler
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ImportSession importsession = ImportSession.getSession();
            importsession.setManifest(mock(ExportManifest.class));
            return null;
        }
    }).when(handler).getImportSession();
    when(pentahoObjectFactory.objectDefined(anyString())).thenReturn(true);
    when(pentahoObjectFactory.get(this.anyClass(), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            if (invocation.getArguments()[0].equals(IAuthorizationPolicy.class)) {
                return policy;
            }
            if (invocation.getArguments()[0].equals(ITenantedPrincipleNameResolver.class)) {
                return resolver;
            }
            if (invocation.getArguments()[0].equals(IMondrianCatalogService.class)) {
                return catalogService;
            }
            return null;
        }
    });
    PentahoSystem.registerObjectFactory(pentahoObjectFactory);
    PentahoSystem.registerObject(iPlatformMimeResolver);
    PentahoSystem.registerObject(iRepositoryImportLogger);
    PentahoSystem.registerObject(catalogService);
    PentahoSystem.registerObject(handler);
    PentahoSystem.registerObject(importer);
}
Also used : ImportSession(org.pentaho.platform.plugin.services.importexport.ImportSession) PentahoPlatformImporter(org.pentaho.platform.plugin.services.importer.PentahoPlatformImporter) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) NameBaseMimeResolver(org.pentaho.platform.plugin.services.importer.NameBaseMimeResolver) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) IRepositoryImportLogger(org.pentaho.platform.plugin.services.importexport.IRepositoryImportLogger) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) MondrianCatalogHelper(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogHelper) ITenant(org.pentaho.platform.api.mt.ITenant) SolutionImportHandler(org.pentaho.platform.plugin.services.importer.SolutionImportHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ITenantedPrincipleNameResolver(org.pentaho.platform.api.mt.ITenantedPrincipleNameResolver) Before(org.junit.Before)

Example 14 with ITenant

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

the class UserRoleDaoResource_RolesUpdatedTest method rolesUpdated_WhenAssignAllUsersToRole_WithSessionUser.

@Test
public void rolesUpdated_WhenAssignAllUsersToRole_WithSessionUser() {
    ITenant tenant = mock(ITenant.class);
    doReturn(tenant).when(resource).getTenant(DEFAULT_STRING);
    IPentahoUser sessionUser = new MockPentahoUser(tenant, SESSION_USER_NAME, DEFAULT_STRING, DEFAULT_STRING, true);
    doReturn(Collections.singletonList(sessionUser)).when(userRoleDao).getUsers(tenant);
    resource.assignAllUsersToRole(DEFAULT_STRING, ROLE_NAME_DEVELOPER);
    verify(resource).updateRolesForCurrentSession();
}
Also used : MockPentahoUser(org.pentaho.test.mock.MockPentahoUser) ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) Test(org.junit.Test)

Example 15 with ITenant

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

the class UserRoleDaoResource_RolesUpdatedTest method rolesNotUpdated_WhenAssignAllUsersToRole_WithNoSessionUser.

@Test
public void rolesNotUpdated_WhenAssignAllUsersToRole_WithNoSessionUser() {
    ITenant tenant = mock(ITenant.class);
    doReturn(tenant).when(resource).getTenant(DEFAULT_STRING);
    IPentahoUser nonSessionUser = new MockPentahoUser(tenant, NON_SESSION_USER_NAME, DEFAULT_STRING, DEFAULT_STRING, true);
    doReturn(Collections.singletonList(nonSessionUser)).when(userRoleDao).getUsers(tenant);
    resource.assignAllUsersToRole(DEFAULT_STRING, ROLE_NAME_DEVELOPER);
    verify(resource, never()).updateRolesForCurrentSession();
}
Also used : MockPentahoUser(org.pentaho.test.mock.MockPentahoUser) ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) 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