Search in sources :

Example 1 with VerificationToken

use of org.craftercms.profile.api.VerificationToken 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 2 with VerificationToken

use of org.craftercms.profile.api.VerificationToken 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 3 with VerificationToken

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

the class VerificationServiceImpl method createToken.

@Override
public VerificationToken createToken(Profile profile) throws ProfileException {
    String tenant = profile.getTenant();
    String profileId = profile.getId().toString();
    VerificationToken token = new VerificationToken();
    token.setId(UUID.randomUUID().toString());
    token.setTenant(tenant);
    token.setProfileId(profileId);
    token.setTimestamp(new Date());
    try {
        tokenRepository.insert(token);
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_CREATE_TOKEN_ERROR, profileId);
    }
    logger.debug(LOG_KEY_TOKEN_CREATED, profileId, token);
    return token;
}
Also used : VerificationToken(org.craftercms.profile.api.VerificationToken) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) Date(java.util.Date)

Example 4 with VerificationToken

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

the class ProfileServiceImplTest method testGetVerificationToken.

@Test
public void testGetVerificationToken() throws Exception {
    VerificationToken token = profileService.getVerificationToken(VERIFICATION_TOKEN_ID1);
    assertNotNull(token);
    assertEquals(VERIFICATION_TOKEN_ID1, token.getId());
    assertEquals(TENANT1_NAME, token.getTenant());
    assertEquals(PROFILE1_ID.toString(), token.getProfileId());
    assertNotNull(token.getTimestamp());
    verify(verificationService).getToken(VERIFICATION_TOKEN_ID1);
}
Also used : VerificationToken(org.craftercms.profile.api.VerificationToken) Test(org.junit.Test)

Example 5 with VerificationToken

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

the class ProfileServiceImplTest method testCreateProfile.

@Test
public void testCreateProfile() throws Exception {
    Profile expected = getTenant2Profile();
    expected.setTenant(TENANT1_NAME);
    expected.setAttributes(getAttributesWithoutPrivateAttribute());
    expected.setAttribute(ATTRIB_NAME_GENDER, GENDER);
    Profile actual = profileService.createProfile(TENANT1_NAME, USERNAME2, PASSWORD2, EMAIL2, true, ROLES2, getAttributesWithoutPrivateAttribute(), VERIFICATION_URL);
    assertEqualProfiles(expected, actual);
    assertTrue(CryptoUtils.matchPassword(actual.getPassword(), PASSWORD2));
    assertNotNull(actual.getCreatedOn());
    assertNotNull(actual.getLastModified());
    VerificationToken token = new VerificationToken();
    token.setId(VERIFICATION_TOKEN_ID1);
    verify(tenantPermissionEvaluator).isAllowed(TENANT1_NAME, TenantAction.MANAGE_PROFILES.toString());
    verify(tenantService).getTenant(TENANT1_NAME);
    verify(profileRepository).insert(actual);
    verify(verificationService).createToken(actual);
    verify(verificationService).sendEmail(token, actual, VERIFICATION_URL, VERIFICATION_FROM_ADDRESS, VERIFICATION_SUBJECT, VERIFICATION_TEMPLATE_NAME);
}
Also used : VerificationToken(org.craftercms.profile.api.VerificationToken) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test)

Aggregations

VerificationToken (org.craftercms.profile.api.VerificationToken)18 Profile (org.craftercms.profile.api.Profile)12 Test (org.junit.Test)10 Date (java.util.Date)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 MongoDataException (org.craftercms.commons.mongo.MongoDataException)2 AttributeDefinition (org.craftercms.profile.api.AttributeDefinition)2 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)2 NoSuchVerificationTokenException (org.craftercms.profile.exceptions.NoSuchVerificationTokenException)2 Before (org.junit.Before)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 ObjectId (org.bson.types.ObjectId)1 DuplicateKeyException (org.craftercms.commons.mongo.DuplicateKeyException)1 Tenant (org.craftercms.profile.api.Tenant)1 InvalidEmailAddressException (org.craftercms.profile.exceptions.InvalidEmailAddressException)1 ProfileExistsException (org.craftercms.profile.exceptions.ProfileExistsException)1