Search in sources :

Example 21 with CarbonContext

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;
}
Also used : Group(org.activiti.engine.identity.Group) ProcessDefinitionEntityManager(org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntityManager) CommandContext(org.activiti.engine.impl.interceptor.CommandContext) CommandExecutor(org.activiti.engine.impl.interceptor.CommandExecutor) ProcessDefinitionQuery(org.activiti.engine.repository.ProcessDefinitionQuery) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) RestIdentityLink(org.wso2.carbon.bpmn.rest.model.common.RestIdentityLink) IdentityLink(org.activiti.engine.task.IdentityLink) BPSGroupIdentityManager(org.wso2.carbon.bpmn.core.integration.BPSGroupIdentityManager) ProcessDefinitionEntity(org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity) RepositoryService(org.activiti.engine.RepositoryService) ProcessEngine(org.activiti.engine.ProcessEngine)

Example 22 with CarbonContext

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;
}
Also used : PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 23 with CarbonContext

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);
}
Also used : UserRealm(org.wso2.carbon.user.core.UserRealm) ComponentContext(org.osgi.service.component.ComponentContext) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) Registry(org.wso2.carbon.registry.core.Registry) CarbonContext(org.wso2.carbon.context.CarbonContext)

Example 24 with CarbonContext

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);
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) ComponentContext(org.osgi.service.component.ComponentContext) UserRealm(org.wso2.carbon.user.core.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) RegistryContext(org.wso2.carbon.registry.core.config.RegistryContext) AuthorizationManager(org.wso2.carbon.user.core.AuthorizationManager) Registry(org.wso2.carbon.registry.core.Registry) CarbonContext(org.wso2.carbon.context.CarbonContext) BundleContext(org.osgi.framework.BundleContext)

Example 25 with CarbonContext

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);
}
Also used : CertificateMgtDAO(org.wso2.carbon.apimgt.impl.dao.CertificateMgtDAO) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) CarbonContext(org.wso2.carbon.context.CarbonContext) BeforeClass(org.junit.BeforeClass)

Aggregations

PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)28 RealmService (org.wso2.carbon.user.core.service.RealmService)15 CarbonContext (org.wso2.carbon.context.CarbonContext)9 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 UserStoreException (org.wso2.carbon.user.api.UserStoreException)6 UserRealm (org.wso2.carbon.user.core.UserRealm)6 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)6 ArrayList (java.util.ArrayList)5 Registry (org.wso2.carbon.registry.core.Registry)5 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)5 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 List (java.util.List)3 Log (org.apache.commons.logging.Log)3 LogFactory (org.apache.commons.logging.LogFactory)3 OAuthTokenInfo (org.wso2.carbon.apimgt.api.OAuthTokenInfo)3 APIConstants (org.wso2.carbon.apimgt.impl.APIConstants)3 APIUtil (org.wso2.carbon.apimgt.impl.utils.APIUtil)3 ServiceReferenceHolder (org.wso2.carbon.apimgt.persistence.internal.ServiceReferenceHolder)3 RestApiConstants (org.wso2.carbon.apimgt.rest.api.common.RestApiConstants)3