Search in sources :

Example 1 with DuplicateKeyException

use of org.craftercms.commons.mongo.DuplicateKeyException 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 2 with DuplicateKeyException

use of org.craftercms.commons.mongo.DuplicateKeyException in project profile by craftercms.

the class TenantServiceImpl method createTenant.

@Override
public Tenant createTenant(Tenant tenant) throws ProfileException {
    checkIfTenantActionIsAllowed(null, TenantAction.CREATE_TENANT);
    // Make sure ID is null, we want it auto-generated
    tenant.setId(null);
    try {
        tenantRepository.insert(tenant);
    } catch (DuplicateKeyException e) {
        throw new TenantExistsException(tenant.getName());
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_CREATE_TENANT_ERROR, e, tenant.getName());
    }
    logger.debug(LOG_KEY_TENANT_CREATED, tenant);
    return tenant;
}
Also used : I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) TenantExistsException(org.craftercms.profile.exceptions.TenantExistsException) DuplicateKeyException(org.craftercms.commons.mongo.DuplicateKeyException)

Example 3 with DuplicateKeyException

use of org.craftercms.commons.mongo.DuplicateKeyException in project profile by craftercms.

the class AccessTokenServiceImpl method createToken.

@Override
public AccessToken createToken(AccessToken token) throws ProfileException {
    checkIfTokenActionIsAllowed(null, Action.CREATE_TOKEN);
    if (token.getId() == null) {
        token.setId(UUID.randomUUID().toString());
    }
    try {
        accessTokenRepository.insert(token);
    } catch (DuplicateKeyException e) {
        throw new AccessTokenExistsException(token.getId());
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_CREATE_ACCESS_TOKEN_ERROR, e, token);
    }
    logger.debug(LOG_KEY_ACCESS_TOKEN_CREATED, token);
    return token;
}
Also used : AccessTokenExistsException(org.craftercms.profile.exceptions.AccessTokenExistsException) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) DuplicateKeyException(org.craftercms.commons.mongo.DuplicateKeyException)

Aggregations

DuplicateKeyException (org.craftercms.commons.mongo.DuplicateKeyException)3 MongoDataException (org.craftercms.commons.mongo.MongoDataException)3 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)3 Date (java.util.Date)1 AttributeDefinition (org.craftercms.profile.api.AttributeDefinition)1 Profile (org.craftercms.profile.api.Profile)1 Tenant (org.craftercms.profile.api.Tenant)1 VerificationToken (org.craftercms.profile.api.VerificationToken)1 AccessTokenExistsException (org.craftercms.profile.exceptions.AccessTokenExistsException)1 InvalidEmailAddressException (org.craftercms.profile.exceptions.InvalidEmailAddressException)1 ProfileExistsException (org.craftercms.profile.exceptions.ProfileExistsException)1 TenantExistsException (org.craftercms.profile.exceptions.TenantExistsException)1