@Test
  public void testDeleteUserSettingsByName() throws Exception {
    IAuthorizationPolicy policy = mock(IAuthorizationPolicy.class);
    when(policy.isAllowed(anyString())).thenReturn(true);
    PentahoSystem.registerObject(policy);

    final RepositoryFile repositoryFile = mock(RepositoryFile.class);
    when(repositoryFile.getId()).thenReturn(USER_FOLDER_ID);
    when(repository.getFile(anyString())).thenReturn(repositoryFile);

    doAnswer(
            new Answer() {
              @Override
              public Object answer(InvocationOnMock invocation) throws Throwable {

                final Map<String, Serializable> settings =
                    (Map<String, Serializable>) invocation.getArguments()[1];

                assertNotNull(settings);
                assertEquals(2, settings.size());

                final Iterator<String> iterator = settings.keySet().iterator();
                assertFalse(iterator.next().startsWith(UserSettingService.SETTING_PREFIX));
                assertFalse(iterator.next().startsWith(UserSettingService.SETTING_PREFIX));

                return null;
              }
            })
        .when(repository)
        .setFileMetadata(eq(USER_FOLDER_ID), anyMap());

    userSettingService.deleteUserSettings("test");
  }
 protected void validateEtcReadAccess(String path) {
   IAuthorizationPolicy policy = PentahoSystem.get(IAuthorizationPolicy.class);
   boolean isAdmin = policy.isAllowed(AdministerSecurityAction.NAME);
   if (!isAdmin && path.startsWith("/etc")) {
     throw new RuntimeException("This user is not allowed to access the ETC folder in JCR.");
   }
 }
 @Override
 protected boolean isAdmin() {
   IAuthorizationPolicy policy = PentahoSystem.get(IAuthorizationPolicy.class);
   if (policy == null) {
     throw new IllegalStateException(
         Messages.getInstance()
             .getString(
                 "AuthorizationPolicyBasedUserRoleWebService.ERROR_0001_MISSING_AUTHZ_POLICY")); //$NON-NLS-1$
   }
   return policy.isAllowed(AdministerSecurityAction.NAME); // $NON-NLS-1$
 }
  @Test
  public void testGetUserSettingByName() throws Exception {
    final String settingName = USER_SETTING_NAME_3;
    final String defaultValue = "defaultValue";

    IAuthorizationPolicy policy = mock(IAuthorizationPolicy.class);
    when(policy.isAllowed(anyString())).thenReturn(true);
    PentahoSystem.registerObject(policy);

    // try to get existing setting
    final IUserSetting userSetting =
        userSettingService.getUserSetting("test", settingName, defaultValue);
    assertEquals(settingName, userSetting.getSettingName());
    assertEquals(USER_SETTING_VALUE_3, userSetting.getSettingValue());
  }
  @Test
  public void testSetUserSettingByName() throws Exception {
    final String settingName = "settingName";
    final String settingValue = "settingValue";

    IAuthorizationPolicy policy = mock(IAuthorizationPolicy.class);
    when(policy.isAllowed(anyString())).thenReturn(true);
    PentahoSystem.registerObject(policy);

    assertEquals(4, userSettings.size());
    userSettingService.setUserSetting("test", settingName, settingValue);
    assertEquals(5, userSettings.size());

    final Serializable value = userSettings.get(UserSettingService.SETTING_PREFIX + settingName);
    assertEquals(settingValue, value);

    verify(repository).setFileMetadata(eq(USER_FOLDER_ID), anyMap());
  }
  @Test
  public void testGetUserSettingsByName() throws Exception {
    final List<IUserSetting> settings = userSettingService.getUserSettings("test");

    IAuthorizationPolicy policy = mock(IAuthorizationPolicy.class);
    when(policy.isAllowed(anyString())).thenReturn(true);
    PentahoSystem.registerObject(policy);

    assertNotNull(settings);
    assertEquals(2, settings.size());
    for (IUserSetting setting : settings) {
      if (COMMON_SETTING_NAME.equals(setting.getSettingName())) {
        assertEquals(COMMON_USER_SETTING_VALUE, setting.getSettingValue());
      } else if (USER_SETTING_NAME_3.equals(setting.getSettingName())) {
        assertEquals(USER_SETTING_VALUE_3, setting.getSettingValue());
      } else if (GLOBAL_SETTING_NAME_3.equals(setting.getSettingName())) {
        assertEquals(GLOBAL_SETTING_VALUE_3, setting.getSettingValue());
      }
    }
  }
  public RepositoryFileTreeDto getTree(
      final String path, final int depth, final String filter, final boolean showHidden) {
    RepositoryFileTree tree = repo.getTree(path, depth, filter, showHidden);

    // Filter system folders from non-admin users.
    // PDI uses this web-service and system folders must be returned to admin repository database
    // connections.
    List<RepositoryFileTree> files = new ArrayList<RepositoryFileTree>();
    IAuthorizationPolicy policy = PentahoSystem.get(IAuthorizationPolicy.class);
    boolean isAdmin = policy.isAllowed(AdministerSecurityAction.NAME);
    for (RepositoryFileTree file : tree.getChildren()) {
      Map<String, Serializable> fileMeta = repo.getFileMetadata(file.getFile().getId());
      boolean isSystemFolder =
          fileMeta.containsKey(IUnifiedRepository.SYSTEM_FOLDER)
              ? (Boolean) fileMeta.get(IUnifiedRepository.SYSTEM_FOLDER)
              : false;
      if (!isAdmin && isSystemFolder) {
        continue;
      }
      files.add(file);
    }
    tree = new RepositoryFileTree(tree.getFile(), files);
    return tree != null ? repositoryFileTreeAdapter.marshal(tree) : null;
  }
  @BeforeClass
  public static void setUp() throws Exception {
    System.setProperty(
        "org.osjava.sj.root", "test-res/solution1/system/simple-jndi"); // $NON-NLS-1$ //$NON-NLS-2$
    mp = new MicroPlatform("test-res/solution1");

    IAuthorizationPolicy mockAuthorizationPolicy = mock(IAuthorizationPolicy.class);
    when(mockAuthorizationPolicy.isAllowed(anyString())).thenReturn(true);

    IUserRoleListService mockUserRoleListService = mock(IUserRoleListService.class);

    IDataAccessPermissionHandler mockDataAccessPermHandler =
        mock(IDataAccessPermissionHandler.class);
    when(mockDataAccessPermHandler.hasDataAccessPermission(any(IPentahoSession.class)))
        .thenReturn(true);

    mp.define(
        ISolutionEngine.class, SolutionEngine.class, IPentahoDefinableObjectFactory.Scope.GLOBAL);
    mp.define(
        IUnifiedRepository.class,
        TestFileSystemBackedUnifiedRepository.class,
        IPentahoDefinableObjectFactory.Scope.GLOBAL);
    mp.define(
        IMondrianCatalogService.class,
        MondrianCatalogHelper.class,
        IPentahoDefinableObjectFactory.Scope.GLOBAL);
    mp.define("connection-SQL", SQLConnection.class);
    mp.define("connection-MDX", MDXConnection.class);
    mp.define("connection-MDXOlap4j", MDXOlap4jConnection.class);
    mp.define(
        IDBDatasourceService.class,
        JndiDatasourceService.class,
        IPentahoDefinableObjectFactory.Scope.GLOBAL);
    mp.define(
        MDXConnection.MDX_CONNECTION_MAPPER_KEY,
        MondrianOneToOneUserRoleListMapper.class,
        IPentahoDefinableObjectFactory.Scope.GLOBAL);
    mp.define(IDatasourceMgmtService.class, MockDatasourceMgmtService.class);
    mp.define(IClientRepositoryPathsStrategy.class, MockClientRepositoryPathsStrategy.class);
    mp.define(ISecurityHelper.class, MockSecurityHelper.class);
    mp.define(UserDetailsService.class, MockUserDetailService.class);
    mp.define("singleTenantAdminUserName", "admin");
    mp.defineInstance(IMetadataDomainRepository.class, createMetadataDomainRepository());
    mp.defineInstance(IAuthorizationPolicy.class, mockAuthorizationPolicy);
    mp.defineInstance(
        IPluginResourceLoader.class,
        new PluginResourceLoader() {
          protected PluginClassLoader getOverrideClassloader() {
            return new PluginClassLoader(
                new File(".", "test-res/solution1/system/simple-jndi"), this);
          }
        });
    mp.defineInstance(IUserRoleListService.class, mockUserRoleListService);
    mp.defineInstance(IDataAccessPermissionHandler.class, mockDataAccessPermHandler);

    mp.setSettingsProvider(new SystemSettings());
    mp.start();

    PentahoSessionHolder.setStrategyName(PentahoSessionHolder.MODE_GLOBAL);
    SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_GLOBAL);
  }
 private boolean canAdminister() {
   IAuthorizationPolicy policy = PentahoSystem.get(IAuthorizationPolicy.class);
   return policy.isAllowed(RepositoryReadAction.NAME)
       && policy.isAllowed(RepositoryCreateAction.NAME)
       && (policy.isAllowed(AdministerSecurityAction.NAME));
 }