use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.
the class TenantUtils method getCurrentTenant.
/**
* Returns the tenant ID of the current user.
*/
public static ITenant getCurrentTenant() {
IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
if (pentahoSession == null) {
throw new IllegalStateException();
}
String tenantId = (String) pentahoSession.getAttribute(IPentahoSession.TENANT_ID_KEY);
if (tenantId == null) {
ITenantedPrincipleNameResolver tenantedUserNameUtils = PentahoSystem.get(ITenantedPrincipleNameResolver.class, "tenantedUserNameUtils", pentahoSession);
if (tenantedUserNameUtils != null) {
ITenant tenant = tenantedUserNameUtils.getTenant(pentahoSession.getId());
pentahoSession.setAttribute(IPentahoSession.TENANT_ID_KEY, tenant.getId());
return new Tenant(tenant.getId(), true);
}
}
return new Tenant(tenantId, true);
}
use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.
the class SecurityHelper method becomeUser.
/**
* Hi-jacks the system for the named user. <p/> <p> This is for unit tests only. </p>
*/
@Override
public void becomeUser(final String principalName, final IParameterProvider paramProvider) {
UserSession session = null;
tenantedUserNameUtils = getTenantedUserNameUtils();
if (tenantedUserNameUtils != null) {
session = new UserSession(principalName, null, false, paramProvider);
ITenant tenant = tenantedUserNameUtils.getTenant(principalName);
session.setAttribute(IPentahoSession.TENANT_ID_KEY, tenant.getId());
session.setAuthenticated(tenant.getId(), principalName);
} else {
session = new UserSession(principalName, null, false, paramProvider);
session.setAuthenticated(principalName);
}
PentahoSessionHolder.setSession(session);
Authentication auth = createAuthentication(principalName);
// TODO We need to figure out how to inject this
// Get the tenant id from the principle name and set it as an attribute of the pentaho session
// Clearing the SecurityContext to force the subsequent call to getContext() to generate a new SecurityContext.
// This prevents us from modifying the Authentication on a SecurityContext isntance which may be shared between
// threads.
PentahoSessionHolder.getSession().setAttribute(IPentahoSession.SESSION_ROLES, auth.getAuthorities());
SecurityContextHolder.clearContext();
SecurityContextHolder.getContext().setAuthentication(auth);
PentahoSystem.sessionStartup(PentahoSessionHolder.getSession(), paramProvider);
}
use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.
the class RepositoryImportResourceTest method setUp.
@Before
public void setUp() throws ObjectFactoryException, PlatformImportException, DomainIdNullException, DomainAlreadyExistsException, DomainStorageException, IOException {
PentahoSystem.init();
IPentahoSession session = mock(IPentahoSession.class);
doReturn("sampleSession").when(session).getName();
PentahoSessionHolder.setSession(session);
handler = mock(SolutionImportHandler.class);
importer = mock(PentahoPlatformImporter.class);
policy = mock(IAuthorizationPolicy.class);
ITenant tenat = mock(ITenant.class);
resolver = mock(ITenantedPrincipleNameResolver.class);
doReturn(tenat).when(resolver).getTenant(anyString());
doReturn(REAL_USER).when(resolver).getPrincipleName(anyString());
policy = mock(IAuthorizationPolicy.class);
pentahoObjectFactory = mock(IPentahoObjectFactory.class);
iPlatformMimeResolver = mock(NameBaseMimeResolver.class);
iRepositoryImportLogger = mock(IRepositoryImportLogger.class);
catalogService = mock(MondrianCatalogHelper.class);
doReturn("xml").when(iPlatformMimeResolver).resolveMimeForFileName("");
doReturn(iRepositoryImportLogger).when(importer).getRepositoryImportLogger();
// for calling importFile in RepositoryImportResource
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
handler.importFile(any(IPlatformImportBundle.class));
return null;
}
}).when(importer).importFile(any(IPlatformImportBundle.class));
// for calling importFile in PentahoPlatformImporter
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
handler.getImportSession();
return null;
}
}).when(handler).importFile(any(IPlatformImportBundle.class));
// for calling getImportSession in SolutionImportHandler
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
ImportSession importsession = ImportSession.getSession();
importsession.setManifest(mock(ExportManifest.class));
return null;
}
}).when(handler).getImportSession();
when(pentahoObjectFactory.objectDefined(anyString())).thenReturn(true);
when(pentahoObjectFactory.get(this.anyClass(), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
if (invocation.getArguments()[0].equals(IAuthorizationPolicy.class)) {
return policy;
}
if (invocation.getArguments()[0].equals(ITenantedPrincipleNameResolver.class)) {
return resolver;
}
if (invocation.getArguments()[0].equals(IMondrianCatalogService.class)) {
return catalogService;
}
return null;
}
});
PentahoSystem.registerObjectFactory(pentahoObjectFactory);
PentahoSystem.registerObject(iPlatformMimeResolver);
PentahoSystem.registerObject(iRepositoryImportLogger);
PentahoSystem.registerObject(catalogService);
PentahoSystem.registerObject(handler);
PentahoSystem.registerObject(importer);
}
use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.
the class UserRoleDaoResource_RolesUpdatedTest method rolesUpdated_WhenAssignAllUsersToRole_WithSessionUser.
@Test
public void rolesUpdated_WhenAssignAllUsersToRole_WithSessionUser() {
ITenant tenant = mock(ITenant.class);
doReturn(tenant).when(resource).getTenant(DEFAULT_STRING);
IPentahoUser sessionUser = new MockPentahoUser(tenant, SESSION_USER_NAME, DEFAULT_STRING, DEFAULT_STRING, true);
doReturn(Collections.singletonList(sessionUser)).when(userRoleDao).getUsers(tenant);
resource.assignAllUsersToRole(DEFAULT_STRING, ROLE_NAME_DEVELOPER);
verify(resource).updateRolesForCurrentSession();
}
use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.
the class UserRoleDaoResource_RolesUpdatedTest method rolesNotUpdated_WhenAssignAllUsersToRole_WithNoSessionUser.
@Test
public void rolesNotUpdated_WhenAssignAllUsersToRole_WithNoSessionUser() {
ITenant tenant = mock(ITenant.class);
doReturn(tenant).when(resource).getTenant(DEFAULT_STRING);
IPentahoUser nonSessionUser = new MockPentahoUser(tenant, NON_SESSION_USER_NAME, DEFAULT_STRING, DEFAULT_STRING, true);
doReturn(Collections.singletonList(nonSessionUser)).when(userRoleDao).getUsers(tenant);
resource.assignAllUsersToRole(DEFAULT_STRING, ROLE_NAME_DEVELOPER);
verify(resource, never()).updateRolesForCurrentSession();
}
Aggregations