Search in sources :

Example 1 with IAnyUserSettingService

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

the class PentahoPlatformExporter method exportUsersAndRoles.

protected void exportUsersAndRoles() {
    log.debug("export users & roles");
    IUserRoleListService userRoleListService = PentahoSystem.get(IUserRoleListService.class);
    UserDetailsService userDetailsService = PentahoSystem.get(UserDetailsService.class);
    IRoleAuthorizationPolicyRoleBindingDao roleBindingDao = PentahoSystem.get(IRoleAuthorizationPolicyRoleBindingDao.class);
    ITenant tenant = TenantUtils.getCurrentTenant();
    // get the user settings for this user
    IUserSettingService service = getUserSettingService();
    // User Export
    List<String> userList = userRoleListService.getAllUsers(tenant);
    for (String user : userList) {
        UserExport userExport = new UserExport();
        userExport.setUsername(user);
        userExport.setPassword(userDetailsService.loadUserByUsername(user).getPassword());
        for (String role : userRoleListService.getRolesForUser(tenant, user)) {
            userExport.setRole(role);
        }
        if (service != null && service instanceof IAnyUserSettingService) {
            IAnyUserSettingService userSettings = (IAnyUserSettingService) service;
            List<IUserSetting> settings = userSettings.getUserSettings(user);
            if (settings != null) {
                for (IUserSetting setting : settings) {
                    userExport.addUserSetting(new ExportManifestUserSetting(setting));
                }
            }
        }
        this.getExportManifest().addUserExport(userExport);
    }
    // export the global user settings
    if (service != null) {
        List<IUserSetting> globalUserSettings = service.getGlobalUserSettings();
        if (globalUserSettings != null) {
            for (IUserSetting setting : globalUserSettings) {
                getExportManifest().addGlobalUserSetting(new ExportManifestUserSetting(setting));
            }
        }
    }
    // RoleExport
    List<String> roles = userRoleListService.getAllRoles();
    for (String role : roles) {
        RoleExport roleExport = new RoleExport();
        roleExport.setRolename(role);
        roleExport.setPermission(roleBindingDao.getRoleBindingStruct(null).bindingMap.get(role));
        exportManifest.addRoleExport(roleExport);
    }
}
Also used : IRoleAuthorizationPolicyRoleBindingDao(org.pentaho.platform.security.policy.rolebased.IRoleAuthorizationPolicyRoleBindingDao) ExportManifestUserSetting(org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting) IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting) IAnyUserSettingService(org.pentaho.platform.api.usersettings.IAnyUserSettingService) ITenant(org.pentaho.platform.api.mt.ITenant) UserExport(org.pentaho.platform.plugin.services.importexport.UserExport) IUserSettingService(org.pentaho.platform.api.usersettings.IUserSettingService) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService) RoleExport(org.pentaho.platform.plugin.services.importexport.RoleExport)

Example 2 with IAnyUserSettingService

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

the class SolutionImportHandlerTest method testImportUserSettings_NoOverwrite.

@Test
public void testImportUserSettings_NoOverwrite() throws Exception {
    UserExport user = new UserExport();
    user.setUsername("pentaho");
    user.addUserSetting(new ExportManifestUserSetting("theme", "crystal"));
    user.addUserSetting(new ExportManifestUserSetting("language", "en_US"));
    IAnyUserSettingService userSettingService = Mockito.mock(IAnyUserSettingService.class);
    PentahoSystem.registerObject(userSettingService);
    importHandler.setOverwriteFile(false);
    IUserSetting existingSetting = Mockito.mock(IUserSetting.class);
    Mockito.when(userSettingService.getUserSetting("pentaho", "theme", null)).thenReturn(existingSetting);
    Mockito.when(userSettingService.getUserSetting("pentaho", "language", null)).thenReturn(null);
    importHandler.importUserSettings(user);
    Mockito.verify(userSettingService, Mockito.never()).setUserSetting("pentaho", "theme", "crystal");
    Mockito.verify(userSettingService).setUserSetting("pentaho", "language", "en_US");
    Mockito.verify(userSettingService).getUserSetting("pentaho", "theme", null);
    Mockito.verify(userSettingService).getUserSetting("pentaho", "language", null);
}
Also used : IAnyUserSettingService(org.pentaho.platform.api.usersettings.IAnyUserSettingService) UserExport(org.pentaho.platform.plugin.services.importexport.UserExport) ExportManifestUserSetting(org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting) IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting) Test(org.junit.Test)

Example 3 with IAnyUserSettingService

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

the class SolutionImportHandlerTest method testImportUserSettings.

@Test
public void testImportUserSettings() throws Exception {
    UserExport user = new UserExport();
    user.setUsername("pentaho");
    user.addUserSetting(new ExportManifestUserSetting("theme", "crystal"));
    user.addUserSetting(new ExportManifestUserSetting("language", "en_US"));
    IAnyUserSettingService userSettingService = Mockito.mock(IAnyUserSettingService.class);
    PentahoSystem.registerObject(userSettingService);
    importHandler.setOverwriteFile(true);
    importHandler.importUserSettings(user);
    Mockito.verify(userSettingService).setUserSetting("pentaho", "theme", "crystal");
    Mockito.verify(userSettingService).setUserSetting("pentaho", "language", "en_US");
}
Also used : IAnyUserSettingService(org.pentaho.platform.api.usersettings.IAnyUserSettingService) UserExport(org.pentaho.platform.plugin.services.importexport.UserExport) ExportManifestUserSetting(org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting) Test(org.junit.Test)

Example 4 with IAnyUserSettingService

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

the class SolutionImportHandler method importUserSettings.

protected void importUserSettings(UserExport user) {
    IUserSettingService settingService = PentahoSystem.get(IUserSettingService.class);
    IAnyUserSettingService userSettingService = null;
    if (settingService != null && settingService instanceof IAnyUserSettingService) {
        userSettingService = (IAnyUserSettingService) settingService;
    }
    if (userSettingService != null) {
        List<ExportManifestUserSetting> exportedSettings = user.getUserSettings();
        try {
            for (ExportManifestUserSetting exportedSetting : exportedSettings) {
                if (isOverwriteFile()) {
                    userSettingService.setUserSetting(user.getUsername(), exportedSetting.getName(), exportedSetting.getValue());
                } else {
                    // see if it's there first before we set this setting
                    IUserSetting userSetting = userSettingService.getUserSetting(user.getUsername(), exportedSetting.getName(), null);
                    if (userSetting == null) {
                        // only set it if we didn't find that it exists already
                        userSettingService.setUserSetting(user.getUsername(), exportedSetting.getName(), exportedSetting.getValue());
                    }
                }
            }
        } catch (SecurityException e) {
            log.error(Messages.getInstance().getString("ERROR.ImportingUserSetting", user.getUsername()));
            log.debug(Messages.getInstance().getString("ERROR.ImportingUserSetting", user.getUsername()), e);
        }
    }
}
Also used : IAnyUserSettingService(org.pentaho.platform.api.usersettings.IAnyUserSettingService) IUserSettingService(org.pentaho.platform.api.usersettings.IUserSettingService) ExportManifestUserSetting(org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting) IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting)

Example 5 with IAnyUserSettingService

use of org.pentaho.platform.api.usersettings.IAnyUserSettingService 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)

Aggregations

IAnyUserSettingService (org.pentaho.platform.api.usersettings.IAnyUserSettingService)5 ExportManifestUserSetting (org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting)5 IUserSetting (org.pentaho.platform.api.usersettings.pojo.IUserSetting)4 UserExport (org.pentaho.platform.plugin.services.importexport.UserExport)4 Test (org.junit.Test)3 IUserRoleListService (org.pentaho.platform.api.engine.IUserRoleListService)2 ITenant (org.pentaho.platform.api.mt.ITenant)2 IUserSettingService (org.pentaho.platform.api.usersettings.IUserSettingService)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 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Mockito.anyString (org.mockito.Mockito.anyString)1 ExportManifest (org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest)1 RoleBindingStruct (org.pentaho.platform.security.policy.rolebased.RoleBindingStruct)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 User (org.springframework.security.core.userdetails.User)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1