Search in sources :

Example 1 with RoleBindingStruct

use of org.pentaho.platform.security.policy.rolebased.RoleBindingStruct in project pentaho-platform by pentaho.

the class UserRoleDaoService method getRoleBindingStruct.

public SystemRolesMap getRoleBindingStruct(String locale) throws SecurityException {
    if (canAdminister()) {
        RoleBindingStruct roleBindingStruct = getRoleBindingDao().getRoleBindingStruct(locale);
        SystemRolesMap systemRolesMap = new SystemRolesMap();
        for (Map.Entry<String, String> localalizeNameEntry : roleBindingStruct.logicalRoleNameMap.entrySet()) {
            systemRolesMap.getLocalizedRoleNames().add(new LocalizedLogicalRoleName(localalizeNameEntry.getKey(), localalizeNameEntry.getValue()));
        }
        for (Map.Entry<String, List<String>> logicalRoleAssignments : roleBindingStruct.bindingMap.entrySet()) {
            systemRolesMap.getAssignments().add(new LogicalRoleAssignment(logicalRoleAssignments.getKey(), logicalRoleAssignments.getValue(), roleBindingStruct.immutableRoles.contains(logicalRoleAssignments.getKey())));
        }
        return systemRolesMap;
    } else {
        throw new SecurityException();
    }
}
Also used : LocalizedLogicalRoleName(org.pentaho.platform.web.http.api.resources.LocalizedLogicalRoleName) RoleBindingStruct(org.pentaho.platform.security.policy.rolebased.RoleBindingStruct) List(java.util.List) SystemRolesMap(org.pentaho.platform.web.http.api.resources.SystemRolesMap) Map(java.util.Map) SystemRolesMap(org.pentaho.platform.web.http.api.resources.SystemRolesMap) LogicalRoleAssignment(org.pentaho.platform.web.http.api.resources.LogicalRoleAssignment)

Example 2 with RoleBindingStruct

use of org.pentaho.platform.security.policy.rolebased.RoleBindingStruct 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>();
    String user = "testUser";
    String role = "testRole";
    userList.add(user);
    when(mockDao.getAllUsers(any(ITenant.class))).thenReturn(userList);
    List<String> roleList = new ArrayList<String>();
    roleList.add(role);
    when(mockDao.getAllRoles()).thenReturn(roleList);
    Map<String, List<String>> map = new HashMap<String, List<String>>();
    List<String> permissions = new ArrayList<String>();
    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<GrantedAuthority>();
    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());
}
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) Mockito.anyString(org.mockito.Mockito.anyString) 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 3 with RoleBindingStruct

use of org.pentaho.platform.security.policy.rolebased.RoleBindingStruct in project pentaho-platform by pentaho.

the class UserRoleDaoServiceTest method testGetRoleBindingStruct.

@Test
public void testGetRoleBindingStruct() {
    setupMockSessionUser(SESSION_USER_NAME, true);
    Map<String, String> localizedNameEntry = new HashMap<>();
    localizedNameEntry.put("testEntry", "testValue");
    Map<String, List<String>> testBindingMap = new HashMap<>();
    testBindingMap.put("testBinding", new ArrayList<String>());
    RoleBindingStruct roleBindingStruct = mock(RoleBindingStruct.class);
    roleBindingStruct.logicalRoleNameMap = localizedNameEntry;
    roleBindingStruct.bindingMap = testBindingMap;
    roleBindingStruct.immutableRoles = new HashSet<>();
    IRoleAuthorizationPolicyRoleBindingDao roleBindingDao = mock(IRoleAuthorizationPolicyRoleBindingDao.class);
    when(roleBindingDao.getRoleBindingStruct(anyString())).thenReturn(roleBindingStruct);
    PentahoSystem.registerObject(roleBindingDao);
    SystemRolesMap validateMap = userRoleService.getRoleBindingStruct("en");
    assert (1 == validateMap.getLocalizedRoleNames().size());
    assertEquals("testEntry", validateMap.getLocalizedRoleNames().get(0).getRoleName());
    assertEquals("testValue", validateMap.getLocalizedRoleNames().get(0).getLocalizedName());
}
Also used : IRoleAuthorizationPolicyRoleBindingDao(org.pentaho.platform.security.policy.rolebased.IRoleAuthorizationPolicyRoleBindingDao) HashMap(java.util.HashMap) RoleBindingStruct(org.pentaho.platform.security.policy.rolebased.RoleBindingStruct) ArrayList(java.util.ArrayList) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) SystemRolesMap(org.pentaho.platform.web.http.api.resources.SystemRolesMap) Test(org.junit.Test)

Example 4 with RoleBindingStruct

use of org.pentaho.platform.security.policy.rolebased.RoleBindingStruct in project pentaho-platform by pentaho.

the class DefaultUnifiedRepositoryAuthorizationIT method testRoleAuthorizationPolicyGetRoleBindingStruct.

/**
 * Please keep this test the last of the testRoleAuthorizationPolicy* since it adds a binding that cannot be
 * deleted, only set to no associated logical roles.
 */
@Test
public void testRoleAuthorizationPolicyGetRoleBindingStruct() throws Exception {
    ITenant tenantAcme = null;
    loginAsSysTenantAdmin();
    tenantAcme = tenantManager.createTenant(systemTenant, TENANT_ID_ACME, tenantAdminRoleName, tenantAuthenticatedRoleName, ANONYMOUS_ROLE_NAME);
    userRoleDao.createUser(tenantAcme, USERNAME_ADMIN, PASSWORD, "", new String[] { tenantAdminRoleName });
    login(USERNAME_ADMIN, tenantAcme, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
    RoleBindingStruct struct = roleBindingDao.getRoleBindingStruct(Locale.getDefault().toString());
    assertNotNull(struct);
    assertNotNull(struct.bindingMap);
    assertEquals(3, struct.bindingMap.size());
    assertEquals(Arrays.asList(new String[] { RepositoryReadAction.NAME, RepositoryCreateAction.NAME, SchedulerAction.NAME, AdministerSecurityAction.NAME, PublishAction.NAME }), struct.bindingMap.get(superAdminRoleName));
    assertEquals(Arrays.asList(new String[] { RepositoryReadAction.NAME, RepositoryCreateAction.NAME, SchedulerAction.NAME, AdministerSecurityAction.NAME, PublishAction.NAME }), struct.bindingMap.get(tenantAdminRoleName));
    assertEquals(Arrays.asList(new String[] { RepositoryReadAction.NAME, RepositoryCreateAction.NAME, SchedulerAction.NAME }), struct.bindingMap.get(tenantAuthenticatedRoleName));
    roleBindingDao.setRoleBindings("whatever", Arrays.asList("org.pentaho.p1.reader"));
    struct = roleBindingDao.getRoleBindingStruct(Locale.getDefault().toString());
    assertEquals(4, struct.bindingMap.size());
    assertEquals(Arrays.asList(new String[] { "org.pentaho.p1.reader" }), struct.bindingMap.get("whatever"));
    assertNotNull(struct.logicalRoleNameMap);
    assertEquals(5, struct.logicalRoleNameMap.size());
    assertEquals("Create Content", struct.logicalRoleNameMap.get(RepositoryCreateAction.NAME));
    assertNotNull(struct.immutableRoles);
    assertEquals(2, struct.immutableRoles.size());
    assertTrue(struct.immutableRoles.contains(superAdminRoleName));
    assertTrue(struct.immutableRoles.contains(tenantAdminRoleName));
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) RoleBindingStruct(org.pentaho.platform.security.policy.rolebased.RoleBindingStruct) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 5 with RoleBindingStruct

use of org.pentaho.platform.security.policy.rolebased.RoleBindingStruct in project pentaho-platform by pentaho.

the class RoleBindingStructAdapterTest method testMarshalUnmarshal.

@Test
public void testMarshalUnmarshal() throws Exception {
    final HashMap<String, String> logicalRoleNameMap = new HashMap<String, String>();
    logicalRoleNameMap.put(RepositoryCreateAction.NAME, "Create Content");
    final HashMap<String, List<String>> bindingMap = new HashMap<String, List<String>>();
    final LinkedList<String> roles = new LinkedList<String>();
    roles.add(RepositoryCreateAction.NAME);
    bindingMap.put("admin", roles);
    final HashSet<String> immutableRoles = new HashSet<String>();
    immutableRoles.add("admin");
    final RoleBindingStruct roleBindingStruct = new RoleBindingStruct(logicalRoleNameMap, bindingMap, immutableRoles);
    RoleBindingStructAdapter adapter = new RoleBindingStructAdapter();
    final JaxbSafeRoleBindingStruct marshaled = adapter.marshal(roleBindingStruct);
    assertEquals(roleBindingStruct.bindingMap.size(), marshaled.bindingMapEntries.size());
    assertEquals(roleBindingStruct.logicalRoleNameMap.size(), marshaled.logicalRoleNameMapEntries.size());
    assertEquals(roleBindingStruct.immutableRoles.size(), marshaled.immutableRoles.size());
    final RoleBindingStruct unmarshaled = adapter.unmarshal(marshaled);
    assertEquals(roleBindingStruct.bindingMap, unmarshaled.bindingMap);
    assertEquals(roleBindingStruct.logicalRoleNameMap, unmarshaled.logicalRoleNameMap);
    assertEquals(roleBindingStruct.immutableRoles, unmarshaled.immutableRoles);
}
Also used : HashMap(java.util.HashMap) RoleBindingStruct(org.pentaho.platform.security.policy.rolebased.RoleBindingStruct) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

RoleBindingStruct (org.pentaho.platform.security.policy.rolebased.RoleBindingStruct)5 List (java.util.List)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)2 Matchers.anyString (org.mockito.Matchers.anyString)2 ITenant (org.pentaho.platform.api.mt.ITenant)2 IRoleAuthorizationPolicyRoleBindingDao (org.pentaho.platform.security.policy.rolebased.IRoleAuthorizationPolicyRoleBindingDao)2 SystemRolesMap (org.pentaho.platform.web.http.api.resources.SystemRolesMap)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Mockito.anyString (org.mockito.Mockito.anyString)1 IUserRoleListService (org.pentaho.platform.api.engine.IUserRoleListService)1 IAnyUserSettingService (org.pentaho.platform.api.usersettings.IAnyUserSettingService)1 IUserSetting (org.pentaho.platform.api.usersettings.pojo.IUserSetting)1 ExportManifestUserSetting (org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting)1 RoleExport (org.pentaho.platform.plugin.services.importexport.RoleExport)1 UserExport (org.pentaho.platform.plugin.services.importexport.UserExport)1 ExportManifest (org.pentaho.platform.plugin.services.importexport.exportManifest.ExportManifest)1