Search in sources :

Example 1 with Email

use of io.gravitee.am.common.email.Email in project gravitee-access-management by gravitee-io.

the class EmailServiceImpl method send.

@Override
public void send(io.gravitee.am.model.Template template, User user, Client client) {
    if (enabled) {
        // get raw email template
        io.gravitee.am.model.Email emailTemplate = getEmailTemplate(template, client);
        // prepare email
        Email email = prepareEmail(template, emailTemplate, user, client);
        // send email
        sendEmail(email, user, client);
    }
}
Also used : Email(io.gravitee.am.common.email.Email)

Example 2 with Email

use of io.gravitee.am.common.email.Email in project gravitee-access-management by gravitee-io.

the class EmailServiceImpl method sendEmail.

private void sendEmail(Email email, User user, Client client) {
    try {
        final Template template = freemarkerConfiguration.getTemplate(email.getTemplate());
        final Template plainTextTemplate = new Template("subject", new StringReader(email.getSubject()), freemarkerConfiguration);
        // compute email subject
        final String subject = processTemplateIntoString(plainTextTemplate, email.getParams());
        // compute email content
        final String content = processTemplateIntoString(template, email.getParams());
        final Email emailToSend = new Email(email);
        emailToSend.setSubject(subject);
        emailToSend.setContent(content);
        emailService.send(emailToSend);
        auditService.report(AuditBuilder.builder(EmailAuditBuilder.class).domain(domain.getId()).client(client).email(email).user(user));
    } catch (final Exception ex) {
        auditService.report(AuditBuilder.builder(EmailAuditBuilder.class).domain(domain.getId()).client(client).email(email).throwable(ex));
    }
}
Also used : EmailAuditBuilder(io.gravitee.am.service.reporter.builder.EmailAuditBuilder) Email(io.gravitee.am.common.email.Email) StringReader(java.io.StringReader) FreeMarkerTemplateUtils.processTemplateIntoString(org.springframework.ui.freemarker.FreeMarkerTemplateUtils.processTemplateIntoString) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 3 with Email

use of io.gravitee.am.common.email.Email in project gravitee-access-management by gravitee-io.

the class EmailServiceImpl method createEmail.

@Override
public EmailWrapper createEmail(io.gravitee.am.model.Template template, Client client, List<String> recipients, Map<String, Object> params) throws IOException, TemplateException {
    io.gravitee.am.model.Email emailTpl = getEmailTemplate(template, client);
    params.put("expireAfterSeconds", emailTpl.getExpiresAfter());
    final long expiresAt = Instant.now().plus(emailTpl.getExpiresAfter(), ChronoUnit.SECONDS).toEpochMilli();
    params.put("expireAt", expiresAt);
    io.gravitee.am.common.email.Email email = new EmailBuilder().from(emailTpl.getFrom()).fromName(emailTpl.getFromName()).template(emailTpl.getTemplate()).to(recipients.toArray(new String[recipients.size()])).build();
    // compute email subject
    final Template plainTextTemplate = new Template("subject", new StringReader(emailTpl.getSubject()), freemarkerConfiguration);
    email.setSubject(processTemplateIntoString(plainTextTemplate, params));
    // compute email content
    final Template subjectTemplate = freemarkerConfiguration.getTemplate(email.getTemplate());
    email.setContent(processTemplateIntoString(subjectTemplate, params));
    EmailWrapper wrapper = new EmailWrapper(email);
    wrapper.setExpireAt(expiresAt);
    return wrapper;
}
Also used : Email(io.gravitee.am.common.email.Email) StringReader(java.io.StringReader) EmailBuilder(io.gravitee.am.common.email.EmailBuilder) Template(freemarker.template.Template)

Example 4 with Email

use of io.gravitee.am.common.email.Email in project gravitee-access-management by gravitee-io.

the class EmailServiceImpl method prepareEmail.

private Email prepareEmail(io.gravitee.am.model.Template template, io.gravitee.am.model.Email emailTemplate, User user, Client client) {
    Map<String, Object> params = prepareEmailParams(user, client, emailTemplate.getExpiresAfter(), template.redirectUri());
    Email email = new EmailBuilder().to(user.getEmail()).from(emailTemplate.getFrom()).fromName(emailTemplate.getFromName()).subject(emailTemplate.getSubject()).template(emailTemplate.getTemplate()).params(params).build();
    return email;
}
Also used : Email(io.gravitee.am.common.email.Email) FreeMarkerTemplateUtils.processTemplateIntoString(org.springframework.ui.freemarker.FreeMarkerTemplateUtils.processTemplateIntoString) EmailBuilder(io.gravitee.am.common.email.EmailBuilder)

Example 5 with Email

use of io.gravitee.am.common.email.Email in project gravitee-access-management by gravitee-io.

the class DomainNotificationServiceTest method shouldNotifyUserFromGroup.

@Test
public void shouldNotifyUserFromGroup() throws Exception {
    final Membership member = new Membership();
    member.setMemberType(MemberType.GROUP);
    member.setMemberId("groupId");
    when(membershipService.findByCriteria(eq(ReferenceType.DOMAIN), eq(DOMAIN_ID), any())).thenReturn(Flowable.just(member), Flowable.empty());
    final List<User> tenUsers = IntStream.range(0, 10).mapToObj(x -> {
        final User user = new User();
        user.setId("" + x);
        user.setEmail(x + "@acme.fr");
        return user;
    }).collect(Collectors.toList());
    final User singleUser = new User();
    singleUser.setId("single");
    singleUser.setEmail("single@acme.fr");
    when(groupService.findMembers(any(), any(), any(), anyInt(), anyInt())).thenReturn(Single.just(new Page<>(tenUsers, 0, 11)), Single.just(new Page<>(Arrays.asList(singleUser), 1, 11)));
    when(emailService.getFinalEmail(any(), any(), any(), any(), any())).thenReturn(new Email());
    cut.registerCertificateExpiration(certificate);
    // wait subscription execution
    Thread.sleep(1000);
    verify(notifierService, times(11)).register(any(), any(), any());
    verify(userService, never()).findById(any(), any(), any());
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Certificate(io.gravitee.am.model.Certificate) EnvironmentService(io.gravitee.am.service.EnvironmentService) DomainNotifierServiceImpl(io.gravitee.am.management.service.impl.DomainNotifierServiceImpl) Arrays(java.util.Arrays) SystemRole(io.gravitee.am.model.permissions.SystemRole) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Role(io.gravitee.am.model.Role) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) DomainService(io.gravitee.am.service.DomainService) Environment(io.gravitee.am.model.Environment) Mockito.argThat(org.mockito.Mockito.argThat) Single(io.reactivex.Single) Flowable(io.reactivex.Flowable) After(org.junit.After) Email(io.gravitee.am.common.email.Email) ReferenceType(io.gravitee.am.model.ReferenceType) User(io.gravitee.am.model.User) GroupService(io.gravitee.am.service.GroupService) MembershipService(io.gravitee.am.service.MembershipService) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) RoleService(io.gravitee.am.service.RoleService) NotifierService(io.gravitee.node.api.notifier.NotifierService) Page(io.gravitee.am.model.common.Page) MemberType(io.gravitee.am.model.membership.MemberType) TYPE_EMAIL_NOTIFIER(io.gravitee.am.management.service.impl.notifications.NotificationDefinitionUtils.TYPE_EMAIL_NOTIFIER) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Domain(io.gravitee.am.model.Domain) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) Membership(io.gravitee.am.model.Membership) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) List(java.util.List) Mockito.never(org.mockito.Mockito.never) OrganizationUserService(io.gravitee.am.service.OrganizationUserService) Mockito.anyInt(org.mockito.Mockito.anyInt) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) EmailNotifierConfiguration(io.gravitee.am.management.service.impl.notifications.EmailNotifierConfiguration) TYPE_UI_NOTIFIER(io.gravitee.am.management.service.impl.notifications.NotificationDefinitionUtils.TYPE_UI_NOTIFIER) User(io.gravitee.am.model.User) Email(io.gravitee.am.common.email.Email) Membership(io.gravitee.am.model.Membership) Page(io.gravitee.am.model.common.Page) Test(org.junit.Test)

Aggregations

Email (io.gravitee.am.common.email.Email)13 Template (freemarker.template.Template)3 TemplateException (freemarker.template.TemplateException)3 EmailBuilder (io.gravitee.am.common.email.EmailBuilder)3 Membership (io.gravitee.am.model.Membership)3 User (io.gravitee.am.model.User)3 IOException (java.io.IOException)3 StringReader (java.io.StringReader)3 FreeMarkerTemplateUtils.processTemplateIntoString (org.springframework.ui.freemarker.FreeMarkerTemplateUtils.processTemplateIntoString)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DomainNotifierServiceImpl (io.gravitee.am.management.service.impl.DomainNotifierServiceImpl)2 EmailNotifierConfiguration (io.gravitee.am.management.service.impl.notifications.EmailNotifierConfiguration)2 TYPE_EMAIL_NOTIFIER (io.gravitee.am.management.service.impl.notifications.NotificationDefinitionUtils.TYPE_EMAIL_NOTIFIER)2 TYPE_UI_NOTIFIER (io.gravitee.am.management.service.impl.notifications.NotificationDefinitionUtils.TYPE_UI_NOTIFIER)2 Certificate (io.gravitee.am.model.Certificate)2 Domain (io.gravitee.am.model.Domain)2 Environment (io.gravitee.am.model.Environment)2 ReferenceType (io.gravitee.am.model.ReferenceType)2 Role (io.gravitee.am.model.Role)2 Page (io.gravitee.am.model.common.Page)2