use of org.craftercms.profile.exceptions.InvalidEmailAddressException 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);
}
}
use of org.craftercms.profile.exceptions.InvalidEmailAddressException in project profile by craftercms.
the class ProfileServiceImpl method updateProfile.
@Override
public Profile updateProfile(final String profileId, final String username, final String password, final String email, final Boolean enabled, final Set<String> roles, final Map<String, Object> attributes, String... attributesToReturn) throws ProfileException {
if (StringUtils.isNotEmpty(email) && !EmailUtils.validateEmail(email)) {
throw new InvalidEmailAddressException(email);
}
Profile profile = updateProfile(profileId, profileUpdater -> {
if (StringUtils.isNotEmpty(username)) {
profileUpdater.setUsername(username);
}
if (StringUtils.isNotEmpty(password)) {
profileUpdater.setPassword(CryptoUtils.hashPassword(password));
}
if (StringUtils.isNotEmpty(email)) {
profileUpdater.setEmail(email);
}
if (enabled != null) {
profileUpdater.setEnabled(enabled);
}
if (roles != null) {
profileUpdater.setRoles(roles);
}
if (MapUtils.isNotEmpty(attributes)) {
String tenantName = profileUpdater.getProfile().getTenant();
rejectAttributesIfActionNotAllowed(tenantName, attributes.keySet(), AttributeAction.WRITE_ATTRIBUTE);
profileUpdater.addAttributes(attributes);
}
}, attributesToReturn);
logger.debug(LOG_KEY_PROFILE_UPDATED, profile);
return profile;
}
Aggregations