/** * Logs in with given username. * * @param username username of user * @param tenantId tenant to which this user belongs * @tenantAdmin true to add the tenant admin authority to the user's roles */ protected void login(final String username, final ITenant tenant, String[] roles) { StandaloneSession pentahoSession = new StandaloneSession(tenantedUserNameUtils.getPrincipleId(tenant, username)); pentahoSession.setAuthenticated( tenant.getId(), tenantedUserNameUtils.getPrincipleId(tenant, username)); PentahoSessionHolder.setSession(pentahoSession); pentahoSession.setAttribute(IPentahoSession.TENANT_ID_KEY, tenant.getId()); final String password = "******"; List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(); for (String roleName : roles) { authList.add( new GrantedAuthorityImpl(tenantedRoleNameUtils.getPrincipleId(tenant, roleName))); } GrantedAuthority[] authorities = authList.toArray(new GrantedAuthority[0]); UserDetails userDetails = new User(username, password, true, true, true, true, authorities); Authentication auth = new UsernamePasswordAuthenticationToken(userDetails, password, authorities); PentahoSessionHolder.setSession(pentahoSession); // this line necessary for Spring Security's MethodSecurityInterceptor SecurityContextHolder.getContext().setAuthentication(auth); SecurityHelper.getInstance().becomeUser(tenantedUserNameUtils.getPrincipleId(tenant, username)); SecurityContextHolder.getContext().setAuthentication(auth); }
@Before public void setUp() throws Exception { repository = mock(IUnifiedRepository.class); session = mock(IPentahoSession.class); when(session.getName()).thenReturn("test"); PentahoSessionHolder.setSession(session); userSettings = new HashMap<String, Serializable>() { { put(USER_SETTING_NAME_1, USER_SETTING_VALUE_1); put(UserSettingService.SETTING_PREFIX + COMMON_SETTING_NAME, COMMON_USER_SETTING_VALUE); put(USER_SETTING_NAME_2, USER_SETTING_VALUE_2); put(UserSettingService.SETTING_PREFIX + USER_SETTING_NAME_3, USER_SETTING_VALUE_3); } }; globalSettings = new HashMap<String, Serializable>() { { put(GLOBAL_SETTING_NAME_1, GLOBAL_SETTING_VALUE_1); put( UserSettingService.SETTING_PREFIX + COMMON_SETTING_NAME, COMMON_GLOBAL_SETTING_VALUE); put(GLOBAL_SETTING_NAME_2, GLOBAL_SETTING_VALUE_2); put(UserSettingService.SETTING_PREFIX + GLOBAL_SETTING_NAME_3, GLOBAL_SETTING_VALUE_3); } }; when(repository.getFileMetadata(eq(USER_FOLDER_ID))).thenReturn(userSettings); when(repository.getFileMetadata(eq(TENANT_FOLDER_ID))).thenReturn(globalSettings); final RepositoryFile tenantRepositoryFile = mock(RepositoryFile.class); when(tenantRepositoryFile.getId()).thenReturn(TENANT_FOLDER_ID); when(repository.getFile(eq(ClientRepositoryPaths.getEtcFolderPath()))) .thenReturn(tenantRepositoryFile); final RepositoryFile userRepositoryFile = mock(RepositoryFile.class); when(userRepositoryFile.getId()).thenReturn(USER_FOLDER_ID); when(repository.getFile(eq(ClientRepositoryPaths.getUserHomeFolderPath(session.getName())))) .thenReturn(userRepositoryFile); securityHelper = mock(ISecurityHelper.class); when(securityHelper.runAsSystem(any(Callable.class))) .thenAnswer( new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { final Callable callable = (Callable) invocation.getArguments()[0]; if (callable != null) { return callable.call(); } return null; } }); SecurityHelper.setMockInstance(securityHelper); userSettingService = new UserSettingService(repository); userSettingService.init(session); }
protected void login(final String username, final String tenantId, final boolean tenantAdmin) { StandaloneSession pentahoSession = new StandaloneSession(username); pentahoSession.setAuthenticated(username); pentahoSession.setAttribute(IPentahoSession.TENANT_ID_KEY, tenantId); final String password = "******"; List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(); authList.add( new GrantedAuthorityImpl( MessageFormat.format(tenantAuthenticatedAuthorityNamePattern, tenantId))); if (tenantAdmin) { authList.add( new GrantedAuthorityImpl( MessageFormat.format(tenantAdminAuthorityNamePattern, tenantId))); } GrantedAuthority[] authorities = authList.toArray(new GrantedAuthority[0]); UserDetails userDetails = new User(username, password, true, true, true, true, authorities); Authentication auth = new UsernamePasswordAuthenticationToken(userDetails, password, authorities); PentahoSessionHolder.setSession(pentahoSession); // this line necessary for Spring Security's MethodSecurityInterceptor SecurityContextHolder.getContext().setAuthentication(auth); manager.newTenant(); manager.newUser(); }
@Test public void testUserWorkspace() { PentahoSessionHolder.setSession(new StandaloneSession("jerry")); WebResource webResource = resource(); String userWorkspaceDir = webResource.path("session/userWorkspaceDir").accept(TEXT_PLAIN).get(String.class); assertTrue(userWorkspaceDir != null); assertTrue(userWorkspaceDir.length() > 0); }
protected void loginAsRepositoryAdmin() { StandaloneSession pentahoSession = new StandaloneSession(repositoryAdminUsername); pentahoSession.setAuthenticated(repositoryAdminUsername); final GrantedAuthority[] repositoryAdminAuthorities = new GrantedAuthority[] {}; final String password = "******"; UserDetails repositoryAdminUserDetails = new User( repositoryAdminUsername, password, true, true, true, true, repositoryAdminAuthorities); Authentication repositoryAdminAuthentication = new UsernamePasswordAuthenticationToken( repositoryAdminUserDetails, password, repositoryAdminAuthorities); PentahoSessionHolder.setSession(pentahoSession); // this line necessary for Spring Security's MethodSecurityInterceptor SecurityContextHolder.getContext().setAuthentication(repositoryAdminAuthentication); }
public static synchronized void initializePentahoSystem(String solutionPath) throws Exception { // this setting is useful only for running the integration tests from within IntelliJ // this same property is set for integration tests via the pom when running with mvn String folderPaths = "target/spoon/plugins"; File f = new File(folderPaths); System.setProperty("KETTLE_PLUGIN_BASE_FOLDERS", f.getAbsolutePath()); StandaloneApplicationContext appContext = new StandaloneApplicationContext(solutionPath, ""); PentahoSystem.setSystemSettingsService(new PathBasedSystemSettings()); if (solutionPath == null) { throw new MetaverseException( Messages.getString("ERROR.MetaverseInit.BadConfigPath", solutionPath)); } try { ClassLoader cl = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(MetaverseUtil.class.getClassLoader()); IPentahoObjectFactory pentahoObjectFactory = new StandaloneSpringPentahoObjectFactory(); pentahoObjectFactory.init(solutionPath, PentahoSystem.getApplicationContext()); PentahoSystem.registerObjectFactory(pentahoObjectFactory); // Restore context classloader Thread.currentThread().setContextClassLoader(cl); } catch (Exception e) { throw new MetaverseException(Messages.getString("ERROR.MetaverseInit.CouldNotInit"), e); } PentahoSystem.init(appContext); PentahoSessionHolder.setSession(new StandaloneSession()); registerKettlePlugins(); try { KettleEnvironment.init(); } catch (KettleException e) { e.printStackTrace(); } }