Search in sources :

Example 1 with StandaloneSession

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

the class InheritDefaultAclHandlerTest method setUp.

@Before
public void setUp() {
    StandaloneSession pentahoSession = new StandaloneSession("test", "test");
    PentahoSessionHolder.setSession(pentahoSession);
    repositoryFile = mock(RepositoryFile.class);
    inheritDefaultAclHandler = new InheritDefaultAclHandler();
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Before(org.junit.Before)

Example 2 with StandaloneSession

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

the class RepositoryTestCase method setUp.

public void setUp() {
    super.setUp();
    // $NON-NLS-1$
    sess = new StandaloneSession(Messages.getInstance().getString("REPOSTEST.JUNIT_TEST_SESSION"));
    HibernateUtil.beginTransaction();
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession)

Example 3 with StandaloneSession

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

the class SecurityHelper method runAsSystem.

/**
 * Runs code as system with full privileges.
 */
public <T> T runAsSystem(final Callable<T> callable) throws Exception {
    String singleTenantAdmin = PentahoSystem.get(String.class, "singleTenantAdminUserName", null);
    IPentahoSession origSession = PentahoSessionHolder.getSession();
    Authentication origAuth = SecurityContextHolder.getContext().getAuthentication();
    StandaloneSession session = null;
    try {
        session = new StandaloneSession(singleTenantAdmin);
        session.setAuthenticated(singleTenantAdmin);
        // Set the session first or else the call to
        // createAuthentication will fail
        PentahoSessionHolder.setSession(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.
        SecurityContextHolder.clearContext();
        // Now create the authentication
        // $NON-NLS-1$
        Authentication auth = createAuthentication(singleTenantAdmin);
        SecurityContextHolder.getContext().setAuthentication(auth);
        // Invoke the delta.
        return callable.call();
    } finally {
        // Make sure to destroy the system session so we don't leak anything.
        if (session != null) {
            try {
                session.destroy();
            } catch (Exception e) {
                // We can safely ignore this.
                e.printStackTrace();
            }
        }
        // Reset the original session.
        PentahoSessionHolder.setSession(origSession);
        SecurityContextHolder.getContext().setAuthentication(origAuth);
    }
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Authentication(org.springframework.security.core.Authentication)

Example 4 with StandaloneSession

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

the class SecurityHelper method runAsAnonymous.

/**
 * Utility method that allows you to run a block of code as the given user. Regardless of success or exception
 * situation, the original session and authentication will be restored once your block of code is finished executing,
 * i.e. the given user will apply only to your {@link Callable}, then the system environment will return to the user
 * present prior to you calling this method.
 *
 * @param <T>      the return type of your operation, specify this type as <code>T</code>
 * @param callable {@link Callable#call()} contains the code you wish to run as the given user
 * @return the value returned by your implementation of {@link Callable#call()}
 * @throws Exception
 * @see {@link Callable}
 */
@Override
public <T> T runAsAnonymous(final Callable<T> callable) throws Exception {
    IPentahoSession origSession = PentahoSessionHolder.getSession();
    Authentication origAuth = SecurityContextHolder.getContext().getAuthentication();
    try {
        PentahoSessionHolder.setSession(new StandaloneSession());
        // get anonymous username/role defined in pentaho.xml
        String user = PentahoSystem.getSystemSetting("anonymous-authentication/anonymous-user", // $NON-NLS-1$//$NON-NLS-2$
        "anonymousUser");
        String role = PentahoSystem.getSystemSetting("anonymous-authentication/anonymous-role", // $NON-NLS-1$//$NON-NLS-2$
        "Anonymous");
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        authorities.add(new SimpleGrantedAuthority(role));
        Authentication auth = new AnonymousAuthenticationToken("anonymousUser", new User(user, "ignored", true, true, true, true, authorities), authorities);
        // 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.
        SecurityContextHolder.clearContext();
        SecurityContextHolder.getContext().setAuthentication(auth);
        return callable.call();
    } finally {
        PentahoSessionHolder.setSession(origSession);
        SecurityContextHolder.getContext().setAuthentication(origAuth);
    }
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Authentication(org.springframework.security.core.Authentication) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken)

Example 5 with StandaloneSession

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

the class SessionCachingMetadataDomainRepositoryTest method shouldUseDomainIdsCacheIfEnabled.

@Test
public void shouldUseDomainIdsCacheIfEnabled() throws Exception {
    MockSessionAwareMetadataDomainRepository mock = spy(new MockSessionAwareMetadataDomainRepository());
    PentahoSessionHolder.setSession(new StandaloneSession("session", "1"));
    SessionCachingMetadataDomainRepository repo = new SessionCachingMetadataDomainRepository(mock);
    Domain domain = new Domain();
    domain.setId("id");
    mock.setPersistedDomains(domain);
    ICacheManager manager = mock(ICacheManager.class);
    Set<String> ids = new HashSet<>(Arrays.asList("domainId1", "domainId2"));
    when(manager.getFromRegionCache("metadata-domain-repository", "domain-id-cache-for-session:1")).thenReturn(ids);
    repo.cacheManager = manager;
    repo.domainIdsCacheEnabled = true;
    Set<String> domainIds = repo.getDomainIds();
    assertEquals(ids, domainIds);
    verify(mock, times(0)).reloadDomains();
    verify(manager, times(1)).getFromRegionCache("metadata-domain-repository", "domain-id-cache-for-session:1");
}
Also used : StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) MockSessionAwareMetadataDomainRepository(org.pentaho.test.platform.plugin.services.metadata.MockSessionAwareMetadataDomainRepository) ICacheManager(org.pentaho.platform.api.engine.ICacheManager) Domain(org.pentaho.metadata.model.Domain) HashSet(java.util.HashSet) Test(org.junit.Test)

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