use of io.gravitee.rest.api.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) {
EmailNotificationBuilder.EmailTemplate template = null;
Map<String, Object> params = null;
GroupEntity groupEntity;
NotificationParamsBuilder paramsBuilder = new NotificationParamsBuilder();
switch(referenceType) {
case APPLICATION:
ApplicationEntity applicationEntity = applicationService.findById(referenceId);
template = EmailNotificationBuilder.EmailTemplate.TEMPLATES_FOR_ACTION_APPLICATION_MEMBER_SUBSCRIPTION;
params = paramsBuilder.application(applicationEntity).user(user).build();
break;
case API:
ApiEntity apiEntity = apiService.findById(referenceId);
template = EmailNotificationBuilder.EmailTemplate.TEMPLATES_FOR_ACTION_API_MEMBER_SUBSCRIPTION;
params = paramsBuilder.api(apiEntity).user(user).build();
break;
case GROUP:
groupEntity = groupService.findById(referenceId);
template = EmailNotificationBuilder.EmailTemplate.TEMPLATES_FOR_ACTION_GROUP_MEMBER_SUBSCRIPTION;
params = paramsBuilder.group(groupEntity).user(user).build();
break;
default:
break;
}
if (template == null) {
return null;
}
return new EmailNotificationBuilder().to(user.getEmail()).template(template).params(params).build();
}
use of io.gravitee.rest.api.service.builder.EmailNotificationBuilder in project gravitee-management-rest-api by gravitee-io.
the class TicketServiceTest method shouldCreateWithApi.
@Test
public void shouldCreateWithApi() throws TechnicalException {
when(mockParameterService.findAsBoolean(Key.CONSOLE_SUPPORT_ENABLED, REFERENCE_ID, REFERENCE_TYPE)).thenReturn(Boolean.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, true)).thenReturn(api);
when(applicationService.findById(APPLICATION_ID)).thenReturn(application);
Ticket ticketToCreate = new Ticket();
ticketToCreate.setId("generatedId");
ticketToCreate.setApi(API_ID);
ticketToCreate.setApplication(APPLICATION_ID);
ticketToCreate.setSubject(EMAIL_SUBJECT);
ticketToCreate.setContent(EMAIL_CONTENT);
ticketToCreate.setCreatedAt(new Date());
ticketToCreate.setFromUser(USERNAME);
when(ticketRepository.create(any(Ticket.class))).thenReturn(ticketToCreate);
final Map<String, String> metadata = new HashMap<>();
metadata.put(DefaultMetadataUpgrader.METADATA_EMAIL_SUPPORT_KEY, EMAIL_SUPPORT);
when(api.getMetadata()).thenReturn(metadata);
TicketEntity createdTicket = ticketService.create(USERNAME, newTicketEntity, REFERENCE_ID, REFERENCE_TYPE);
verify(emailService).sendEmailNotification(new EmailNotificationBuilder().replyTo(USER_EMAIL).fromName(USER_FIRSTNAME + ' ' + USER_LASTNAME).to(EMAIL_SUPPORT).copyToSender(EMAIL_COPY_TO_SENDER).template(TEMPLATES_FOR_ACTION_SUPPORT_TICKET).params(ImmutableMap.of("user", user, "api", api, "content", "Email<br />Content", "application", application, "ticketSubject", EMAIL_SUBJECT)).build());
verify(mockNotifierService, times(1)).trigger(eq(PortalHook.NEW_SUPPORT_TICKET), anyMap());
assertEquals("Invalid saved ticket id", createdTicket.getId(), ticketToCreate.getId());
assertEquals("Invalid saved ticket api", createdTicket.getApi(), ticketToCreate.getApi());
assertEquals("Invalid saved ticket application", createdTicket.getApplication(), ticketToCreate.getApplication());
assertEquals("Invalid saved ticket subject", createdTicket.getSubject(), ticketToCreate.getSubject());
assertEquals("Invalid saved ticket content", createdTicket.getContent(), ticketToCreate.getContent());
assertEquals("Invalid saved ticket from user", createdTicket.getFromUser(), ticketToCreate.getFromUser());
assertEquals("Invalid saved ticket created at", createdTicket.getCreatedAt(), ticketToCreate.getCreatedAt());
}
use of io.gravitee.rest.api.service.builder.EmailNotificationBuilder in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method createAndSendEmail.
/**
* Allows to create an user and send an email notification to finalize its creation.
*/
private UserEntity createAndSendEmail(final NewExternalUserEntity newExternalUserEntity, final ACTION action, final String confirmationPageUrl, final boolean autoRegistrationEnabled) {
if (!EmailValidator.isValid(newExternalUserEntity.getEmail())) {
throw new EmailFormatInvalidException(newExternalUserEntity.getEmail());
}
String organizationId = GraviteeContext.getCurrentOrganization();
if (isBlank(newExternalUserEntity.getSource())) {
newExternalUserEntity.setSource(IDP_SOURCE_GRAVITEE);
} else {
if (!IDP_SOURCE_GRAVITEE.equals(newExternalUserEntity.getSource())) {
// check if IDP exists
identityProviderService.findById(newExternalUserEntity.getSource());
}
}
if (isBlank(newExternalUserEntity.getSourceId())) {
newExternalUserEntity.setSourceId(newExternalUserEntity.getEmail());
}
final Optional<User> optionalUser;
try {
optionalUser = userRepository.findBySource(newExternalUserEntity.getSource(), newExternalUserEntity.getSourceId(), organizationId);
if (optionalUser.isPresent()) {
throw new UserAlreadyExistsException(newExternalUserEntity.getSource(), newExternalUserEntity.getSourceId(), organizationId);
}
} catch (final TechnicalException e) {
LOGGER.error("An error occurs while trying to create user {} / {}", newExternalUserEntity.getSource(), newExternalUserEntity.getSourceId(), e);
throw new TechnicalManagementException(e.getMessage(), e);
}
final UserEntity userEntity = create(newExternalUserEntity, true, autoRegistrationEnabled);
if (IDP_SOURCE_GRAVITEE.equals(newExternalUserEntity.getSource())) {
final Map<String, Object> params = getTokenRegistrationParams(userEntity, REGISTRATION_PATH, action, confirmationPageUrl);
emailService.sendAsyncEmailNotification(new EmailNotificationBuilder().to(userEntity.getEmail()).template(EmailNotificationBuilder.EmailTemplate.TEMPLATES_FOR_ACTION_USER_REGISTRATION).params(params).param("registrationAction", USER_REGISTRATION.equals(action) ? "registration" : "creation").build(), GraviteeContext.getCurrentContext());
if (autoRegistrationEnabled) {
notifierService.trigger(ACTION.USER_REGISTRATION.equals(action) ? PortalHook.USER_REGISTERED : PortalHook.USER_CREATED, params);
} else {
notifierService.trigger(PortalHook.USER_REGISTRATION_REQUEST, params);
}
}
if (newExternalUserEntity.getNewsletter() != null && newExternalUserEntity.getNewsletter()) {
newsletterService.subscribe(newExternalUserEntity.getEmail());
}
return userEntity;
}
use of io.gravitee.rest.api.service.builder.EmailNotificationBuilder in project gravitee-management-rest-api by gravitee-io.
the class MessageServiceImpl method send.
private int send(Api api, MessageEntity message, Set<String> recipientsId) {
switch(message.getChannel()) {
case MAIL:
Set<String> mails = getRecipientsEmails(recipientsId);
if (!mails.isEmpty()) {
emailService.sendAsyncEmailNotification(new EmailNotificationBuilder().to(EmailService.DEFAULT_MAIL_TO).bcc(mails.toArray(new String[0])).template(EmailNotificationBuilder.EmailTemplate.TEMPLATES_FOR_ACTION_GENERIC_MESSAGE).param("message", message.getText()).param("messageSubject", message.getTitle()).build(), GraviteeContext.getCurrentContext());
}
return mails.size();
case PORTAL:
Hook hook = api == null ? PortalHook.MESSAGE : ApiHook.MESSAGE;
portalNotificationService.create(hook, new ArrayList<>(recipientsId), getPortalParams(api, message));
return recipientsId.size();
case HTTP:
if (!httpEnabled) {
throw new NotifierDisabledException();
}
String url = recipientsId.iterator().next();
environment.getProperty("notifiers.webhook.whitelist");
if (httpWhitelist != null && !httpWhitelist.isEmpty()) {
// Check the provided url is allowed.
if (httpWhitelist.stream().noneMatch(whitelistUrl -> whitelistUrl.endsWith("/") ? url.startsWith(whitelistUrl) : (url.equals(whitelistUrl) || url.startsWith(whitelistUrl + '/')))) {
throw new MessageUrlForbiddenException();
}
}
httpClientService.request(HttpMethod.POST, url, message.getParams(), getPostMessage(api, message), Boolean.valueOf(message.isUseSystemProxy()));
return 1;
default:
return 0;
}
}
Aggregations