Search in sources :

Example 6 with User

use of com.redhat.cloud.notifications.recipients.User in project notifications-backend by RedHatInsights.

the class TemplateEngineResource method render.

@PUT
@Path("/render")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response render(@NotNull @Valid RenderEmailTemplateRequest renderEmailTemplateRequest) {
    User user = createInternalUser();
    String payload = renderEmailTemplateRequest.getPayload();
    try {
        Action action = actionParser.fromJsonString(payload);
        TemplateInstance subjectTemplate = templateService.compileTemplate(renderEmailTemplateRequest.getSubjectTemplate(), "subject");
        String subject = templateService.renderTemplate(user, action, subjectTemplate);
        TemplateInstance bodyTemplate = templateService.compileTemplate(renderEmailTemplateRequest.getBodyTemplate(), "body");
        String body = templateService.renderTemplate(user, action, bodyTemplate);
        return Response.ok(new RenderEmailTemplateResponse.Success(subject, body)).build();
    } catch (Exception e) {
        return Response.status(Response.Status.BAD_REQUEST).entity(new RenderEmailTemplateResponse.Error(e.getMessage())).build();
    }
}
Also used : Action(com.redhat.cloud.notifications.ingress.Action) User(com.redhat.cloud.notifications.recipients.User) RenderEmailTemplateResponse(com.redhat.cloud.notifications.routers.models.RenderEmailTemplateResponse) TemplateInstance(io.quarkus.qute.TemplateInstance) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 7 with User

use of com.redhat.cloud.notifications.recipients.User in project notifications-backend by RedHatInsights.

the class TemplateEngineResource method createInternalUser.

private User createInternalUser() {
    User user = new User();
    user.setUsername("jdoe");
    user.setEmail("jdoe@jdoe.com");
    user.setFirstName("John");
    user.setLastName("Doe");
    user.setActive(true);
    user.setAdmin(false);
    return user;
}
Also used : User(com.redhat.cloud.notifications.recipients.User)

Example 8 with User

use of com.redhat.cloud.notifications.recipients.User in project notifications-backend by RedHatInsights.

the class RbacRecipientUsersProvider method getUsers.

@CacheResult(cacheName = "rbac-recipient-users-provider-get-users")
public List<User> getUsers(String accountId, String orgId, boolean adminsOnly) {
    Timer.Sample getUsersTotalTimer = Timer.start(meterRegistry);
    List<User> users;
    List<ITUserResponse> usersPaging;
    List<ITUserResponse> usersTotal = new ArrayList<>();
    int firstResult = 0;
    do {
        ITUserRequest request = new ITUserRequest(accountId, orgId, useOrgId, adminsOnly, firstResult, maxResultsPerPage);
        usersPaging = retryOnItError(() -> itUserService.getUsers(request));
        usersTotal.addAll(usersPaging);
        firstResult += maxResultsPerPage;
    } while (usersPaging.size() == maxResultsPerPage);
    LOGGER.info("usersTotal: " + usersTotal);
    users = transformToUser(usersTotal);
    // Micrometer doesn't like when tags are null and throws a NPE.
    String accountIdTag = accountId == null ? "" : accountId;
    getUsersTotalTimer.stop(meterRegistry.timer("rbac.get-users.total", "accountId", accountIdTag, "users", String.valueOf(users.size())));
    rbacUsers.set(users.size());
    return users;
}
Also used : User(com.redhat.cloud.notifications.recipients.User) Timer(io.micrometer.core.instrument.Timer) ArrayList(java.util.ArrayList) ITUserRequest(com.redhat.cloud.notifications.recipients.itservice.pojo.request.ITUserRequest) ITUserResponse(com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse) CacheResult(io.quarkus.cache.CacheResult)

Example 9 with User

use of com.redhat.cloud.notifications.recipients.User in project notifications-backend by RedHatInsights.

the class RbacRecipientUsersProvider method transformToUser.

List<User> transformToUser(List<ITUserResponse> itUserResponses) {
    List<User> users = new ArrayList<>();
    for (ITUserResponse itUserResponse : itUserResponses) {
        User user = new User();
        user.setUsername(itUserResponse.authentications.get(0).principal);
        final List<Email> emails = itUserResponse.accountRelationships.get(0).emails;
        for (Email email : emails) {
            if (email != null && email.isPrimary != null && email.isPrimary) {
                String address = email.address;
                user.setEmail(address);
            }
        }
        user.setAdmin(false);
        if (itUserResponse.accountRelationships != null) {
            for (AccountRelationship accountRelationship : itUserResponse.accountRelationships) {
                if (accountRelationship.permissions != null) {
                    for (Permission permission : accountRelationship.permissions) {
                        if (ORG_ADMIN_PERMISSION.equals(permission.permissionCode)) {
                            user.setAdmin(true);
                        }
                    }
                }
            }
        }
        user.setActive(true);
        user.setFirstName(itUserResponse.personalInformation.firstName);
        user.setLastName(itUserResponse.personalInformation.lastNames);
        users.add(user);
    }
    return users;
}
Also used : User(com.redhat.cloud.notifications.recipients.User) Email(com.redhat.cloud.notifications.recipients.itservice.pojo.response.Email) AccountRelationship(com.redhat.cloud.notifications.recipients.itservice.pojo.response.AccountRelationship) ArrayList(java.util.ArrayList) Permission(com.redhat.cloud.notifications.recipients.itservice.pojo.response.Permission) ITUserResponse(com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse)

Example 10 with User

use of com.redhat.cloud.notifications.recipients.User in project notifications-backend by RedHatInsights.

the class LifecycleITest method setupEmailMock.

private void setupEmailMock(String accountId, String orgId, String username) {
    Mockito.when(emailTemplateFactory.get(anyString(), anyString())).thenReturn(new Blank());
    User user = new User();
    user.setUsername(username);
    user.setAdmin(true);
    user.setActive(true);
    user.setEmail("user email");
    user.setFirstName("user firstname");
    user.setLastName("user lastname");
    Mockito.when(rbacRecipientUsersProvider.getUsers(eq(accountId), eq(orgId), eq(true))).thenReturn(List.of(user));
    updateField(emailSender, "bopUrl", getMockServerUrl() + EMAIL_SENDER_MOCK_PATH, EmailSender.class);
}
Also used : Blank(com.redhat.cloud.notifications.templates.Blank) User(com.redhat.cloud.notifications.recipients.User)

Aggregations

User (com.redhat.cloud.notifications.recipients.User)13 Action (com.redhat.cloud.notifications.ingress.Action)3 ITUserResponse (com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse)3 TemplateInstance (io.quarkus.qute.TemplateInstance)3 QuarkusTest (io.quarkus.test.junit.QuarkusTest)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Test (org.junit.jupiter.api.Test)3 EmailAggregationRepository (com.redhat.cloud.notifications.db.repositories.EmailAggregationRepository)2 EmailSubscriptionRepository (com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository)2 Context (com.redhat.cloud.notifications.ingress.Context)2 AggregationEmailTemplate (com.redhat.cloud.notifications.models.AggregationEmailTemplate)2 EmailAggregation (com.redhat.cloud.notifications.models.EmailAggregation)2 EmailAggregationKey (com.redhat.cloud.notifications.models.EmailAggregationKey)2 EmailSubscriptionType (com.redhat.cloud.notifications.models.EmailSubscriptionType)2 Endpoint (com.redhat.cloud.notifications.models.Endpoint)2 Event (com.redhat.cloud.notifications.models.Event)2 InstantEmailTemplate (com.redhat.cloud.notifications.models.InstantEmailTemplate)2 RecipientResolver (com.redhat.cloud.notifications.recipients.RecipientResolver)2 ITUserRequest (com.redhat.cloud.notifications.recipients.itservice.pojo.request.ITUserRequest)2