use of org.wso2.carbon.context.CarbonContext in project carbon-business-process by wso2.
the class ProcessInstanceService method isValidUserToStartProcess.
private boolean isValidUserToStartProcess(ProcessInstanceCreateRequest processInstanceCreateRequest) {
// check whether the users/groups exist
String processDefinitionId = processInstanceCreateRequest.getProcessDefinitionId();
RepositoryService repositoryService = BPMNOSGIService.getRepositoryService();
if (processDefinitionId == null) {
final String processDefinitionKey = processInstanceCreateRequest.getProcessDefinitionKey();
final String tenantId = processInstanceCreateRequest.getTenantId();
ProcessEngine processEngine = BPMNOSGIService.getBPMNEngineService().getProcessEngine();
if (processEngine != null) {
if (processDefinitionKey != null) {
if (((ProcessEngineImpl) processEngine).getProcessEngineConfiguration() != null) {
CommandExecutor commandExecutor = ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration().getCommandExecutor();
if (commandExecutor != null) {
processDefinitionId = (String) commandExecutor.execute(new Command<Object>() {
public Object execute(CommandContext commandContext) {
ProcessDefinitionEntityManager processDefinitionEntityManager = commandContext.getSession(ProcessDefinitionEntityManager.class);
ProcessDefinitionEntity processDefinitionEntity = processDefinitionEntityManager.findLatestProcessDefinitionByKeyAndTenantId(processDefinitionKey, tenantId);
if (processDefinitionEntity != null && processDefinitionEntity.getProcessDefinition() != null) {
return processDefinitionEntity.getProcessDefinition().getId();
}
return null;
}
});
}
}
if (processDefinitionId == null) {
return false;
}
}
String messageName = processInstanceCreateRequest.getMessage();
if (messageName != null && !messageName.isEmpty()) {
ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery().messageEventSubscriptionName(messageName);
if (processDefinitionQuery != null) {
processDefinitionQuery = processDefinitionQuery.processDefinitionTenantId(processInstanceCreateRequest.getTenantId());
if (processDefinitionQuery != null && processDefinitionQuery.count() > 1) {
processDefinitionQuery = processDefinitionQuery.latestVersion();
}
}
if (processDefinitionQuery != null) {
ProcessDefinition processDefinition = processDefinitionQuery.singleResult();
if (processDefinition != null) {
processDefinitionId = processDefinition.getId();
}
}
if (processDefinitionId == null) {
return false;
}
}
}
}
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
String userName = carbonContext.getUsername();
String tenantDomain = carbonContext.getTenantDomain();
String userNameWithTenantDomain = userName + "@" + tenantDomain;
BPSGroupIdentityManager bpsGroupIdentityManager = BPMNOSGIService.getGroupIdentityManager();
List<Group> groupList = bpsGroupIdentityManager.findGroupsByUser(userName);
List<IdentityLink> identityLinkList = repositoryService.getIdentityLinksForProcessDefinition(processDefinitionId);
boolean valueExistsForUserId = false;
boolean valueExistsForGroupId = false;
for (IdentityLink identityLink : identityLinkList) {
String userId = identityLink.getUserId();
if (userId != null) {
valueExistsForUserId = true;
if (userId.contains("$")) {
userId = resolveVariable(processInstanceCreateRequest, userId);
}
if (!userId.isEmpty() && (userId.equals(userName) || userId.equals(userNameWithTenantDomain))) {
return true;
}
continue;
}
String groupId = identityLink.getGroupId();
if (groupId != null) {
valueExistsForGroupId = true;
if (groupId.contains("$")) {
groupId = resolveVariable(processInstanceCreateRequest, groupId);
}
for (Group identityGroup : groupList) {
if (!groupId.isEmpty() && identityGroup.getId() != null && identityGroup.getId().equals(groupId)) {
return true;
}
}
}
}
if (!valueExistsForGroupId && !valueExistsForUserId) {
return true;
}
return false;
}
use of org.wso2.carbon.context.CarbonContext in project carbon-business-process by wso2.
the class CarbonContextConfigurator method invoke.
@Override
public InvocationResponse invoke(MessageContext messageContext) throws AxisFault {
PrivilegedCarbonContext cc = PrivilegedCarbonContext.getThreadLocalCarbonContext();
// If this request is not dispatching to BPS published axis2 services, just return
if (!isDispatchingToBPS(messageContext)) {
return InvocationResponse.CONTINUE;
}
// If carbonContext is already populated with tenant domain and tenant id properly configured ,just return
if (cc.getTenantDomain() != null && cc.getTenantId() != MultitenantConstants.INVALID_TENANT_ID) {
return InvocationResponse.CONTINUE;
}
// If incoming transport is non http, assume super tenant domain and tenant id and configure
if (messageContext.getTransportIn() != null && messageContext.getTransportIn().getName() != null && !messageContext.getTransportIn().getName().contains("http")) {
cc.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
cc.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
return InvocationResponse.CONTINUE;
}
try {
EndpointReference epr = messageContext.getTo();
if (epr != null) {
String to = epr.getAddress();
if (to != null && to.indexOf("/" + MultitenantConstants.TENANT_AWARE_URL_PREFIX + "/") != -1) {
String str1 = to.substring(to.indexOf("/t/") + 3);
String domain = str1.substring(0, str1.indexOf("/"));
cc.setTenantDomain(domain, true);
} else {
cc.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
cc.setTenantId(MultitenantConstants.SUPER_TENANT_ID);
}
}
} catch (Throwable ignore) {
// don't care if anything failed
}
return InvocationResponse.CONTINUE;
}
use of org.wso2.carbon.context.CarbonContext in project carbon-apimgt by wso2.
the class APIManagerComponentPermissionsTest method setup.
@Before
public void setup() throws Exception {
PowerMockito.mockStatic(CarbonContext.class);
PowerMockito.mockStatic(APIUtil.class);
CarbonContext carbonContext = Mockito.mock(CarbonContext.class);
realm = Mockito.mock(UserRealm.class);
componentContext = Mockito.mock(ComponentContext.class);
registry = Mockito.mock(Registry.class);
Mockito.when(carbonContext.getRegistry(RegistryType.USER_GOVERNANCE)).thenReturn(registry);
Mockito.when(carbonContext.getUsername()).thenReturn(USER_NAME);
Mockito.when(carbonContext.getUserRealm()).thenReturn(realm);
PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(carbonContext);
PowerMockito.when(APIUtil.class, "loadTenantExternalStoreConfig", MultitenantConstants.SUPER_TENANT_DOMAIN_NAME).thenThrow(IndexOutOfBoundsException.class);
}
use of org.wso2.carbon.context.CarbonContext in project carbon-apimgt by wso2.
the class APIManagerComponentImagePermissionTest method setup.
@Before
public void setup() throws Exception {
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.mockStatic(CarbonContext.class);
PowerMockito.mockStatic(RegistryUtils.class);
PowerMockito.mockStatic(APIMgtDBUtil.class);
authManager = Mockito.mock(AuthorizationManager.class);
serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
realmService = Mockito.mock(RealmService.class);
componentContext = Mockito.mock(ComponentContext.class);
BundleContext bundleContext = Mockito.mock(BundleContext.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
Registry registry = Mockito.mock(Registry.class);
CarbonContext carbonContext = Mockito.mock(CarbonContext.class);
APIManagerConfiguration configuration = Mockito.mock(APIManagerConfiguration.class);
Mockito.when(componentContext.getBundleContext()).thenReturn(bundleContext);
Mockito.when(realmService.getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID)).thenReturn(userRealm);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(userRealm.getAuthorizationManager()).thenReturn(authManager);
Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true);
Mockito.when(carbonContext.getRegistry(RegistryType.USER_GOVERNANCE)).thenReturn(registry);
Mockito.doNothing().when(configuration).load(Mockito.anyString());
Mockito.when(configuration.getFirstProperty(Mockito.anyString())).thenReturn("");
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
PowerMockito.when(APIUtil.getMountedPath(null, "")).thenReturn("");
PowerMockito.when(RegistryUtils.getAbsolutePath(null, null)).thenReturn("");
PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(carbonContext);
PowerMockito.when(APIUtil.getMountedPath(Mockito.any(RegistryContext.class), Mockito.anyString())).thenReturn("");
PowerMockito.doThrow(new IndexOutOfBoundsException()).when(APIMgtDBUtil.class, "initialize");
PowerMockito.whenNew(APIManagerConfiguration.class).withAnyArguments().thenReturn(configuration);
}
use of org.wso2.carbon.context.CarbonContext in project carbon-apimgt by wso2.
the class CertificateManagerImplTest method init.
@BeforeClass
public static void init() throws XMLStreamException {
PowerMockito.mockStatic(CertificateMgtDAO.class);
certificateMgtDAO = Mockito.mock(CertificateMgtDAO.class);
PowerMockito.when(CertificateMgtDAO.getInstance()).thenReturn(certificateMgtDAO);
System.setProperty("javax.net.ssl.trustStore", CertificateManagerImplTest.class.getClassLoader().getResource("security/client-truststore.jks").getPath());
System.setProperty(JAVAX_NET_SSL_TRUST_STORE_PASSWORD_PROPERTY, JAVAX_SSL_TRUST_STORE_PASSWORD);
TestUtils.initConfigurationContextService(true);
certificateManager = CertificateManagerImpl.getInstance();
System.setProperty(CARBON_HOME, "");
PowerMockito.mockStatic(CarbonContext.class);
CarbonContext carbonContext = Mockito.mock(CarbonContext.class);
PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(carbonContext);
Mockito.when(carbonContext.getTenantDomain()).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
Mockito.when(carbonContext.getTenantId()).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
}
Aggregations