PentahoPlatformExporter extends ZipExportProcessor { 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(); IUserSettingService service = getUserSettingService(); 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 ); } if ( service != null ) { List<IUserSetting> globalUserSettings = service.getGlobalUserSettings(); if ( globalUserSettings != null ) { for ( IUserSetting setting : globalUserSettings ) { getExportManifest().addGlobalUserSetting( new ExportManifestUserSetting( setting ) ); } } } 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 ); } } PentahoPlatformExporter( IUnifiedRepository repository ); File performExport(); @Override File performExport( RepositoryFile exportRepositoryFile ); IScheduler getScheduler(); void setScheduler( IScheduler scheduler ); IMetadataDomainRepository getMetadataDomainRepository(); void setMetadataDomainRepository( IMetadataDomainRepository metadataDomainRepository ); IDatasourceMgmtService getDatasourceMgmtService(); void setDatasourceMgmtService( IDatasourceMgmtService datasourceMgmtService ); MondrianCatalogRepositoryHelper getMondrianCatalogRepositoryHelper(); void setMondrianCatalogRepositoryHelper(
    MondrianCatalogRepositoryHelper mondrianCatalogRepositoryHelper ); IMondrianCatalogService getMondrianCatalogService(); void setMondrianCatalogService(
    IMondrianCatalogService mondrianCatalogService ); IUserSettingService getUserSettingService(); void setUserSettingService( IUserSettingService userSettingService ); static final String ROOT; static final String DATA_SOURCES_PATH_IN_ZIP; static final String METADATA_PATH_IN_ZIP; static final String ANALYSIS_PATH_IN_ZIP; static final String CONNECTIONS_PATH_IN_ZIP; static final String METASTORE; static final String METASTORE_BACKUP_EXT; }
@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 user = "testUser"; String role = "testRole"; userList.add( user ); when( mockDao.getAllUsers( any( ITenant.class ) ) ).thenReturn( userList ); List<String> roleList = new ArrayList<>(); roleList.add( role ); when( mockDao.getAllRoles() ).thenReturn( roleList ); Map<String, List<String>> map = new HashMap<>(); List<String> permissions = new ArrayList<>(); 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<>(); 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() ); }