Search in sources :

Example 6 with Profile

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

the class ProfileServiceImpl method verifyProfile.

@Override
public Profile verifyProfile(String verificationTokenId, final String... attributesToReturn) throws ProfileException {
    VerificationToken token = verificationService.getToken(verificationTokenId);
    if (token == null) {
        throw new NoSuchVerificationTokenException(verificationTokenId);
    }
    Profile profile = updateProfile(token.getProfileId(), profileUpdater -> {
        profileUpdater.setEnabled(true);
        profileUpdater.setVerified(true);
    }, attributesToReturn);
    verificationService.deleteToken(verificationTokenId);
    logger.debug(LOG_KEY_PROFILE_VERIFIED, profile.getId());
    return profile;
}
Also used : NoSuchVerificationTokenException(org.craftercms.profile.exceptions.NoSuchVerificationTokenException) VerificationToken(org.craftercms.profile.api.VerificationToken) Profile(org.craftercms.profile.api.Profile)

Example 7 with Profile

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

the class ProfileServiceImpl method getProfilesByExistingAttribute.

@Override
public List<Profile> getProfilesByExistingAttribute(String tenantName, String attributeName, String sortBy, SortOrder sortOrder, String... attributesToReturn) throws ProfileException {
    checkIfManageProfilesIsAllowed(tenantName);
    try {
        List<Profile> profiles = IterableUtils.toList(profileRepository.findByTenantAndExistingAttribute(tenantName, attributeName, sortBy, sortOrder, attributesToReturn));
        filterNonReadableAttributes(profiles);
        return profiles;
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_GET_PROFILES_BY_EXISTING_ATTRIB_ERROR, e, attributeName, tenantName);
    }
}
Also used : I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) Profile(org.craftercms.profile.api.Profile)

Example 8 with Profile

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

the class ProfileServiceImpl method getProfilesByQuery.

@Override
public List<Profile> getProfilesByQuery(String tenantName, String query, String sortBy, SortOrder sortOrder, Integer start, Integer count, String... attributesToReturn) throws ProfileException {
    checkIfManageProfilesIsAllowed(tenantName);
    Tenant tenant = getTenant(tenantName);
    try {
        List<Profile> profiles = IterableUtils.toList(profileRepository.findByQuery(getFinalQuery(tenant, query), sortBy, sortOrder, start, count, attributesToReturn));
        filterNonReadableAttributes(tenant, profiles);
        return profiles;
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_GET_PROFILES_BY_QUERY_ERROR, e, tenant, query);
    }
}
Also used : Tenant(org.craftercms.profile.api.Tenant) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) Profile(org.craftercms.profile.api.Profile)

Example 9 with Profile

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

the class ProfileServiceImpl method createProfile.

@Override
public Profile createProfile(String tenantName, String username, String password, String email, boolean enabled, Set<String> roles, Map<String, Object> attributes, String verificationUrl) throws ProfileException {
    checkIfManageProfilesIsAllowed(tenantName);
    if (!EmailUtils.validateEmail(email)) {
        throw new InvalidEmailAddressException(email);
    }
    try {
        Tenant tenant = getTenant(tenantName);
        Date now = new Date();
        Profile profile = new Profile();
        profile.setTenant(tenantName);
        profile.setUsername(username);
        profile.setPassword(CryptoUtils.hashPassword(password));
        profile.setEmail(email);
        profile.setCreatedOn(now);
        profile.setLastModified(now);
        profile.setVerified(false);
        boolean emailNewProfiles = tenant.isVerifyNewProfiles();
        if (!emailNewProfiles || StringUtils.isEmpty(verificationUrl)) {
            profile.setEnabled(enabled);
        }
        if (CollectionUtils.isNotEmpty(roles)) {
            profile.setRoles(roles);
        }
        for (AttributeDefinition definition : tenant.getAttributeDefinitions()) {
            if (definition.getDefaultValue() != null) {
                profile.setAttribute(definition.getName(), definition.getDefaultValue());
            }
        }
        if (MapUtils.isNotEmpty(attributes)) {
            rejectAttributesIfActionNotAllowed(tenant, attributes.keySet(), AttributeAction.WRITE_ATTRIBUTE);
            profile.getAttributes().putAll(attributes);
        }
        profileRepository.insert(profile);
        logger.debug(LOG_KEY_PROFILE_CREATED, profile);
        if (emailNewProfiles && StringUtils.isNotEmpty(verificationUrl)) {
            VerificationToken token = verificationService.createToken(profile);
            verificationService.sendEmail(token, profile, verificationUrl, newProfileEmailFromAddress, newProfileEmailSubject, newProfileEmailTemplateName);
        }
        return profile;
    } catch (DuplicateKeyException e) {
        throw new ProfileExistsException(tenantName, username);
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_CREATE_PROFILE_ERROR, e, username, tenantName);
    }
}
Also used : ProfileExistsException(org.craftercms.profile.exceptions.ProfileExistsException) Tenant(org.craftercms.profile.api.Tenant) InvalidEmailAddressException(org.craftercms.profile.exceptions.InvalidEmailAddressException) VerificationToken(org.craftercms.profile.api.VerificationToken) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) AttributeDefinition(org.craftercms.profile.api.AttributeDefinition) MongoDataException(org.craftercms.commons.mongo.MongoDataException) Date(java.util.Date) Profile(org.craftercms.profile.api.Profile) DuplicateKeyException(org.craftercms.commons.mongo.DuplicateKeyException)

Example 10 with Profile

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

the class ProfileServiceImpl method setFailedLoginAttempts.

@Override
public Profile setFailedLoginAttempts(String profileId, final int failedAttempts, String... attributesToReturn) throws ProfileException {
    Profile profile = updateProfile(profileId, profileUpdater -> profileUpdater.setFailedLoginAttempts(failedAttempts), attributesToReturn);
    logger.debug(LOG_KEY_PROFILE_ENABLED, profileId);
    return profile;
}
Also used : Profile(org.craftercms.profile.api.Profile)

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