Search in sources :

Example 26 with IUserRoleListService

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

the class UserRoleListServiceTest method testGetUsers.

@Test
public void testGetUsers() {
    IUserRoleListService service = mock(IUserRoleListService.class);
    doReturn(service).when(userRoleListService).getUserRoleListService();
    List<String> users = new ArrayList<String>();
    users.add("admin");
    users.add("joe");
    users.add("suzy");
    doReturn(users).when(service).getAllUsers();
    UserListWrapper usersWrapper = userRoleListService.getUsers();
    assertTrue(usersWrapper.getUsers().size() == 3);
}
Also used : ArrayList(java.util.ArrayList) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService) UserListWrapper(org.pentaho.platform.web.http.api.resources.UserListWrapper) Test(org.junit.Test)

Example 27 with IUserRoleListService

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

the class DefaultUnifiedRepositoryBase method initialize.

public void initialize(boolean multiByteEncoding) throws Exception {
    loginAsRepositoryAdmin();
    SimpleJcrTestUtils.deleteItem(testJcrTemplate, ServerRepositoryPaths.getPentahoRootFolderPath());
    mp = new MicroPlatform(getSolutionPath());
    // used by DefaultPentahoJackrabbitAccessControlHelper
    mp.defineInstance("tenantedUserNameUtils", userNameUtils);
    mp.defineInstance("tenantedRoleNameUtils", roleNameUtils);
    mp.defineInstance("ILockHelper", new DefaultLockHelper(userNameUtils));
    mp.defineInstance(IAuthorizationPolicy.class, authorizationPolicy);
    mp.defineInstance(ITenantManager.class, tenantManager);
    mp.defineInstance("roleAuthorizationPolicyRoleBindingDaoTarget", roleBindingDaoTarget);
    mp.defineInstance("repositoryAdminUsername", repositoryAdminUsername);
    mp.defineInstance("RepositoryFileProxyFactory", new RepositoryFileProxyFactory(this.jcrTemplate, this.repositoryFileDao));
    mp.defineInstance("ITenantedPrincipleNameResolver", new DefaultTenantedPrincipleNameResolver());
    mp.defineInstance("useMultiByteEncoding", multiByteEncoding);
    mp.defineInstance(IUnifiedRepository.class, repo);
    mp.defineInstance(IRepositoryFileAclDao.class, repositoryFileAclDao);
    IUserRoleListService userRoleListService = mock(IUserRoleListService.class);
    when(userRoleListService.getRolesForUser(any(ITenant.class), anyString())).thenReturn(Arrays.asList(tenantAdminRoleName, AUTHENTICATED_ROLE_NAME));
    mp.defineInstance(IUserRoleListService.class, userRoleListService);
    mp.defineInstance("singleTenantAdminUserName", singleTenantAdminUserName);
    mp.defineInstance("singleTenantAdminAuthorityName", tenantAdminRoleName);
    // Start the micro-platform
    mp.start();
    loginAsRepositoryAdmin();
    setAclManagement();
    systemTenant = tenantManager.createTenant(null, ServerRepositoryPaths.getPentahoRootFolderName(), tenantAdminRoleName, tenantAuthenticatedRoleName, ANONYMOUS_ROLE_NAME);
    userRoleDao.createUser(systemTenant, sysAdminUserName, PASSWORD, "", new String[] { tenantAdminRoleName });
    logout();
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) DefaultLockHelper(org.pentaho.platform.repository2.unified.jcr.DefaultLockHelper) MicroPlatform(org.pentaho.test.platform.engine.core.MicroPlatform) DefaultTenantedPrincipleNameResolver(org.pentaho.platform.security.userroledao.DefaultTenantedPrincipleNameResolver) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService) RepositoryFileProxyFactory(org.pentaho.platform.repository2.unified.jcr.RepositoryFileProxyFactory)

Example 28 with IUserRoleListService

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

the class SecurityHelperTest method runAsSystemTest.

@Test
@SuppressWarnings("unchecked")
public void runAsSystemTest() throws Exception {
    // creating environment
    PentahoSystemBoot boot = new PentahoSystemBoot();
    boot.setFilePath("test-src/solution");
    IPentahoObjectFactory pentahoObjectFactory = mock(IPentahoObjectFactory.class, PENTAHO_OBJECT_FACTORY_MOCK_NAME);
    when(pentahoObjectFactory.objectDefined(eq(SINGLE_TENANT_ADMIN_USER_NAME))).thenReturn(true);
    when(pentahoObjectFactory.get(eq(String.class), eq(SINGLE_TENANT_ADMIN_USER_NAME), Matchers.<IPentahoSession>any())).thenReturn(ADMIN_USER_NAME);
    when(pentahoObjectFactory.getName()).thenReturn(PENTAHO_OBJECT_FACTORY_MOCK_NAME);
    boot.setFactory(pentahoObjectFactory);
    IUserRoleListService mockUserRoleListService = getUserRoleListServiceMock(ADMIN_USER_NAME, ADMIN_ROLES_ARRAY);
    doReturn(mockUserRoleListService).when(emptySecurityHelper).getUserRoleListService();
    // test for call
    Callable<String> callable = (Callable<String>) mock(Callable.class);
    when(callable.call()).thenReturn(CALLABLE_RETURNED_VALUE_OK);
    String runningResult = emptySecurityHelper.runAsSystem(callable);
    assertEquals(CALLABLE_RETURNED_VALUE_OK, runningResult);
}
Also used : PentahoSystemBoot(org.pentaho.platform.engine.core.system.boot.PentahoSystemBoot) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 29 with IUserRoleListService

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

the class SecurityHelperTest method getUserRoleListServiceMock.

private IUserRoleListService getUserRoleListServiceMock(String userName, String[] roles) {
    IUserRoleListService mockUserRoleListService = mock(IUserRoleListService.class);
    List<String> noRoles = new ArrayList<String>();
    List<String> allRoles = new ArrayList<String>(Arrays.asList(roles));
    when(mockUserRoleListService.getRolesForUser(Matchers.<ITenant>any(), eq(userName))).thenReturn(allRoles);
    when(mockUserRoleListService.getRolesForUser(Matchers.<ITenant>any(), AdditionalMatchers.not(eq(userName)))).thenReturn(noRoles);
    return mockUserRoleListService;
}
Also used : ArrayList(java.util.ArrayList) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService)

Example 30 with IUserRoleListService

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

the class SecurityHelperTest method testWithSharedSecurityContext.

@Test
public /**
 * Verification for BISERVER-12365 where a Threads are sharing a SecurityContext and making concurrent calls to
 * runAsSytem() and runAsUser()
 */
void testWithSharedSecurityContext() throws InterruptedException {
    IUserRoleListService userRoleListService = getUserRoleListServiceMock("admin", new String[] { "authenticated" });
    when(userRoleListService.getRolesForUser(Matchers.<ITenant>any(), eq("suzy"))).thenReturn(Collections.singletonList("authenticated"));
    PentahoSystem.registerObject(userRoleListService);
    PentahoSystem.registerReference(new SingletonPentahoObjectReference.Builder<String>(String.class).object("admin").attributes(Collections.<String, Object>singletonMap("id", "singleTenantAdminUserName")).build());
    SecurityContextHolder.setStrategyName(PentahoSecurityContextHolderStrategy.class.getName());
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("suzy", "password");
    final SecurityContext context = new SecurityContextImpl();
    SecurityContextHolder.setContext(context);
    SecurityContextHolder.getContext().setAuthentication(token);
    final CountDownLatch startSignal = new CountDownLatch(1);
    final CountDownLatch doneSignal = new CountDownLatch(1);
    final Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                SecurityContextHolder.setContext(context);
                SecurityHelper.getInstance().runAsSystem(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        System.out.println("Starting Thread 2");
                        startSignal.await();
                        // waiting for t1 to finish
                        doneSignal.await();
                        System.out.println("Finishing Thread 2");
                        return null;
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                fail(e.getMessage());
            }
        }
    });
    final Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                SecurityContextHolder.setContext(context);
                SecurityHelper.getInstance().runAsSystem(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        System.out.println("Starting Thread 1");
                        startSignal.await();
                        System.out.println("Finishing Thread 1");
                        return null;
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                fail(e.getMessage());
            }
        }
    });
    t1.start();
    t2.start();
    // let all threads proceed
    startSignal.countDown();
    // waits for t1 to die
    t1.join();
    // t2 is waiting for t1 to finish, let t2 proceed
    doneSignal.countDown();
    // waits for t2 to die
    t2.join();
    assertSame(token.getPrincipal(), SecurityContextHolder.getContext().getAuthentication().getPrincipal());
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) SecurityContext(org.springframework.security.core.context.SecurityContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) 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