Search in sources :

Example 1 with RoleExport

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

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

the class SolutionImportHandlerTest method testImportRoles.

@Test
public void testImportRoles() {
    String roleName = "ADMIN";
    List<String> permissions = new ArrayList<>();
    RoleExport role = new RoleExport();
    role.setRolename(roleName);
    role.setPermission(permissions);
    List<RoleExport> roles = new ArrayList<>();
    roles.add(role);
    Map<String, List<String>> roleToUserMap = new HashMap<>();
    final List<String> adminUsers = new ArrayList<>();
    adminUsers.add("admin");
    adminUsers.add("root");
    roleToUserMap.put(roleName, adminUsers);
    String[] userStrings = adminUsers.toArray(new String[] {});
    importHandler.importRoles(roles, roleToUserMap);
    verify(userRoleDao).createRole(any(ITenant.class), eq(roleName), nullable(String.class), any(userStrings.getClass()));
    verify(roleAuthorizationPolicyRoleBindingDao).setRoleBindings(any(ITenant.class), eq(roleName), eq(permissions));
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Mockito.anyString(org.mockito.Mockito.anyString) RoleExport(org.pentaho.platform.plugin.services.importexport.RoleExport) Test(org.junit.Test)

Example 3 with RoleExport

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

the class SolutionImportHandlerTest method testImportRoles_roleAlreadyExists_overwriteFalse.

@Test
public void testImportRoles_roleAlreadyExists_overwriteFalse() {
    String roleName = "ADMIN";
    List<String> permissions = new ArrayList<>();
    RoleExport role = new RoleExport();
    role.setRolename(roleName);
    role.setPermission(permissions);
    List<RoleExport> roles = new ArrayList<>();
    roles.add(role);
    Map<String, List<String>> roleToUserMap = new HashMap<>();
    final List<String> adminUsers = new ArrayList<>();
    adminUsers.add("admin");
    adminUsers.add("root");
    roleToUserMap.put(roleName, adminUsers);
    String[] userStrings = adminUsers.toArray(new String[] {});
    when(userRoleDao.createRole(any(ITenant.class), nullable(String.class), nullable(String.class), any(userStrings.getClass()))).thenThrow(new AlreadyExistsException("already there"));
    importHandler.setOverwriteFile(false);
    importHandler.importRoles(roles, roleToUserMap);
    verify(userRoleDao).createRole(any(ITenant.class), nullable(String.class), nullable(String.class), any(userStrings.getClass()));
    // even if the roles exists, make sure we set the permissions on it Mockito.anyway... they might have changed
    verify(roleAuthorizationPolicyRoleBindingDao, never()).setRoleBindings(any(ITenant.class), eq(roleName), eq(permissions));
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Mockito.anyString(org.mockito.Mockito.anyString) RoleExport(org.pentaho.platform.plugin.services.importexport.RoleExport) Test(org.junit.Test)

Example 4 with RoleExport

use of org.pentaho.platform.plugin.services.importexport.RoleExport 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 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(nullable(String.class))).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(nullable(String.class))).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) 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)

Example 5 with RoleExport

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

the class SolutionImportHandlerTest method testImportRoles_roleAlreadyExists.

@Test
public void testImportRoles_roleAlreadyExists() {
    String roleName = "ADMIN";
    List<String> permissions = new ArrayList<>();
    RoleExport role = new RoleExport();
    role.setRolename(roleName);
    role.setPermission(permissions);
    List<RoleExport> roles = new ArrayList<>();
    roles.add(role);
    Map<String, List<String>> roleToUserMap = new HashMap<>();
    final List<String> adminUsers = new ArrayList<>();
    adminUsers.add("admin");
    adminUsers.add("root");
    roleToUserMap.put(roleName, adminUsers);
    String[] userStrings = adminUsers.toArray(new String[] {});
    when(userRoleDao.createRole(any(ITenant.class), nullable(String.class), nullable(String.class), any(userStrings.getClass()))).thenThrow(new AlreadyExistsException("already there"));
    importHandler.setOverwriteFile(true);
    importHandler.importRoles(roles, roleToUserMap);
    verify(userRoleDao).createRole(any(ITenant.class), nullable(String.class), nullable(String.class), any(userStrings.getClass()));
    // even if the roles exists, make sure we set the permissions on it Mockito.anyway... they might have changed
    verify(roleAuthorizationPolicyRoleBindingDao).setRoleBindings(any(ITenant.class), eq(roleName), eq(permissions));
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Mockito.anyString(org.mockito.Mockito.anyString) RoleExport(org.pentaho.platform.plugin.services.importexport.RoleExport) Test(org.junit.Test)

Aggregations

ITenant (org.pentaho.platform.api.mt.ITenant)6 RoleExport (org.pentaho.platform.plugin.services.importexport.RoleExport)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Test (org.junit.Test)4 Mockito.anyString (org.mockito.Mockito.anyString)3 AlreadyExistsException (org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException)3 IRoleAuthorizationPolicyRoleBindingDao (org.pentaho.platform.security.policy.rolebased.IRoleAuthorizationPolicyRoleBindingDao)3 IUserRoleListService (org.pentaho.platform.api.engine.IUserRoleListService)2 IAnyUserSettingService (org.pentaho.platform.api.usersettings.IAnyUserSettingService)2 IUserSetting (org.pentaho.platform.api.usersettings.pojo.IUserSetting)2 ExportManifestUserSetting (org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting)2 UserExport (org.pentaho.platform.plugin.services.importexport.UserExport)2 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)1 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)1 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)1