Search in sources :

Example 41 with Profile

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());
    }
}
Also used : Profile(org.craftercms.profile.api.Profile) HashSet(java.util.HashSet) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 42 with Profile

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());
    }
}
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 43 with Profile

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());
    }
}
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 44 with Profile

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());
    }
}
Also used : Profile(org.craftercms.profile.api.Profile) HashSet(java.util.HashSet) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 45 with Profile

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

Aggregations

Profile (org.craftercms.profile.api.Profile)110 Test (org.junit.Test)54 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)19 MongoDataException (org.craftercms.commons.mongo.MongoDataException)15 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)15 LinkedHashMap (java.util.LinkedHashMap)13 VerificationToken (org.craftercms.profile.api.VerificationToken)13 DefaultAuthentication (org.craftercms.security.authentication.impl.DefaultAuthentication)12 Authentication (org.craftercms.security.authentication.Authentication)11 Date (java.util.Date)10 Map (java.util.Map)10 ObjectId (org.bson.types.ObjectId)9 RequestContext (org.craftercms.commons.http.RequestContext)9 ArgumentMatcher (org.mockito.ArgumentMatcher)9 Mockito.anyString (org.mockito.Mockito.anyString)9 RequestSecurityProcessorChain (org.craftercms.security.processors.RequestSecurityProcessorChain)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 Tenant (org.craftercms.profile.api.Tenant)5 Ticket (org.craftercms.profile.api.Ticket)4