Search in sources :

Example 16 with IUserRoleListService

use of org.pentaho.platform.api.engine.IUserRoleListService in project pentaho-platform by pentaho.

the class SystemService method getRoles.

/**
 * Returns XML for list of roles.
 */
public Document getRoles() throws ServletException, IOException {
    IUserRoleListService service = PentahoSystem.get(IUserRoleListService.class);
    // $NON-NLS-1$
    Element rootElement = new DefaultElement("roles");
    Document doc = DocumentHelper.createDocument(rootElement);
    if (service != null) {
        List<String> roles = service.getAllRoles();
        for (Iterator<String> rolesIterator = roles.iterator(); rolesIterator.hasNext(); ) {
            String roleName = rolesIterator.next().toString();
            if ((null != roleName) && (roleName.length() > 0)) {
                // $NON-NLS-1$
                rootElement.addElement("role").setText(roleName);
            }
        }
    }
    return doc;
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService) Document(org.dom4j.Document)

Example 17 with IUserRoleListService

use of org.pentaho.platform.api.engine.IUserRoleListService in project pentaho-platform by pentaho.

the class SystemService method getUsers.

/**
 * Returns XML for list of users in the platform.
 */
public Document getUsers() throws ServletException, IOException, IllegalAccessException {
    if (!canAdminister()) {
        throw new IllegalAccessException();
    }
    IUserRoleListService service = PentahoSystem.get(IUserRoleListService.class);
    // $NON-NLS-1$
    Element rootElement = new DefaultElement("users");
    Document doc = DocumentHelper.createDocument(rootElement);
    if (service != null) {
        List<String> users = service.getAllUsers();
        for (Iterator<String> usersIterator = users.iterator(); usersIterator.hasNext(); ) {
            String username = usersIterator.next().toString();
            if ((null != username) && (username.length() > 0)) {
                // $NON-NLS-1$
                rootElement.addElement("user").setText(username);
            }
        }
    }
    return doc;
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService) Document(org.dom4j.Document)

Example 18 with IUserRoleListService

use of org.pentaho.platform.api.engine.IUserRoleListService in project pentaho-platform by pentaho.

the class UserRoleListService method getPermissionRoles.

public RoleListWrapper getPermissionRoles(String adminRole) {
    IUserRoleListService userRoleListService = getUserRoleListService();
    List<String> allRoles = userRoleListService.getAllRoles();
    // We will not allow user to update permission for Administrator
    if (allRoles.contains(adminRole)) {
        allRoles.remove(adminRole);
    }
    // Add extra roles to the list of roles
    if (extraRoles != null) {
        for (String extraRole : extraRoles) {
            if (!allRoles.contains(extraRole)) {
                allRoles.add(extraRole);
            }
        }
    }
    if (null != roleComparator) {
        Collections.sort(allRoles, roleComparator);
    }
    return new RoleListWrapper(allRoles);
}
Also used : RoleListWrapper(org.pentaho.platform.web.http.api.resources.RoleListWrapper) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService)

Example 19 with IUserRoleListService

use of org.pentaho.platform.api.engine.IUserRoleListService in project pentaho-platform by pentaho.

the class HttpWebService method getRoles.

/**
 * Returns XML for list of roles.
 */
protected Document getRoles() throws ServletException, IOException {
    IUserRoleListService service = PentahoSystem.get(IUserRoleListService.class);
    Element rootElement = new DefaultElement("roles");
    Document doc = DocumentHelper.createDocument(rootElement);
    if (service != null) {
        List roles = service.getAllRoles();
        for (Iterator rolesIterator = roles.iterator(); rolesIterator.hasNext(); ) {
            String roleName = rolesIterator.next().toString();
            if ((null != roleName) && (roleName.length() > 0)) {
                rootElement.addElement("role").addCDATA(roleName);
            }
        }
    }
    return doc;
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) DefaultElement(org.dom4j.tree.DefaultElement) Element(org.dom4j.Element) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService) Document(org.dom4j.Document)

Example 20 with IUserRoleListService

use of org.pentaho.platform.api.engine.IUserRoleListService 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)

Aggregations

IUserRoleListService (org.pentaho.platform.api.engine.IUserRoleListService)34 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)8 IAuthorizationPolicy (org.pentaho.platform.api.engine.IAuthorizationPolicy)5 List (java.util.List)4 Document (org.dom4j.Document)4 Element (org.dom4j.Element)4 DefaultElement (org.dom4j.tree.DefaultElement)4 RoleListWrapper (org.pentaho.platform.web.http.api.resources.RoleListWrapper)4 MicroPlatform (org.pentaho.test.platform.engine.core.MicroPlatform)4 File (java.io.File)3 Callable (java.util.concurrent.Callable)3 IPluginResourceLoader (org.pentaho.platform.api.engine.IPluginResourceLoader)3 ITenant (org.pentaho.platform.api.mt.ITenant)3 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)3 SystemSettings (org.pentaho.platform.engine.core.system.SystemSettings)3 PluginClassLoader (org.pentaho.platform.plugin.services.pluginmgr.PluginClassLoader)3 PluginResourceLoader (org.pentaho.platform.plugin.services.pluginmgr.PluginResourceLoader)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2