use of io.gravitee.management.service.builder.EmailNotificationBuilder in project gravitee-management-rest-api by gravitee-io.
the class MembershipServiceImpl method buildEmailNotification.
private EmailNotification buildEmailNotification(UserEntity user, MembershipReferenceType referenceType, String referenceId) {
String subject = null;
EmailNotificationBuilder.EmailTemplate template = null;
Map<String, Object> params = null;
GroupEntity groupEntity;
NotificationParamsBuilder paramsBuilder = new NotificationParamsBuilder();
switch(referenceType) {
case APPLICATION:
ApplicationEntity applicationEntity = applicationService.findById(referenceId);
subject = "Subscription to application " + applicationEntity.getName();
template = EmailNotificationBuilder.EmailTemplate.APPLICATION_MEMBER_SUBSCRIPTION;
params = paramsBuilder.application(applicationEntity).user(user).build();
break;
case API:
ApiEntity apiEntity = apiService.findById(referenceId);
subject = "Subscription to API " + apiEntity.getName();
template = EmailNotificationBuilder.EmailTemplate.API_MEMBER_SUBSCRIPTION;
params = paramsBuilder.api(apiEntity).user(user).build();
break;
case GROUP:
groupEntity = groupService.findById(referenceId);
subject = "Subscription to group " + groupEntity.getName();
template = EmailNotificationBuilder.EmailTemplate.GROUP_MEMBER_SUBSCRIPTION;
params = paramsBuilder.group(groupEntity).user(user).build();
break;
}
if (template == null) {
return null;
}
return new EmailNotificationBuilder().to(user.getEmail()).subject(subject).template(template).params(params).build();
}
use of io.gravitee.management.service.builder.EmailNotificationBuilder in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method register.
/**
* Allows to pre-create a user and send an email notification to finalize its creation.
*/
@Override
public UserEntity register(final NewExternalUserEntity newExternalUserEntity) {
checkUserRegistrationEnabled();
newExternalUserEntity.setUsername(newExternalUserEntity.getEmail());
newExternalUserEntity.setSource("gravitee");
newExternalUserEntity.setSourceId(newExternalUserEntity.getUsername());
final UserEntity userEntity = create(newExternalUserEntity, true);
// generate a JWT to store user's information and for security purpose
final Map<String, Object> claims = new HashMap<>();
claims.put(Claims.ISSUER, environment.getProperty("jwt.issuer", DEFAULT_JWT_ISSUER));
claims.put(Claims.SUBJECT, userEntity.getUsername());
claims.put(Claims.EMAIL, userEntity.getEmail());
claims.put(Claims.FIRSTNAME, userEntity.getFirstname());
claims.put(Claims.LASTNAME, userEntity.getLastname());
final JWTSigner.Options options = new JWTSigner.Options();
options.setExpirySeconds(environment.getProperty("user.creation.token.expire-after", Integer.class, DEFAULT_JWT_EMAIL_REGISTRATION_EXPIRE_AFTER));
options.setIssuedAt(true);
options.setJwtId(true);
// send a confirm email with the token
final String jwtSecret = environment.getProperty("jwt.secret");
if (jwtSecret == null || jwtSecret.isEmpty()) {
throw new IllegalStateException("JWT secret is mandatory");
}
final String token = new JWTSigner(jwtSecret).sign(claims, options);
String portalUrl = environment.getProperty("portalURL");
if (portalUrl.endsWith("/")) {
portalUrl = portalUrl.substring(0, portalUrl.length() - 1);
}
String registrationUrl = portalUrl + "/#!/registration/confirm/" + token;
final Map<String, Object> params = new NotificationParamsBuilder().user(userEntity).token(token).registrationUrl(registrationUrl).build();
notifierService.trigger(PortalHook.USER_REGISTERED, params);
emailService.sendAsyncEmailNotification(new EmailNotificationBuilder().to(userEntity.getEmail()).subject("User registration - " + userEntity.getUsername()).template(EmailNotificationBuilder.EmailTemplate.USER_REGISTRATION).params(params).build());
return userEntity;
}
use of io.gravitee.management.service.builder.EmailNotificationBuilder in project gravitee-management-rest-api by gravitee-io.
the class TicketServiceTest method shouldCreateWithApi.
@Test
public void shouldCreateWithApi() {
setField(ticketService, "enabled", true);
when(newTicketEntity.getApi()).thenReturn(API_ID);
when(newTicketEntity.getApplication()).thenReturn(APPLICATION_ID);
when(newTicketEntity.getSubject()).thenReturn(EMAIL_SUBJECT);
when(newTicketEntity.isCopyToSender()).thenReturn(EMAIL_COPY_TO_SENDER);
when(newTicketEntity.getContent()).thenReturn(EMAIL_CONTENT);
when(userService.findById(USERNAME)).thenReturn(user);
when(user.getEmail()).thenReturn(USER_EMAIL);
when(user.getFirstname()).thenReturn(USER_FIRSTNAME);
when(user.getLastname()).thenReturn(USER_LASTNAME);
when(apiService.findByIdForTemplates(API_ID)).thenReturn(api);
when(applicationService.findById(APPLICATION_ID)).thenReturn(application);
final Map<String, String> metadata = new HashMap<>();
metadata.put(METADATA_EMAIL_SUPPORT_KEY, EMAIL_SUPPORT);
when(api.getMetadata()).thenReturn(metadata);
ticketService.create(USERNAME, newTicketEntity);
verify(emailService).sendEmailNotification(new EmailNotificationBuilder().from(USER_EMAIL).fromName(USER_FIRSTNAME + ' ' + USER_LASTNAME).to(EMAIL_SUPPORT).subject(EMAIL_SUBJECT).copyToSender(EMAIL_COPY_TO_SENDER).template(SUPPORT_TICKET).params(ImmutableMap.of("user", user, "api", api, "content", "Email<br />Content", "application", application)).build());
}
use of io.gravitee.management.service.builder.EmailNotificationBuilder in project gravitee-management-rest-api by gravitee-io.
the class TicketServiceImpl method create.
@Override
public void create(final String userId, final NewTicketEntity ticketEntity) {
if (!enabled) {
throw new SupportUnavailableException();
}
LOGGER.info("Creating a support ticket: {}", ticketEntity);
final Map<String, Object> parameters = new HashMap<>();
final UserEntity user = userService.findById(userId);
if (user.getEmail() == null) {
throw new EmailRequiredException(userId);
}
parameters.put("user", user);
final String emailTo;
if (ticketEntity.getApi() == null) {
final MetadataEntity emailMetadata = metadataService.findDefaultByKey(METADATA_EMAIL_SUPPORT_KEY);
if (emailMetadata == null) {
throw new IllegalStateException("The support email metadata has not been found");
}
emailTo = emailMetadata.getValue();
} else {
final ApiModelEntity api = apiService.findByIdForTemplates(ticketEntity.getApi());
final String apiMetadataEmailSupport = api.getMetadata().get(METADATA_EMAIL_SUPPORT_KEY);
if (apiMetadataEmailSupport == null) {
throw new IllegalStateException("The support email API metadata has not been found");
}
emailTo = apiMetadataEmailSupport;
parameters.put("api", api);
}
if (DEFAULT_METADATA_EMAIL_SUPPORT.equals(emailTo)) {
throw new IllegalStateException("The support email API metadata has not been changed");
}
if (ticketEntity.getApplication() != null) {
parameters.put("application", applicationService.findById(ticketEntity.getApplication()));
}
parameters.put("content", ticketEntity.getContent().replaceAll("(\r\n|\n)", "<br />"));
emailService.sendEmailNotification(new EmailNotificationBuilder().from(user.getEmail()).fromName(user.getFirstname() + ' ' + user.getLastname()).to(emailTo).subject(ticketEntity.getSubject()).copyToSender(ticketEntity.isCopyToSender()).template(SUPPORT_TICKET).params(parameters).build());
}
Aggregations