Search in sources :

Example 1 with UserExport

use of org.pentaho.platform.plugin.services.importexport.UserExport 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 UserExport

use of org.pentaho.platform.plugin.services.importexport.UserExport in project pentaho-platform by pentaho.

the class SolutionImportHandler method importUsers.

/**
 * Imports UserExport objects into the platform as users.
 *
 * @param users
 * @return A map of role names to list of users in that role
 */
protected Map<String, List<String>> importUsers(List<UserExport> users) {
    Map<String, List<String>> roleToUserMap = new HashMap<>();
    IUserRoleDao roleDao = PentahoSystem.get(IUserRoleDao.class);
    ITenant tenant = new Tenant("/pentaho/" + TenantUtils.getDefaultTenant(), true);
    if (users != null && roleDao != null) {
        for (UserExport user : users) {
            String password = user.getPassword();
            log.debug("Importing user: " + user.getUsername());
            // map the user to the roles he/she is in
            for (String role : user.getRoles()) {
                List<String> userList;
                if (!roleToUserMap.containsKey(role)) {
                    userList = new ArrayList<>();
                    roleToUserMap.put(role, userList);
                } else {
                    userList = roleToUserMap.get(role);
                }
                userList.add(user.getUsername());
            }
            String[] userRoles = user.getRoles().toArray(new String[] {});
            try {
                roleDao.createUser(tenant, user.getUsername(), password, null, userRoles);
            } catch (AlreadyExistsException e) {
                // it's ok if the user already exists, it is probably a default user
                log.info(Messages.getInstance().getString("USER.Already.Exists", user.getUsername()));
                try {
                    if (isOverwriteFile()) {
                        // set the roles, maybe they changed
                        roleDao.setUserRoles(tenant, user.getUsername(), userRoles);
                        // set the password just in case it changed
                        roleDao.setPassword(tenant, user.getUsername(), password);
                    }
                } catch (Exception ex) {
                    // couldn't set the roles or password either
                    log.debug("Failed to set roles or password for existing user on import", ex);
                }
            } catch (Exception e) {
                log.error(Messages.getInstance().getString("ERROR.CreatingUser", user.getUsername()));
            }
            importUserSettings(user);
        }
    }
    return roleToUserMap;
}
Also used : AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) HashMap(java.util.HashMap) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) ITenant(org.pentaho.platform.api.mt.ITenant) Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) UserExport(org.pentaho.platform.plugin.services.importexport.UserExport) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with UserExport

use of org.pentaho.platform.plugin.services.importexport.UserExport in project pentaho-platform by pentaho.

the class SolutionImportHandlerTest method testImportUsers_userAlreadyExists.

@Test
public void testImportUsers_userAlreadyExists() throws Exception {
    List<UserExport> users = new ArrayList<>();
    UserExport user = new UserExport();
    user.setUsername("scrum master");
    user.setRole("coder");
    user.setPassword("password");
    users.add(user);
    String[] strings = {};
    Mockito.when(userRoleDao.createUser(Mockito.any(ITenant.class), Mockito.eq("scrum master"), Mockito.anyString(), Mockito.anyString(), Mockito.any(strings.getClass()))).thenThrow(new AlreadyExistsException("already there"));
    importHandler.setOverwriteFile(true);
    Map<String, List<String>> rolesToUsers = importHandler.importUsers(users);
    Assert.assertEquals(1, rolesToUsers.size());
    Assert.assertEquals("scrum master", rolesToUsers.get("coder").get(0));
    Mockito.verify(userRoleDao).createUser(Mockito.any(ITenant.class), Mockito.eq("scrum master"), Mockito.anyString(), Mockito.anyString(), Mockito.any(strings.getClass()));
    // should set the password or roles explicitly if the createUser failed
    Mockito.verify(userRoleDao).setUserRoles(Mockito.any(ITenant.class), Mockito.anyString(), Mockito.any(strings.getClass()));
    Mockito.verify(userRoleDao).setPassword(Mockito.any(ITenant.class), Mockito.anyString(), Mockito.anyString());
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) UserExport(org.pentaho.platform.plugin.services.importexport.UserExport) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 4 with UserExport

use of org.pentaho.platform.plugin.services.importexport.UserExport 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 5 with UserExport

use of org.pentaho.platform.plugin.services.importexport.UserExport 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)

Aggregations

UserExport (org.pentaho.platform.plugin.services.importexport.UserExport)9 Test (org.junit.Test)7 ITenant (org.pentaho.platform.api.mt.ITenant)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 IAnyUserSettingService (org.pentaho.platform.api.usersettings.IAnyUserSettingService)4 ExportManifestUserSetting (org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting)4 AlreadyExistsException (org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException)3 IUserSetting (org.pentaho.platform.api.usersettings.pojo.IUserSetting)3 HashMap (java.util.HashMap)2 IUserRoleListService (org.pentaho.platform.api.engine.IUserRoleListService)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 Mockito.anyString (org.mockito.Mockito.anyString)1 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)1 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)1 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)1 IUserRoleDao (org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao)1