Search in sources :

Example 16 with IUserSetting

use of org.pentaho.platform.api.usersettings.pojo.IUserSetting in project pentaho-platform by pentaho.

the class PentahoPlatformExporterTest method testExportUsersAndRoles.

@Test
public void testExportUsersAndRoles() {
    IUserRoleListService mockDao = mock(IUserRoleListService.class);
    IAnyUserSettingService userSettingService = mock(IAnyUserSettingService.class);
    UserDetailsService userDetailsService = mock(UserDetailsService.class);
    PentahoSystem.registerObject(mockDao);
    PentahoSystem.registerObject(userSettingService);
    PentahoSystem.registerObject(userDetailsService);
    IRoleAuthorizationPolicyRoleBindingDao roleBindingDao = mock(IRoleAuthorizationPolicyRoleBindingDao.class);
    PentahoSystem.registerObject(roleBindingDao);
    String tenantPath = "path";
    when(session.getAttribute(IPentahoSession.TENANT_ID_KEY)).thenReturn(tenantPath);
    List<String> userList = new ArrayList<String>();
    String user = "testUser";
    String role = "testRole";
    userList.add(user);
    when(mockDao.getAllUsers(any(ITenant.class))).thenReturn(userList);
    List<String> roleList = new ArrayList<String>();
    roleList.add(role);
    when(mockDao.getAllRoles()).thenReturn(roleList);
    Map<String, List<String>> map = new HashMap<String, List<String>>();
    List<String> permissions = new ArrayList<String>();
    permissions.add("read");
    map.put("testRole", permissions);
    RoleBindingStruct struct = mock(RoleBindingStruct.class);
    struct.bindingMap = map;
    when(roleBindingDao.getRoleBindingStruct(anyString())).thenReturn(struct);
    ArgumentCaptor<UserExport> userCaptor = ArgumentCaptor.forClass(UserExport.class);
    ArgumentCaptor<RoleExport> roleCaptor = ArgumentCaptor.forClass(RoleExport.class);
    ExportManifest manifest = mock(ExportManifest.class);
    exporter.setExportManifest(manifest);
    List<IUserSetting> settings = new ArrayList<>();
    IUserSetting setting = mock(IUserSetting.class);
    settings.add(setting);
    when(userSettingService.getUserSettings(user)).thenReturn(settings);
    when(userSettingService.getGlobalUserSettings()).thenReturn(settings);
    List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
    UserDetails userDetails = new User("testUser", "testPassword", true, true, true, true, authList);
    when(userDetailsService.loadUserByUsername(anyString())).thenReturn(userDetails);
    exporter.exportUsersAndRoles();
    verify(manifest).addUserExport(userCaptor.capture());
    verify(manifest).addRoleExport(roleCaptor.capture());
    verify(userSettingService).getGlobalUserSettings();
    verify(manifest).addGlobalUserSetting(any(ExportManifestUserSetting.class));
    assertEquals(settings.size(), userCaptor.getValue().getUserSettings().size());
    UserExport userExport = userCaptor.getValue();
    assertEquals("testUser", userExport.getUsername());
    RoleExport roleExport = roleCaptor.getValue();
    assertEquals("testRole", roleExport.getRolename());
}
Also used : IRoleAuthorizationPolicyRoleBindingDao(org.pentaho.platform.security.policy.rolebased.IRoleAuthorizationPolicyRoleBindingDao) User(org.springframework.security.core.userdetails.User) HashMap(java.util.HashMap) RoleBindingStruct(org.pentaho.platform.security.policy.rolebased.RoleBindingStruct) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) ExportManifestUserSetting(org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting) Mockito.anyString(org.mockito.Mockito.anyString) ExportManifest(org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest) IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting) IAnyUserSettingService(org.pentaho.platform.api.usersettings.IAnyUserSettingService) ITenant(org.pentaho.platform.api.mt.ITenant) UserDetails(org.springframework.security.core.userdetails.UserDetails) UserExport(org.pentaho.platform.plugin.services.importexport.UserExport) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) List(java.util.List) ArrayList(java.util.ArrayList) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService) RoleExport(org.pentaho.platform.plugin.services.importexport.RoleExport) Test(org.junit.Test)

Example 17 with IUserSetting

use of org.pentaho.platform.api.usersettings.pojo.IUserSetting in project pentaho-platform by pentaho.

the class UserSettingService method getUserSettings.

public List<IUserSetting> getUserSettings() {
    // get the global settings and the user settings
    // merge unseen global settings into the user settings list
    List<IUserSetting> userSettings = new ArrayList<IUserSetting>();
    String tentantHomePath = ClientRepositoryPaths.getEtcFolderPath();
    Serializable tenantHomeId = repository.getFile(tentantHomePath).getId();
    Map<String, Serializable> tenantMetadata = repository.getFileMetadata(tenantHomeId);
    for (Map.Entry<String, Serializable> entry : tenantMetadata.entrySet()) {
        String key = entry.getKey();
        if (key.startsWith(SETTING_PREFIX)) {
            UserSetting setting = createSetting(key.substring(SETTING_PREFIX.length()), entry.getValue());
            userSettings.add(setting);
        }
    }
    String homePath = ClientRepositoryPaths.getUserHomeFolderPath(PentahoSessionHolder.getSession().getName());
    Serializable userHomeId = repository.getFile(homePath).getId();
    Map<String, Serializable> userMetadata = repository.getFileMetadata(userHomeId);
    for (Map.Entry<String, Serializable> entry : userMetadata.entrySet()) {
        String key = entry.getKey();
        if (key.startsWith(SETTING_PREFIX)) {
            UserSetting setting = createSetting(key.substring(SETTING_PREFIX.length()), entry.getValue());
            // see if a global setting exists which will be overridden
            if (userSettings.contains(setting)) {
                userSettings.remove(setting);
            }
            userSettings.add(setting);
        }
    }
    return userSettings;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting) IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting) UserSetting(org.pentaho.platform.repository.usersettings.pojo.UserSetting)

Example 18 with IUserSetting

use of org.pentaho.platform.api.usersettings.pojo.IUserSetting in project pentaho-platform by pentaho.

the class UserSettingServiceTest method testGetUserSettingUnset.

@Test
public void testGetUserSettingUnset() throws Exception {
    final String settingName = "settingName";
    final String defaultValue = "defaultValue";
    when(session.getAttribute(eq("SPRING_SECURITY_CONTEXT"))).thenReturn(1);
    final IUserSetting userSetting = userSettingService.getUserSetting(settingName, defaultValue);
    assertEquals(settingName, userSetting.getSettingName());
    assertEquals(defaultValue, userSetting.getSettingValue());
}
Also used : IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting) Test(org.junit.Test)

Example 19 with IUserSetting

use of org.pentaho.platform.api.usersettings.pojo.IUserSetting in project pentaho-platform by pentaho.

the class UserSettingServiceTest method testGetUserSettingsByName.

@Test
public void testGetUserSettingsByName() throws Exception {
    try {
        // autoboxing
        ((UserSettingServiceForTesting) userSettingService).setCanAdministerOverrideValue(true);
        final List<IUserSetting> settings = userSettingService.getUserSettings("test");
        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());
            }
        }
    } finally {
        ((UserSettingServiceForTesting) userSettingService).setCanAdministerOverrideValue(null);
    }
}
Also used : IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting) Test(org.junit.Test)

Example 20 with IUserSetting

use of org.pentaho.platform.api.usersettings.pojo.IUserSetting in project pentaho-platform by pentaho.

the class UserSettingServiceTest method testGetUserSetting.

@Test
public void testGetUserSetting() throws Exception {
    final String settingName = USER_SETTING_NAME_3;
    final String defaultValue = "defaultValue";
    when(session.getAttribute(eq("SPRING_SECURITY_CONTEXT"))).thenReturn(1);
    // try to get existing setting
    final IUserSetting userSetting = userSettingService.getUserSetting(settingName, defaultValue);
    assertEquals(settingName, userSetting.getSettingName());
    assertEquals(USER_SETTING_VALUE_3, userSetting.getSettingValue());
}
Also used : IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting) Test(org.junit.Test)

Aggregations

IUserSetting (org.pentaho.platform.api.usersettings.pojo.IUserSetting)21 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)8 IUserSettingService (org.pentaho.platform.api.usersettings.IUserSettingService)6 ExportManifestUserSetting (org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting)5 HashMap (java.util.HashMap)4 IAnyUserSettingService (org.pentaho.platform.api.usersettings.IAnyUserSettingService)4 Serializable (java.io.Serializable)3 Map (java.util.Map)3 UserExport (org.pentaho.platform.plugin.services.importexport.UserExport)3 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Facet (org.codehaus.enunciate.Facet)2 IUserRoleListService (org.pentaho.platform.api.engine.IUserRoleListService)2 ITenant (org.pentaho.platform.api.mt.ITenant)2 RoleExport (org.pentaho.platform.plugin.services.importexport.RoleExport)2 IRoleAuthorizationPolicyRoleBindingDao (org.pentaho.platform.security.policy.rolebased.IRoleAuthorizationPolicyRoleBindingDao)2 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)2 IOException (java.io.IOException)1 List (java.util.List)1