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;
}
}
}
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());
}
}
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());
}
}
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);
}
}
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);
}
}
Aggregations