Search in sources :

Example 1 with ISystemSettings

use of org.pentaho.platform.api.engine.ISystemSettings in project pentaho-platform by pentaho.

the class PentahoSystem method runAsSystem.

/**
 * Runs code as system with full privileges.
 * <p/>
 * <p>
 * Unfortunate copy and paste from SecurityHelper due to dependencies.
 * </p>
 */
private static <T> T runAsSystem(final Callable<T> callable) throws Exception {
    final String name = StringUtils.defaultIfEmpty(PentahoSystem.get(String.class, "singleTenantAdminUserName", null), "admin");
    IPentahoSession origSession = PentahoSessionHolder.getSession();
    SecurityContext originalContext = SecurityContextHolder.getContext();
    try {
        // create pentaho session
        StandaloneSession session = new StandaloneSession(name);
        session.setAuthenticated(name);
        // create authentication
        List<GrantedAuthority> roles;
        ISystemSettings settings = PentahoSystem.getSystemSettings();
        String roleName = (settings != null) ? settings.getSystemSetting("acl-voter/admin-role", "Admin") : "Admin";
        roles = new ArrayList<GrantedAuthority>();
        roles.add(new SimpleGrantedAuthority(roleName));
        User user = new User(name, "", true, true, true, true, roles);
        // $NON-NLS-1$
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user, "", roles);
        // set holders
        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();
        SecurityContextHolder.getContext().setAuthentication(auth);
        return callable.call();
    } finally {
        IPentahoSession sessionToDestroy = PentahoSessionHolder.getSession();
        if (sessionToDestroy != null && sessionToDestroy != origSession) {
            try {
                sessionToDestroy.destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PentahoSessionHolder.setSession(origSession);
        SecurityContextHolder.setContext(originalContext);
    }
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SecurityContext(org.springframework.security.core.context.SecurityContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) ISystemSettings(org.pentaho.platform.api.engine.ISystemSettings) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException) PentahoSystemException(org.pentaho.platform.api.engine.PentahoSystemException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 2 with ISystemSettings

use of org.pentaho.platform.api.engine.ISystemSettings in project pentaho-platform by pentaho.

the class AbstractPentahoAclVoter method init.

public void init(final IPentahoSession session) {
    ISystemSettings settings = PentahoSystem.getSystemSettings();
    // $NON-NLS-1$ //$NON-NLS-2$
    String roleName = settings.getSystemSetting("acl-voter/admin-role", "Administrator");
    adminRole = new SimpleGrantedAuthority(roleName);
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ISystemSettings(org.pentaho.platform.api.engine.ISystemSettings)

Example 3 with ISystemSettings

use of org.pentaho.platform.api.engine.ISystemSettings in project pentaho-platform by pentaho.

the class XActionUtilTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() throws ObjectFactoryException {
    MockitoAnnotations.initMocks(this);
    Map<String, String[]> map = mock(Map.class);
    when(httpServletRequest.getParameterMap()).thenReturn(map);
    when(httpServletRequest.getParameter(anyString())).thenReturn(null);
    when(repository.getFile(anyString())).thenReturn(generatedFile);
    List<IContentItem> items = Arrays.asList(mock(RepositoryFileContentItem.class));
    IRuntimeContext context = mock(IRuntimeContext.class);
    when(context.getOutputContentItems()).thenReturn(items);
    when(engine.execute(anyString(), anyString(), anyBoolean(), anyBoolean(), anyString(), anyBoolean(), anyMap(), any(IOutputHandler.class), any(IActionCompleteListener.class), any(IPentahoUrlFactory.class), anyList())).thenReturn(context);
    pentahoObjectFactoryUnified = mock(IPentahoObjectFactory.class);
    when(pentahoObjectFactoryUnified.objectDefined(anyString())).thenReturn(true);
    when(pentahoObjectFactoryUnified.get(this.anyClass(), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            if (IUnifiedRepository.class.toString().equals(invocation.getArguments()[0].toString())) {
                return repository;
            } else if (ISolutionEngine.class.toString().equals(invocation.getArguments()[0].toString())) {
                return engine;
            } else if (IMessageFormatter.class.toString().equals(invocation.getArguments()[0].toString())) {
                return formatter;
            } else if (IAuthorizationPolicy.class.toString().equals(invocation.getArguments()[0].toString())) {
                return authPolicy;
            } else {
                return null;
            }
        }
    });
    PentahoSystem.registerObjectFactory(pentahoObjectFactoryUnified);
    ISystemSettings systemSettingsService = mock(ISystemSettings.class);
    when(systemSettingsService.getSystemSetting(anyString(), anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0].toString();
        }
    });
    PentahoSystem.setSystemSettingsService(systemSettingsService);
    PentahoSessionHolder.setStrategyName(PentahoSessionHolder.MODE_INHERITABLETHREADLOCAL);
    PentahoSessionHolder.setSession(userSession);
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IAuthorizationPolicy(org.pentaho.platform.api.engine.IAuthorizationPolicy) IPentahoUrlFactory(org.pentaho.platform.api.engine.IPentahoUrlFactory) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) RepositoryFileContentItem(org.pentaho.platform.repository2.unified.fileio.RepositoryFileContentItem) ISystemSettings(org.pentaho.platform.api.engine.ISystemSettings) IOutputHandler(org.pentaho.platform.api.engine.IOutputHandler) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IContentItem(org.pentaho.platform.api.repository.IContentItem) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext) IActionCompleteListener(org.pentaho.platform.api.engine.IActionCompleteListener) Before(org.junit.Before)

Example 4 with ISystemSettings

use of org.pentaho.platform.api.engine.ISystemSettings in project pentaho-platform by pentaho.

the class ChartBeansSystemListenerIT method setUp.

/**
 * @throws java.lang.Exception
 */
@SuppressWarnings("deprecation")
@Before
public void setUp() throws Exception {
    String solutionsRelativePath = TestResourceLocation.TEST_RESOURCES + "/org/pentaho/test/platform/plugin/chartbeans/solutions";
    MicroPlatform mp = new MicroPlatform(solutionsRelativePath);
    mp.define(ISolutionEngine.class, Object.class);
    ISystemSettings settings = new XmlSimpleSystemSettings();
    mp.setSettingsProvider(settings);
    mp.init();
}
Also used : MicroPlatform(org.pentaho.test.platform.engine.core.MicroPlatform) ISystemSettings(org.pentaho.platform.api.engine.ISystemSettings) XmlSimpleSystemSettings(org.pentaho.test.platform.engine.core.XmlSimpleSystemSettings) Before(org.junit.Before)

Example 5 with ISystemSettings

use of org.pentaho.platform.api.engine.ISystemSettings in project pentaho-platform by pentaho.

the class HttpSessionPentahoSessionIntegrationFilterTest method testSessionCookieDisabledInSettings.

@Test
public void testSessionCookieDisabledInSettings() {
    final ISystemSettings systemSettings = PentahoSystem.getSystemSettings();
    try {
        final ISystemSettings mockSettings = Mockito.mock(ISystemSettings.class);
        Mockito.when(mockSettings.getSystemSetting("session-expired-dialog", "true")).thenReturn("false");
        PentahoSystem.setSystemSettingsService(mockSettings);
        new HttpSessionPentahoSessionIntegrationFilter().setSessionExpirationCookies(httpSession, pentahoSession, servletResponse);
        Mockito.verify(servletResponse, Mockito.never()).addCookie(Mockito.any());
    } finally {
        PentahoSystem.setSystemSettingsService(systemSettings);
    }
}
Also used : ISystemSettings(org.pentaho.platform.api.engine.ISystemSettings) Test(org.junit.Test)

Aggregations

ISystemSettings (org.pentaho.platform.api.engine.ISystemSettings)21 Test (org.junit.Test)13 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)4 Before (org.junit.Before)3 File (java.io.File)2 Matchers.anyString (org.mockito.Matchers.anyString)2 IPentahoObjectFactory (org.pentaho.platform.api.engine.IPentahoObjectFactory)2 ISolutionEngine (org.pentaho.platform.api.engine.ISolutionEngine)2 ObjectFactoryException (org.pentaho.platform.api.engine.ObjectFactoryException)2 DatasourceDTO (org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO)2 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Node (org.dom4j.Node)1 Expectations (org.jmock.Expectations)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 IActionCompleteListener (org.pentaho.platform.api.engine.IActionCompleteListener)1