/** * 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.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; }
protected void importRoles(List<RoleExport> roles, Map<String, List<String>> roleToUserMap) { IUserRoleDao roleDao = PentahoSystem.get(IUserRoleDao.class); ITenant tenant = new Tenant("/pentaho/" + TenantUtils.getDefaultTenant(), true); IRoleAuthorizationPolicyRoleBindingDao roleBindingDao = PentahoSystem.get(IRoleAuthorizationPolicyRoleBindingDao.class); Set<String> existingRoles = new HashSet<>(); if (roles != null) { for (RoleExport role : roles) { log.debug("Importing role: " + role.getRolename()); try { List<String> users = roleToUserMap.get(role.getRolename()); String[] userarray = users == null ? new String[] {} : users.toArray(new String[] {}); IPentahoRole role1 = roleDao.createRole(tenant, role.getRolename(), null, userarray); } catch (AlreadyExistsException e) { existingRoles.add(role.getRolename()); // it's ok if the role already exists, it is probably a default role log.info(Messages.getInstance().getString("ROLE.Already.Exists", role.getRolename())); } try { if (existingRoles.contains(role.getRolename())) { // Only update an existing role if the overwrite flag is set if (isOverwriteFile()) { roleBindingDao.setRoleBindings(tenant, role.getRolename(), role.getPermissions()); } } else { // Always write a roles permissions that were not previously existing roleBindingDao.setRoleBindings(tenant, role.getRolename(), role.getPermissions()); } } catch (Exception e) { log.info( Messages.getInstance().getString("ERROR.SettingRolePermissions", role.getRolename()), e); } } } }