Search in sources :

Example 71 with Profile

use of org.craftercms.profile.api.Profile in project profile by craftercms.

the class ProfileServiceIT method testEnableProfile.

@Test
public void testEnableProfile() throws Exception {
    Profile profile = profileService.createProfile(DEFAULT_TENANT, AVASQUEZ_USERNAME, AVASQUEZ_PASSWORD1, AVASQUEZ_EMAIL1, false, AVASQUEZ_ROLES1, null, VERIFICATION_URL);
    try {
        assertNotNull(profile);
        assertFalse(profile.isEnabled());
        Profile updatedProfile = profileService.enableProfile(profile.getId().toString());
        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());
        assertTrue(updatedProfile.isEnabled());
        assertEquals(profile.getCreatedOn(), updatedProfile.getCreatedOn());
        assertTrue(profile.getLastModified().before(updatedProfile.getLastModified()));
        assertEquals(profile.getTenant(), updatedProfile.getTenant());
        assertEquals(profile.getRoles(), updatedProfile.getRoles());
        assertEquals(profile.getAttributes(), updatedProfile.getAttributes());
    } finally {
        profileService.deleteProfile(profile.getId().toString());
    }
}
Also used : Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 72 with Profile

use of org.craftercms.profile.api.Profile in project profile by craftercms.

the class MellonAutoLoginProcessor method processRequest.

@Override
public void processRequest(RequestContext context, RequestSecurityProcessorChain processorChain) throws Exception {
    HttpServletRequest request = context.getRequest();
    String username = request.getHeader(usernameHeaderName);
    Authentication auth = SecurityUtils.getAuthentication(request);
    if (StringUtils.isNotEmpty(username) && (auth == null || !auth.getProfile().getUsername().equals(username))) {
        String[] tenantNames = tenantsResolver.getTenants();
        Tenant tenant = getSsoEnabledTenant(tenantNames);
        if (tenant != null) {
            Profile profile = profileService.getProfileByUsername(tenant.getName(), username);
            if (profile == null) {
                profile = createProfileWithSsoInfo(username, tenant, request);
            }
            SecurityUtils.setAuthentication(request, authenticationManager.authenticateUser(profile));
        } else {
            logger.warn("An SSO login was attempted, but none of the tenants [{}] is enabled for SSO", tenantNames);
        }
    }
    processorChain.processRequest(context);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Tenant(org.craftercms.profile.api.Tenant) Authentication(org.craftercms.security.authentication.Authentication) Profile(org.craftercms.profile.api.Profile)

Example 73 with Profile

use of org.craftercms.profile.api.Profile in project profile by craftercms.

the class RefreshCurrentAuthenticationOnProfileUpdateAspect method refreshCurrentAuthentication.

@AfterReturning(value = "execution(* org.craftercms.profile.api.services.ProfileService.updateProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.verifyProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.enableProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.disableProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.addRoles(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.removeRoles(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.updateAttributes(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.removeAttributes(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.changePassword(..))", returning = "updatedProfile")
public void refreshCurrentAuthentication(Profile updatedProfile) {
    Authentication auth = SecurityUtils.getCurrentAuthentication();
    if (auth != null) {
        Profile profile = auth.getProfile();
        if (profile.equals(updatedProfile)) {
            String ticket = auth.getTicket();
            auth = new DefaultAuthentication(ticket, updatedProfile);
            // Put updated authentication in cache
            authenticationCache.putAuthentication(auth);
            // Update current authentication object
            SecurityUtils.setCurrentAuthentication(auth);
        }
    }
}
Also used : DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) Authentication(org.craftercms.security.authentication.Authentication) DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) Profile(org.craftercms.profile.api.Profile) AfterReturning(org.aspectj.lang.annotation.AfterReturning)

Example 74 with Profile

use of org.craftercms.profile.api.Profile in project profile by craftercms.

the class ProviderLoginSupportImpl method complete.

@Override
public Authentication complete(String tenant, String providerId, HttpServletRequest request, Set<String> newUserRoles, Map<String, Object> newUserAttributes, ConnectSupport connectSupport) throws AuthenticationException {
    if (connectSupport == null) {
        connectSupport = this.connectSupport;
    }
    Connection<?> connection = completeConnection(connectSupport, providerId, request);
    if (connection != null) {
        Profile userData = ConnectionUtils.createProfile(connection);
        Profile profile = getProfile(tenant, userData);
        if (profile == null) {
            if (CollectionUtils.isNotEmpty(newUserRoles)) {
                userData.getRoles().addAll(newUserRoles);
            }
            if (MapUtils.isNotEmpty(newUserAttributes)) {
                userData.getAttributes().putAll(newUserAttributes);
            }
            profile = createProfile(tenant, connection, userData);
        } else {
            profile = updateProfileConnectionData(tenant, connection, profile);
        }
        Authentication auth = authenticationManager.authenticateUser(profile);
        SecurityUtils.setAuthentication(request, auth);
        return auth;
    } else {
        return null;
    }
}
Also used : Authentication(org.craftercms.security.authentication.Authentication) Profile(org.craftercms.profile.api.Profile)

Example 75 with Profile

use of org.craftercms.profile.api.Profile in project profile by craftercms.

the class ConnectionUtils method createProfile.

/**
 * Creates a profile from the specified connection.
 *
 * @param connection the connection where to retrieve the profile info from
 *
 * @return the created profile
 */
public static Profile createProfile(Connection<?> connection) {
    Profile profile = new Profile();
    UserProfile providerProfile = connection.fetchUserProfile();
    String email = providerProfile.getEmail();
    if (StringUtils.isEmpty(email)) {
        throw new IllegalStateException("No email included in provider profile");
    }
    String username = providerProfile.getUsername();
    if (StringUtils.isEmpty(username)) {
        username = email;
    }
    String firstName = providerProfile.getFirstName();
    String lastName = providerProfile.getLastName();
    String displayName;
    if (StringUtils.isNotEmpty(connection.getDisplayName())) {
        displayName = connection.getDisplayName();
    } else {
        displayName = firstName + " " + lastName;
    }
    profile.setUsername(username);
    profile.setEmail(email);
    profile.setAttribute(FIRST_NAME_ATTRIBUTE_NAME, firstName);
    profile.setAttribute(LAST_NAME_ATTRIBUTE_NAME, lastName);
    profile.setAttribute(DISPLAY_NAME_ATTRIBUTE_NAME, displayName);
    if (StringUtils.isNotEmpty(connection.getImageUrl())) {
        profile.setAttribute(AVATAR_LINK_ATTRIBUTE_NAME, connection.getImageUrl());
    }
    return profile;
}
Also used : UserProfile(org.springframework.social.connect.UserProfile) UserProfile(org.springframework.social.connect.UserProfile) Profile(org.craftercms.profile.api.Profile)

Aggregations

Profile (org.craftercms.profile.api.Profile)111 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 Date (java.util.Date)11 Map (java.util.Map)11 ObjectId (org.bson.types.ObjectId)10 RequestContext (org.craftercms.commons.http.RequestContext)9 Authentication (org.craftercms.security.authentication.Authentication)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)6 HashMap (java.util.HashMap)4