use of org.craftercms.profile.api.Profile in project profile by craftercms.
the class ProfileServiceIT method testAddRoles.
@Test
public void testAddRoles() throws Exception {
Profile profile = profileService.createProfile(DEFAULT_TENANT, AVASQUEZ_USERNAME, AVASQUEZ_PASSWORD1, AVASQUEZ_EMAIL1, false, AVASQUEZ_ROLES1, null, VERIFICATION_URL);
try {
assertNotNull(profile);
assertEquals(AVASQUEZ_ROLES1, profile.getRoles());
Profile updatedProfile = profileService.addRoles(profile.getId().toString(), Arrays.asList("SOCIAL_AUTHOR"));
Set<String> expectedRoles = new HashSet<>(AVASQUEZ_ROLES1);
expectedRoles.add("SOCIAL_AUTHOR");
assertNotNull(updatedProfile);
assertEquals(profile.getId(), updatedProfile.getId());
assertEquals(profile.getUsername(), updatedProfile.getUsername());
assertNull(updatedProfile.getPassword());
assertEquals(profile.getEmail(), updatedProfile.getEmail());
assertEquals(profile.isVerified(), updatedProfile.isVerified());
assertEquals(profile.isEnabled(), updatedProfile.isEnabled());
assertEquals(profile.getCreatedOn(), updatedProfile.getCreatedOn());
assertTrue(profile.getLastModified().before(updatedProfile.getLastModified()));
assertEquals(profile.getTenant(), updatedProfile.getTenant());
assertEquals(expectedRoles, updatedProfile.getRoles());
assertEquals(profile.getAttributes(), updatedProfile.getAttributes());
} finally {
profileService.deleteProfile(profile.getId().toString());
}
}
use of org.craftercms.profile.api.Profile 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.api.Profile 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.api.Profile in project profile by craftercms.
the class ProfileServiceIT method testRemoveRoles.
@Test
public void testRemoveRoles() throws Exception {
Profile profile = profileService.createProfile(DEFAULT_TENANT, AVASQUEZ_USERNAME, AVASQUEZ_PASSWORD1, AVASQUEZ_EMAIL1, false, AVASQUEZ_ROLES1, null, VERIFICATION_URL);
try {
assertNotNull(profile);
assertEquals(AVASQUEZ_ROLES1, profile.getRoles());
Profile updatedProfile = profileService.removeRoles(profile.getId().toString(), Arrays.asList("SOCIAL_MODERATOR"));
Set<String> expectedRoles = new HashSet<>(AVASQUEZ_ROLES1);
expectedRoles.remove("SOCIAL_MODERATOR");
assertNotNull(updatedProfile);
assertEquals(profile.getId(), updatedProfile.getId());
assertEquals(profile.getUsername(), updatedProfile.getUsername());
assertNull(updatedProfile.getPassword());
assertEquals(profile.getEmail(), updatedProfile.getEmail());
assertEquals(profile.isVerified(), updatedProfile.isVerified());
assertEquals(profile.isEnabled(), updatedProfile.isEnabled());
assertEquals(profile.getCreatedOn(), updatedProfile.getCreatedOn());
assertTrue(profile.getLastModified().before(updatedProfile.getLastModified()));
assertEquals(profile.getTenant(), updatedProfile.getTenant());
assertEquals(expectedRoles, updatedProfile.getRoles());
assertEquals(profile.getAttributes(), updatedProfile.getAttributes());
} finally {
profileService.deleteProfile(profile.getId().toString());
}
}
use of org.craftercms.profile.api.Profile 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);
}
}
Aggregations