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);
}
}
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;
}
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());
}
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);
}
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");
}
Aggregations