SecurityHelper implements ISecurityHelper { 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 ); PentahoSessionHolder.setSession( session ); SecurityContextHolder.clearContext(); Authentication auth = createAuthentication( singleTenantAdmin ); SecurityContextHolder.getContext().setAuthentication( auth ); return callable.call(); } finally { if ( session != null ) { try { session.destroy(); } catch ( Exception e ) { e.printStackTrace(); } } PentahoSessionHolder.setSession( origSession ); SecurityContextHolder.getContext().setAuthentication( origAuth ); } } protected  SecurityHelper(); static ISecurityHelper getInstance(); static void setMockInstance( ISecurityHelper mockInstanceValue ); @Override void becomeUser( final String principalName ); @Override void becomeUser( final String principalName, final IParameterProvider paramProvider ); @Override T runAsUser( final String principalName, final Callable<T> callable ); @Override T runAsUser( final String principalName, final IParameterProvider paramProvider, final Callable<T> callable ); @Override T runAsAnonymous( final Callable<T> callable ); @Override @Deprecated boolean isPentahoAdministrator( final IPentahoSession session ); @Override boolean isGranted( final IPentahoSession session, final GrantedAuthority role ); @Deprecated boolean hasAccess( IAclHolder aHolder, int actionOperation, IPentahoSession session ); @Deprecated IAclVoter getAclVoter(); @Override Authentication createAuthentication( String principalName ); @Override Authentication getAuthentication(); @Override Authentication getAuthentication( IPentahoSession ignoredSession, boolean ignoredAllowAnonymous ); T runAsSystem( final Callable<T> callable ); IAuthorizationPolicy getAuthorizationPolicy(); ITenantedPrincipleNameResolver getTenantedUserNameUtils(); UserDetailsService getUserDetailsService(); IUserRoleListService getUserRoleListService();  }
@Test @SuppressWarnings( "unchecked" ) public void runAsSystemTest() throws Exception { PentahoSystemBoot boot = new PentahoSystemBoot(); boot.setFilePath( "test-src/solution" ); IPentahoObjectFactory pentahoObjectFactory = mock( IPentahoObjectFactory.class, PENTAHO_OBJECT_FACTORY_MOCK_NAME ); when( pentahoObjectFactory.objectDefined( eq( SINGLE_TENANT_ADMIN_USER_NAME ) ) ).thenReturn( true ); when( pentahoObjectFactory.get( eq( String.class ), eq( SINGLE_TENANT_ADMIN_USER_NAME ), Matchers.<IPentahoSession>any() ) ).thenReturn( ADMIN_USER_NAME ); when( pentahoObjectFactory.getName() ).thenReturn( PENTAHO_OBJECT_FACTORY_MOCK_NAME ); boot.setFactory( pentahoObjectFactory ); IUserRoleListService mockUserRoleListService = getUserRoleListServiceMock( ADMIN_USER_NAME, ADMIN_ROLES_ARRAY ); doReturn( mockUserRoleListService ).when( emptySecurityHelper ).getUserRoleListService(); Callable<String> callable = (Callable<String>) mock( Callable.class ); when( callable.call() ).thenReturn( CALLABLE_RETURNED_VALUE_OK ); String runningResult = emptySecurityHelper.runAsSystem( callable ); assertEquals( CALLABLE_RETURNED_VALUE_OK, runningResult ); }