Search in sources :

Example 1 with UserRegistrationConfigDTO

use of org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO in project carbon-apimgt by wso2.

the class UserSignUpWSWorkflowExecutor method complete.

@Override
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException {
    // workflowDTO.setStatus(workflowDTO.getStatus());
    workflowDTO.setUpdatedTime(System.currentTimeMillis());
    if (log.isDebugEnabled()) {
        log.debug("User Sign Up [Complete] Workflow Invoked. Workflow ID : " + workflowDTO.getExternalWorkflowReference() + "Workflow State : " + workflowDTO.getStatus());
    }
    super.complete(workflowDTO);
    String tenantDomain = workflowDTO.getTenantDomain();
    try {
        UserRegistrationConfigDTO signupConfig = SelfSignUpUtil.getSignupConfiguration(tenantDomain);
        String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(workflowDTO.getWorkflowReference());
        if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
            try {
                updateRolesOfUser(tenantAwareUserName, SelfSignUpUtil.getRoleNames(signupConfig), tenantDomain);
            } catch (Exception e) {
                // updateRolesOfUser throws generic Exception. Therefore generic Exception is caught
                throw new WorkflowException("Error while assigning role to user", e);
            }
        } else {
            try {
                /* Remove created user */
                deleteUser(tenantDomain, tenantAwareUserName);
            } catch (Exception e) {
                throw new WorkflowException("Error while deleting the user", e);
            }
        }
    } catch (APIManagementException e1) {
        throw new WorkflowException("Error while accessing signup configuration", e1);
    }
    return new GeneralWorkflowResponse();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserRegistrationConfigDTO(org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 2 with UserRegistrationConfigDTO

use of org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO in project carbon-apimgt by wso2.

the class APIUtil method isUserInRole.

/**
 * Check whether the user has the given role
 *
 * @throws UserStoreException
 * @throws APIManagementException
 */
public static boolean isUserInRole(String user, String role) throws UserStoreException, APIManagementException {
    String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(user));
    UserRegistrationConfigDTO signupConfig = SelfSignUpUtil.getSignupConfiguration(tenantDomain);
    user = SelfSignUpUtil.getDomainSpecificUserName(user, signupConfig);
    String tenantAwareUserName = MultitenantUtils.getTenantAwareUsername(user);
    RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
    int tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
    UserRealm realm = (UserRealm) realmService.getTenantUserRealm(tenantId);
    org.wso2.carbon.user.core.UserStoreManager manager = realm.getUserStoreManager();
    AbstractUserStoreManager abstractManager = (AbstractUserStoreManager) manager;
    return abstractManager.isUserInRole(tenantAwareUserName, role);
}
Also used : UserRealm(org.wso2.carbon.user.core.UserRealm) UserRegistrationConfigDTO(org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO) RealmService(org.wso2.carbon.user.core.service.RealmService) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) Endpoint(org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint)

Example 3 with UserRegistrationConfigDTO

use of org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO in project carbon-apimgt by wso2.

the class UserSignUpSimpleWorkflowExecutorTest method testExecutingUserSignUpSimpleWorkflow.

@Test
public void testExecutingUserSignUpSimpleWorkflow() throws APIManagementException, UserStoreException {
    Map<String, Boolean> roleMap = new HashMap<String, Boolean>();
    roleMap.put(signUpRole, false);
    UserRegistrationConfigDTO userRegistrationConfigDTO = new UserRegistrationConfigDTO();
    userRegistrationConfigDTO.setAdminUserName("admin");
    userRegistrationConfigDTO.setAdminPassword("admin");
    userRegistrationConfigDTO.setRoles(roleMap);
    PowerMockito.when(SelfSignUpUtil.getSignupConfiguration(tenantDomain)).thenReturn(userRegistrationConfigDTO);
    PowerMockito.when(SelfSignUpUtil.getRoleNames(userRegistrationConfigDTO)).thenCallRealMethod();
    Mockito.when(userStoreManager.isExistingUser(username)).thenReturn(true);
    Mockito.when(userStoreManager.isExistingRole("Internal/" + signUpRole)).thenReturn(true);
    Mockito.doNothing().when(userStoreManager).updateRoleListOfUser(username, null, new String[] { "Internal/" + signUpRole });
    try {
        Assert.assertNotNull(userSignUpSimpleWorkflowExecutor.execute(workflowDTO));
    } catch (WorkflowException e) {
        Assert.fail("Unexpected WorkflowException has thrown while executing the user signup simple workflow");
    }
}
Also used : HashMap(java.util.HashMap) UserRegistrationConfigDTO(org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with UserRegistrationConfigDTO

use of org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO in project carbon-apimgt by wso2.

the class UserSignUpSimpleWorkflowExecutorTest method testFailuresToCompleteUserSignUpSimpleWorkflow.

@Test
public void testFailuresToCompleteUserSignUpSimpleWorkflow() throws Exception {
    Map<String, Boolean> roleMap = new HashMap<String, Boolean>();
    roleMap.put(signUpRole, false);
    UserRegistrationConfigDTO userRegistrationConfigDTO = new UserRegistrationConfigDTO();
    userRegistrationConfigDTO.setRoles(roleMap);
    workflowDTO.setTenantDomain(tenantDomain);
    PowerMockito.when(SelfSignUpUtil.class, "getSignupConfiguration", tenantDomain).thenReturn(userRegistrationConfigDTO);
    PowerMockito.when(SelfSignUpUtil.class, "getRoleNames", userRegistrationConfigDTO).thenReturn(Collections.singletonList("Internal/" + signUpRole));
    Mockito.when(userStoreManager.isExistingUser(username)).thenReturn(true);
    // Test failure to complete workflow execution, when sign up roles are not existing in user realm
    Mockito.when(userStoreManager.isExistingRole("Internal/" + signUpRole)).thenReturn(false);
    try {
        userSignUpSimpleWorkflowExecutor.execute(workflowDTO);
        Assert.fail("Expected WorkflowException has not been thrown when signup role is not existing");
    } catch (WorkflowException e) {
        Assert.assertEquals(e.getMessage(), "Error while assigning role to user");
    }
    // Test failure to complete workflow execution, when error has been occurred while retrieving signup config
    PowerMockito.when(SelfSignUpUtil.getSignupConfiguration(tenantDomain)).thenThrow(new APIManagementException("Error occurred while retrieving signup configuration"));
    try {
        userSignUpSimpleWorkflowExecutor.execute(workflowDTO);
        Assert.fail("Expected WorkflowException has not been thrown retrieving sign up configuration");
    } catch (WorkflowException e) {
        Assert.assertEquals(e.getMessage(), "Error while accessing signup configuration");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) HashMap(java.util.HashMap) UserRegistrationConfigDTO(org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO) SelfSignUpUtil(org.wso2.carbon.apimgt.impl.utils.SelfSignUpUtil) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with UserRegistrationConfigDTO

use of org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO in project carbon-apimgt by wso2.

the class SelfSignupUtilTestCase method testGetSelfSignupConfigFromRegistryTenant.

@Test
public void testGetSelfSignupConfigFromRegistryTenant() throws Exception {
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    APIMConfigService apimConfigService = Mockito.mock(APIMConfigService.class);
    Mockito.when(serviceReferenceHolder.getApimConfigService()).thenReturn(apimConfigService);
    PowerMockito.mockStatic(APIUtil.class);
    Mockito.when(apimConfigService.getSelfSighupConfig("bar.com")).thenReturn("wsdl");
    OMElement omElement = Mockito.mock(OMElement.class);
    Mockito.when(omElement.getFirstChildWithName(Matchers.any(QName.class))).thenReturn(omElement);
    PowerMockito.mockStatic(AXIOMUtil.class);
    Mockito.when(omElement.getChildrenWithLocalName(APIConstants.SELF_SIGN_UP_REG_ROLE_ELEM)).thenReturn(Mockito.mock(Iterator.class));
    PowerMockito.when(AXIOMUtil.stringToOM("wsdl")).thenReturn(omElement);
    PowerMockito.mockStatic(PasswordResolverFactory.class);
    PasswordResolver passwordResolver = Mockito.mock(PasswordResolver.class);
    PowerMockito.when(PasswordResolverFactory.getInstance()).thenReturn(passwordResolver);
    UserRegistrationConfigDTO userRegistrationConfigDTO = SelfSignUpUtil.getSignupConfiguration("bar.com");
    Assert.assertNotNull(userRegistrationConfigDTO);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) PasswordResolver(org.wso2.carbon.apimgt.api.PasswordResolver) QName(javax.xml.namespace.QName) UserRegistrationConfigDTO(org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) APIMConfigService(org.wso2.carbon.apimgt.impl.config.APIMConfigService) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

UserRegistrationConfigDTO (org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO)16 Test (org.junit.Test)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 HashMap (java.util.HashMap)7 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 Iterator (java.util.Iterator)3 QName (javax.xml.namespace.QName)3 OMElement (org.apache.axiom.om.OMElement)3 PasswordResolver (org.wso2.carbon.apimgt.api.PasswordResolver)3 XMLStreamException (javax.xml.stream.XMLStreamException)2 APIMConfigService (org.wso2.carbon.apimgt.impl.config.APIMConfigService)2 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)2 SelfSignUpUtil (org.wso2.carbon.apimgt.impl.utils.SelfSignUpUtil)1 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)1 Endpoint (org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint)1 UserRealm (org.wso2.carbon.user.core.UserRealm)1 AbstractUserStoreManager (org.wso2.carbon.user.core.common.AbstractUserStoreManager)1 RealmService (org.wso2.carbon.user.core.service.RealmService)1