Search in sources :

Example 1 with User

use of org.rembx.jeeshop.user.model.User in project jeeshop by remibantos.

the class Orders method find.

@GET
@Path("/{orderId}")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ ADMIN, ADMIN_READONLY, USER })
public Order find(@PathParam("orderId") @NotNull Long orderId, @QueryParam("enhanced") Boolean enhanced) {
    Order order = entityManager.find(Order.class, orderId);
    if (sessionContext.isCallerInRole(USER) && !sessionContext.isCallerInRole(ADMIN)) {
        User authenticatedUser = userFinder.findByLogin(sessionContext.getCallerPrincipal().getName());
        if (!order.getUser().equals(authenticatedUser)) {
            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }
    }
    if (enhanced != null && enhanced) {
        orderFinder.enhanceOrder(order);
    }
    checkNotNull(order);
    return order;
}
Also used : Order(org.rembx.jeeshop.order.model.Order) User(org.rembx.jeeshop.user.model.User) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 2 with User

use of org.rembx.jeeshop.user.model.User in project jeeshop by remibantos.

the class Orders method find.

@GET
@Path("/{orderId}")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({ ADMIN, ADMIN_READONLY, USER })
public Order find(@Context SecurityContext securityContext, @PathParam("orderId") @NotNull Long orderId, @QueryParam("enhanced") Boolean enhanced) {
    Order order = entityManager.find(Order.class, orderId);
    if (securityContext.isUserInRole(USER) && !securityContext.isUserInRole(ADMIN)) {
        User authenticatedUser = userFinder.findByLogin(securityContext.getUserPrincipal().getName());
        if (!order.getUser().equals(authenticatedUser)) {
            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }
    }
    if (enhanced != null && enhanced) {
        orderFinder.enhanceOrder(order);
    }
    checkNotNull(order);
    return order;
}
Also used : Order(org.rembx.jeeshop.order.model.Order) User(org.rembx.jeeshop.user.model.User) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) RolesAllowed(javax.annotation.security.RolesAllowed)

Example 3 with User

use of org.rembx.jeeshop.user.model.User in project jeeshop by remibantos.

the class UserFinder method findAll.

public List<User> findAll(Integer offset, Integer limit, String orderBy, Boolean isDesc) {
    JPAQuery<User> query = new JPAQueryFactory(entityManager).selectFrom(user);
    if (offset != null)
        query.offset(offset);
    if (limit != null)
        query.limit(limit);
    sortBy(orderBy, isDesc, query);
    return query.fetch();
}
Also used : User(org.rembx.jeeshop.user.model.User) JPAQueryFactory(com.querydsl.jpa.impl.JPAQueryFactory)

Example 4 with User

use of org.rembx.jeeshop.user.model.User in project jeeshop by remibantos.

the class OrderFinder method enhanceOrder.

/**
 * Enhance given order with Catalog items data and order static configuration.
 *
 * @param order the order to enhance
 */
public void enhanceOrder(Order order) {
    User user = order.getUser();
    order.getItems().forEach(orderItem -> {
        Product product = catalogEntityManager.find(Product.class, orderItem.getProductId());
        SKU sku = catalogEntityManager.find(SKU.class, orderItem.getSkuId());
        product.setLocalizedPresentation(user.getPreferredLocale());
        orderItem.setDisplayName(product.getLocalizedPresentation() != null ? product.getLocalizedPresentation().getDisplayName() : product.getName());
        orderItem.setSkuReference(sku.getReference());
        if (product.getLocalizedPresentation() != null && product.getLocalizedPresentation().getSmallImage() != null)
            orderItem.setPresentationImageURI("products/" + orderItem.getProductId() + "/" + product.getLocalizedPresentation().getLocale() + "/" + product.getLocalizedPresentation().getSmallImage().getUri());
    });
    order.getOrderDiscounts().forEach(orderDiscount -> {
        Discount discount = catalogEntityManager.find(Discount.class, orderDiscount.getDiscountId());
        discount.setLocalizedPresentation(user.getPreferredLocale());
        orderDiscount.setDisplayName(discount.getLocalizedPresentation().getDisplayName() != null ? discount.getLocalizedPresentation().getDisplayName() : discount.getName());
        orderDiscount.setRateType(discount.getRateType());
        if (discount.getLocalizedPresentation() != null && discount.getLocalizedPresentation().getSmallImage() != null)
            orderDiscount.setPresentationImageURI("discounts/" + orderDiscount.getDiscountId() + "/" + discount.getLocalizedPresentation().getLocale() + "/" + discount.getLocalizedPresentation().getSmallImage().getUri());
    });
    order.setDeliveryFee(orderConfiguration.getFixedDeliveryFee());
    order.setVat(orderConfiguration.getVAT());
}
Also used : User(org.rembx.jeeshop.user.model.User) Discount(org.rembx.jeeshop.catalog.model.Discount) Product(org.rembx.jeeshop.catalog.model.Product) SKU(org.rembx.jeeshop.catalog.model.SKU)

Example 5 with User

use of org.rembx.jeeshop.user.model.User in project jeeshop by remibantos.

the class UsersCT method resetPassword_shouldUpdateUserPasswordWhenActionTokenMatchesUserActionToken.

@Test
public void resetPassword_shouldUpdateUserPasswordWhenActionTokenMatchesUserActionToken() throws Exception {
    User user = notActivatedTestUser();
    service.resetPassword(sessionContextMock, user.getLogin(), user.getActionToken().toString(), "newPassword");
    final User updatedUser = entityManager.find(User.class, user.getId());
    assertThat(updatedUser).isNotNull();
    assertThat(updatedUser.getPassword()).isEqualTo(hashSha256Base64("newPassword"));
    assertThat(updatedUser.getActionToken()).isNull();
    verify(mailerMock).sendMail(testMailTemplate.changePasswordMailTpl().getSubject(), user.getLogin(), testMailTemplate.changePasswordMailTpl().getContent());
    removeTestUser(user);
}
Also used : TestUser(org.rembx.jeeshop.user.test.TestUser) User(org.rembx.jeeshop.user.model.User) Test(org.junit.jupiter.api.Test)

Aggregations

User (org.rembx.jeeshop.user.model.User)28 Test (org.junit.jupiter.api.Test)18 TestUser (org.rembx.jeeshop.user.test.TestUser)18 Date (java.util.Date)9 WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)9 RolesAllowed (javax.annotation.security.RolesAllowed)4 BasicUserPrincipal (org.apache.http.auth.BasicUserPrincipal)3 Address (org.rembx.jeeshop.user.model.Address)3 UUID (java.util.UUID)2 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 Order (org.rembx.jeeshop.order.model.Order)2 JPAQueryFactory (com.querydsl.jpa.impl.JPAQueryFactory)1 Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1 StringWriter (java.io.StringWriter)1 Discount (org.rembx.jeeshop.catalog.model.Discount)1 Product (org.rembx.jeeshop.catalog.model.Product)1 SKU (org.rembx.jeeshop.catalog.model.SKU)1 MailTemplate (org.rembx.jeeshop.user.model.MailTemplate)1