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();
}
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();
}
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);
}
}
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);
}
}
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");
}
Aggregations