Search in sources :

Example 16 with StandaloneSession

use of org.pentaho.platform.engine.core.system.StandaloneSession in project pentaho-platform by pentaho.

the class FileResourceIT method login.

/**
 * Logs in with given username.
 *
 * @param username username of user
 * @param tenant   tenant to which this user belongs
 * @tenantAdmin true to add the tenant admin authority to the user's roles
 */
protected void login(final String username, final ITenant tenant, final boolean tenantAdmin) {
    StandaloneSession pentahoSession = new StandaloneSession(username);
    pentahoSession.setAuthenticated(username);
    pentahoSession.setAttribute(IPentahoSession.TENANT_ID_KEY, tenant.getId());
    final String password = "password";
    List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
    authList.add(new SimpleGrantedAuthority(authenticatedAuthorityName));
    if (tenantAdmin) {
        authList.add(new SimpleGrantedAuthority(adminAuthorityName));
    }
    UserDetails userDetails = new User(username, password, true, true, true, true, authList);
    Authentication auth = new UsernamePasswordAuthenticationToken(userDetails, password, authList);
    PentahoSessionHolder.setSession(pentahoSession);
    // this line necessary for Spring Security's MethodSecurityInterceptor
    SecurityContextHolder.getContext().setAuthentication(auth);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 17 with StandaloneSession

use of org.pentaho.platform.engine.core.system.StandaloneSession in project pentaho-platform by pentaho.

the class SessionCachingMetadataDomainRepositoryIT method testGetDomain_differentSessions.

public void testGetDomain_differentSessions() throws Exception {
    // $NON-NLS-1$
    final String SESSION_ID1 = "1234";
    // $NON-NLS-1$
    final String SESSION_ID2 = "5678";
    // $NON-NLS-1$
    final String ID = "1";
    MockSessionAwareMetadataDomainRepository mock = new MockSessionAwareMetadataDomainRepository();
    mock.storeDomain(getTestDomain(ID), false);
    SessionCachingMetadataDomainRepository repo = new SessionCachingMetadataDomainRepository(mock);
    // $NON-NLS-1$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", SESSION_ID1));
    // $NON-NLS-1$
    assertEquals(0, mock.getInvocationCount("getDomain"));
    Domain d = repo.getDomain(ID);
    assertEquals(ID, d.getId());
    assertEquals(SESSION_ID1, d.getDescription(TEST_LOCALE));
    // $NON-NLS-1$
    assertEquals(1, mock.getInvocationCount("getDomain"));
    // Cache should contain a domain for this session
    assertEquals(1, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // Get the same domain from a different session
    // $NON-NLS-1$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", SESSION_ID2));
    d = repo.getDomain(ID);
    // Make sure we got a new, session-specific, domain
    assertEquals(SESSION_ID2, d.getDescription(TEST_LOCALE));
    // Make sure cache was missed and delegate was called
    // $NON-NLS-1$
    assertEquals(2, mock.getInvocationCount("getDomain"));
    // We should now have two objects in the cache (1 domain per session)
    assertEquals(2, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // Domains in current session
    assertEquals(1, repo.getDomainIds().size());
    // Switch back to original session
    // $NON-NLS-1$ //$NON-NLS-2$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", "1"));
    // Check for domains available for this session
    assertEquals(1, repo.getDomainIds().size());
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) SessionCachingMetadataDomainRepository(org.pentaho.platform.plugin.services.metadata.SessionCachingMetadataDomainRepository) Domain(org.pentaho.metadata.model.Domain)

Example 18 with StandaloneSession

use of org.pentaho.platform.engine.core.system.StandaloneSession in project pentaho-platform by pentaho.

the class SessionCachingMetadataDomainRepositoryIT method testRemoveModel.

public void testRemoveModel() 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);
    // $NON-NLS-1$ //$NON-NLS-2$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", "1"));
    Domain domain = getTestDomain(ID1);
    LogicalModel model = new LogicalModel();
    // $NON-NLS-1$
    model.setId("test");
    domain.addLogicalModel(model);
    repo.storeDomain(domain, false);
    repo.storeDomain(getTestDomain(ID2), false);
    repo.getDomain(ID1);
    assertEquals(1, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    repo.getDomain(ID2);
    assertEquals(2, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // $NON-NLS-1$ //$NON-NLS-2$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", "2"));
    repo.getDomain(ID1);
    assertEquals(3, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // $NON-NLS-1$
    repo.removeModel(ID1, "test");
    // Removing a model should remove all domains with that same id, leaving others intact
    assertEquals(1, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // $NON-NLS-1$
    assertEquals(1, mock.getInvocationCount("removeModel"));
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) SessionCachingMetadataDomainRepository(org.pentaho.platform.plugin.services.metadata.SessionCachingMetadataDomainRepository) Domain(org.pentaho.metadata.model.Domain)

Example 19 with StandaloneSession

use of org.pentaho.platform.engine.core.system.StandaloneSession in project pentaho-platform by pentaho.

the class SessionCachingMetadataDomainRepositoryIT method testGetDomain.

public void testGetDomain() throws Exception {
    // $NON-NLS-1$
    final String SESSION_ID = "1234-5678-90";
    // $NON-NLS-1$
    final String ID = "1";
    // $NON-NLS-1$
    final String ID2 = "2";
    IAclNodeHelper aclNodeHelper = mock(IAclNodeHelper.class);
    when(aclNodeHelper.canAccess(any(RepositoryFile.class), any(EnumSet.class))).thenReturn(true);
    MockAclAwareMetadataDomainRepository mock = new MockAclAwareMetadataDomainRepository(aclNodeHelper, null);
    mock.storeDomain(getTestDomain(ID), false);
    SessionCachingMetadataDomainRepository repo = new SessionCachingMetadataDomainRepository(mock);
    // $NON-NLS-1$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", SESSION_ID));
    // $NON-NLS-1$
    assertEquals(0, mock.getInvocationCount("getDomain"));
    Domain d = repo.getDomain(ID);
    assertEquals(ID, d.getId());
    // Make sure the domain we got back has our session embedded in it (tests mock repository)
    assertEquals(SESSION_ID, d.getDescription(TEST_LOCALE));
    // $NON-NLS-1$
    assertEquals(1, mock.getInvocationCount("getDomain"));
    // Cache should contain a domain for this session
    assertEquals(1, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    repo.getDomain(ID);
    // Make sure cache was hit and delegate was not called
    // $NON-NLS-1$
    assertEquals(1, mock.getInvocationCount("getDomain"));
    // Cache should contain a domain for this session
    assertEquals(1, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    mock.storeDomain(getTestDomain(ID2), false);
    repo.getDomain(ID2);
    // Cache should contain tow domains for this session
    assertEquals(2, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
    // Block access to domain ID2. Cache should be cleared for this domain
    when(aclNodeHelper.canAccess(any(RepositoryFile.class), any(EnumSet.class))).thenReturn(false);
    repo.getDomain(ID2);
    // Make sure cache was hit and delegate was not called
    // $NON-NLS-1$
    assertEquals(2, mock.getInvocationCount("getDomain"));
    // Cache should contain a domain for this session
    assertEquals(1, PentahoSystem.getCacheManager(null).getAllKeysFromRegionCache(CACHE_NAME).size());
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IAclNodeHelper(org.pentaho.platform.api.repository2.unified.IAclNodeHelper) EnumSet(java.util.EnumSet) SessionCachingMetadataDomainRepository(org.pentaho.platform.plugin.services.metadata.SessionCachingMetadataDomainRepository) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Domain(org.pentaho.metadata.model.Domain)

Example 20 with StandaloneSession

use of org.pentaho.platform.engine.core.system.StandaloneSession in project pentaho-platform by pentaho.

the class SessionCachingMetadataDomainRepositoryIT method testGetDomainIds_differentSessions.

/**
 * Make sure all domain ids are returned in all sessions
 */
public void testGetDomainIds_differentSessions() throws Exception {
    // $NON-NLS-1$
    final String ID = "1";
    MockSessionAwareMetadataDomainRepository mock = new MockSessionAwareMetadataDomainRepository();
    SessionCachingMetadataDomainRepository repo = new SessionCachingMetadataDomainRepository(mock);
    // $NON-NLS-1$ //$NON-NLS-2$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", "1"));
    Set<String> ids = repo.getDomainIds();
    assertEquals(0, ids.size());
    // $NON-NLS-1$
    assertEquals(1, mock.getInvocationCount("getDomainIds"));
    repo.storeDomain(getTestDomain(ID), false);
    ids = repo.getDomainIds();
    assertEquals(1, ids.size());
    // $NON-NLS-1$
    assertEquals(2, mock.getInvocationCount("getDomainIds"));
    // Switch the session
    // $NON-NLS-1$ //$NON-NLS-2$
    PentahoSessionHolder.setSession(new StandaloneSession("Standalone Session", "2"));
    // Make sure the domain id is returned
    ids = repo.getDomainIds();
    assertEquals(1, ids.size());
    // $NON-NLS-1$
    assertEquals(3, mock.getInvocationCount("getDomainIds"));
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) SessionCachingMetadataDomainRepository(org.pentaho.platform.plugin.services.metadata.SessionCachingMetadataDomainRepository)

Aggregations

StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)218 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)74 ArrayList (java.util.ArrayList)46 Authentication (org.springframework.security.core.Authentication)39 Test (org.junit.Test)38 OutputStream (java.io.OutputStream)34 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)30 GrantedAuthority (org.springframework.security.core.GrantedAuthority)30 User (org.springframework.security.core.userdetails.User)30 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)29 UserDetails (org.springframework.security.core.userdetails.UserDetails)29 SimpleOutputHandler (org.pentaho.platform.engine.core.output.SimpleOutputHandler)24 File (java.io.File)21 SimpleParameterProvider (org.pentaho.platform.engine.core.solution.SimpleParameterProvider)21 StandaloneObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneObjectFactory)21 StandaloneSpringPentahoObjectFactory (org.pentaho.platform.engine.core.system.objfac.StandaloneSpringPentahoObjectFactory)21 IPentahoUser (org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)20 SimpleUrlFactory (org.pentaho.platform.util.web.SimpleUrlFactory)20 HashMap (java.util.HashMap)16 Before (org.junit.Before)16