Search in sources :

Example 1 with ProfileRestServiceException

use of org.craftercms.profile.exceptions.ProfileRestServiceException in project profile by craftercms.

the class ProfileServiceRestClient method getProfileByQuery.

@Override
public Profile getProfileByQuery(String tenantName, String query, String... attributesToReturn) throws ProfileException {
    MultiValueMap<String, String> params = createBaseParams();
    HttpUtils.addValue(PARAM_TENANT_NAME, tenantName, params);
    HttpUtils.addValue(PARAM_QUERY, query, params);
    HttpUtils.addValues(PARAM_ATTRIBUTE_TO_RETURN, attributesToReturn, params);
    String url = getAbsoluteUrl(BASE_URL_PROFILE + URL_PROFILE_GET_ONE_BY_QUERY);
    url = addQueryParams(url, params, true);
    try {
        return doGetForObject(new URI(url), Profile.class);
    } catch (URISyntaxException e) {
        throw new I10nProfileException(ERROR_KEY_INVALID_URI_ERROR, url);
    } catch (ProfileRestServiceException e) {
        if (e.getStatus() == HttpStatus.NOT_FOUND) {
            return null;
        } else {
            throw e;
        }
    }
}
Also used : ProfileRestServiceException(org.craftercms.profile.exceptions.ProfileRestServiceException) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 2 with ProfileRestServiceException

use of org.craftercms.profile.exceptions.ProfileRestServiceException in project profile by craftercms.

the class ProfileServiceIT method testDeleteAttributes.

@Test
@DirtiesContext
public void testDeleteAttributes() throws Exception {
    Profile profile = profileService.createProfile(DEFAULT_TENANT, AVASQUEZ_USERNAME, AVASQUEZ_PASSWORD1, AVASQUEZ_EMAIL1, false, AVASQUEZ_ROLES1, null, VERIFICATION_URL);
    Map<String, Object> attributes = new HashMap<>();
    try {
        Map<String, Object> subscriptions = new HashMap<>();
        subscriptions.put("frequency", JDOE_SUBSCRIPTIONS_FREQUENCY);
        subscriptions.put("autoWatch", JDOE_SUBSCRIPTIONS_AUTO_WATCH);
        subscriptions.put("targets", JDOE_SUBSCRIPTIONS_TARGETS);
        attributes.put("subscriptions", subscriptions);
        profileService.updateAttributes(profile.getId().toString(), attributes);
        accessTokenIdResolver.setAccessTokenId(RANDOM_APP_ACCESS_TOKEN_ID);
        // Unallowed deletes should be rejected
        try {
            profileService.removeAttributes(profile.getId().toString(), Arrays.asList("subscriptions"));
            fail("Exception " + ProfileRestServiceException.class.getName() + " expected");
        } catch (ProfileRestServiceException e) {
            assertEquals(HttpStatus.FORBIDDEN, e.getStatus());
            assertEquals(ErrorCode.ACTION_DENIED, e.getErrorCode());
        }
        accessTokenIdResolver.setAccessTokenId(ADMIN_CONSOLE_ACCESS_TOKEN_ID);
        // Delete an attribute
        profile = profileService.removeAttributes(profile.getId().toString(), Arrays.asList("subscriptions"));
        attributes = profile.getAttributes();
        assertNotNull(attributes);
        assertEquals(0, attributes.size());
    } finally {
        profileService.deleteProfile(profile.getId().toString());
    }
}
Also used : ProfileRestServiceException(org.craftercms.profile.exceptions.ProfileRestServiceException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 3 with ProfileRestServiceException

use of org.craftercms.profile.exceptions.ProfileRestServiceException in project profile by craftercms.

the class ProfileServiceIT method testGetProfileByQuery.

@Test
@DirtiesContext
public void testGetProfileByQuery() throws Exception {
    Profile profile = profileService.getProfileByQuery(DEFAULT_TENANT, QUERY1);
    assertAdminProfile(profile);
    // Try with tenant field in query
    try {
        profileService.getProfileByQuery(DEFAULT_TENANT, INVALID_QUERY1);
        fail("Exception " + ProfileRestServiceException.class.getName() + " expected");
    } catch (ProfileRestServiceException e) {
        assertEquals(HttpStatus.BAD_REQUEST, e.getStatus());
        assertEquals(ErrorCode.INVALID_QUERY, e.getErrorCode());
    }
    // Try with $where operator in query
    try {
        profileService.getProfileByQuery(DEFAULT_TENANT, INVALID_QUERY2);
        fail("Exception " + ProfileRestServiceException.class.getName() + " expected");
    } catch (ProfileRestServiceException e) {
        assertEquals(HttpStatus.BAD_REQUEST, e.getStatus());
        assertEquals(ErrorCode.INVALID_QUERY, e.getErrorCode());
    }
    accessTokenIdResolver.setAccessTokenId(RANDOM_APP_ACCESS_TOKEN_ID);
    // Try with unreadable attribute in query
    try {
        profileService.getProfileByQuery(DEFAULT_TENANT, QUERY2);
        fail("Exception " + ProfileRestServiceException.class.getName() + " expected");
    } catch (ProfileRestServiceException e) {
        assertEquals(HttpStatus.BAD_REQUEST, e.getStatus());
        assertEquals(ErrorCode.INVALID_QUERY, e.getErrorCode());
    }
}
Also used : ProfileRestServiceException(org.craftercms.profile.exceptions.ProfileRestServiceException) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 4 with ProfileRestServiceException

use of org.craftercms.profile.exceptions.ProfileRestServiceException in project profile by craftercms.

the class AuthenticationManagerImpl method authenticateUser.

@Override
public Authentication authenticateUser(String tenant, String username, String password) {
    try {
        Ticket ticket = authenticationService.authenticate(tenant, username, password);
        Profile profile = profileService.getProfile(ticket.getProfileId());
        if (profile == null) {
            throw new AuthenticationSystemException("No profile found for ID '" + ticket.getProfileId() + "'");
        }
        String ticketId = ticket.getId();
        DefaultAuthentication auth = new DefaultAuthentication(ticketId, profile);
        authenticationCache.putAuthentication(auth);
        logger.debug("Authentication successful for user '{}' (ticket ID = '{}')", ticket.getProfileId(), ticketId);
        return auth;
    } catch (ProfileRestServiceException e) {
        switch(e.getErrorCode()) {
            case DISABLED_PROFILE:
                throw new DisabledUserException("User is disabled", e);
            case BAD_CREDENTIALS:
                throw new BadCredentialsException("Invalid username and/or password", e);
            default:
                throw new AuthenticationSystemException("An unexpected error occurred while authenticating", e);
        }
    } catch (ProfileException e) {
        throw new AuthenticationSystemException("An unexpected error occurred while authenticating", e);
    }
}
Also used : Ticket(org.craftercms.profile.api.Ticket) ProfileRestServiceException(org.craftercms.profile.exceptions.ProfileRestServiceException) AuthenticationSystemException(org.craftercms.security.exception.AuthenticationSystemException) ProfileException(org.craftercms.profile.api.exceptions.ProfileException) BadCredentialsException(org.craftercms.security.exception.BadCredentialsException) Profile(org.craftercms.profile.api.Profile) DisabledUserException(org.craftercms.security.exception.DisabledUserException)

Example 5 with ProfileRestServiceException

use of org.craftercms.profile.exceptions.ProfileRestServiceException in project profile by craftercms.

the class TenantServiceIT method testCreateTenant.

@Test
public void testCreateTenant() throws Exception {
    Tenant tenant = tenantService.createTenant(getCorporateTenant());
    try {
        assertNotNull(tenant);
        assertNotNull(tenant.getId());
        assertEquals(CORPORATE_TENANT_NAME, tenant.getName());
        assertEquals(false, tenant.isVerifyNewProfiles());
        assertEquals(CORPORATE_ROLES, tenant.getAvailableRoles());
        try {
            tenantService.createTenant(getCorporateTenant());
            fail("Exception " + ProfileRestServiceException.class.getName() + " expected");
        } catch (ProfileRestServiceException e) {
            assertEquals(HttpStatus.CONFLICT, e.getStatus());
            assertEquals(ErrorCode.TENANT_EXISTS, e.getErrorCode());
        }
    } finally {
        tenantService.deleteTenant(CORPORATE_TENANT_NAME);
    }
}
Also used : ProfileRestServiceException(org.craftercms.profile.exceptions.ProfileRestServiceException) Tenant(org.craftercms.profile.api.Tenant) Test(org.junit.Test)

Aggregations

ProfileRestServiceException (org.craftercms.profile.exceptions.ProfileRestServiceException)9 Profile (org.craftercms.profile.api.Profile)4 Test (org.junit.Test)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 DirtiesContext (org.springframework.test.annotation.DirtiesContext)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Ticket (org.craftercms.profile.api.Ticket)2 ProfileException (org.craftercms.profile.api.exceptions.ProfileException)2 AuthenticationSystemException (org.craftercms.security.exception.AuthenticationSystemException)2 DisabledUserException (org.craftercms.security.exception.DisabledUserException)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Tenant (org.craftercms.profile.api.Tenant)1 ErrorCode (org.craftercms.profile.api.exceptions.ErrorCode)1 ErrorDetails (org.craftercms.profile.api.exceptions.ErrorDetails)1 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)1 BadCredentialsException (org.craftercms.security.exception.BadCredentialsException)1 Before (org.junit.Before)1 HttpStatus (org.springframework.http.HttpStatus)1