Skip to content

Commit c14336f

Browse files
authored
Merge pull request #521 from WISVCH/fix-sales
Fix sales
2 parents 10cb8c0 + 0318f36 commit c14336f

16 files changed

Lines changed: 510 additions & 50 deletions

File tree

src/main/java/ch/wisv/events/core/repository/EventRepository.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import ch.wisv.events.core.model.event.Event;
55
import ch.wisv.events.core.model.event.EventStatus;
66
import ch.wisv.events.core.model.product.Product;
7+
import ch.wisv.events.utils.LdapGroup;
78

89
import java.time.LocalDateTime;
10+
import java.util.Collection;
911
import java.util.List;
1012
import java.util.Optional;
1113

@@ -36,6 +38,36 @@ public interface EventRepository extends JpaRepository<Event, Integer> {
3638
*/
3739
List<Event> findAllByPublishedAndEndingIsAfter(EventStatus published, LocalDateTime ending);
3840

41+
/**
42+
* Find all sales-visible events (upcoming from start of day and published/not published).
43+
*
44+
* @param dateTime of type LocalDateTime
45+
* @param statuses of type EventStatus collection
46+
*
47+
* @return list of events
48+
*/
49+
@Query("select e from Event e where e.ending >= :dateTime and e.published in :statuses order by e.start asc")
50+
List<Event> findAllSalesVisibleEvents(
51+
@Param("dateTime") LocalDateTime dateTime,
52+
@Param("statuses") Collection<EventStatus> statuses
53+
);
54+
55+
/**
56+
* Find all sales-visible events for specific organizing LDAP groups.
57+
*
58+
* @param dateTime of type LocalDateTime
59+
* @param statuses of type EventStatus collection
60+
* @param groups of type LdapGroup collection
61+
*
62+
* @return list of events
63+
*/
64+
@Query("select e from Event e where e.ending >= :dateTime and e.published in :statuses and e.organizedBy in :groups order by e.start asc")
65+
List<Event> findAllSalesVisibleEventsByOrganizedByIn(
66+
@Param("dateTime") LocalDateTime dateTime,
67+
@Param("statuses") Collection<EventStatus> statuses,
68+
@Param("groups") Collection<LdapGroup> groups
69+
);
70+
3971
/**
4072
* Find an Event by key.
4173
*

src/main/java/ch/wisv/events/sales/controller/scan/SalesScanEventController.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package ch.wisv.events.sales.controller.scan;
22

33
import ch.wisv.events.core.exception.normal.EventNotFoundException;
4+
import ch.wisv.events.core.model.customer.Customer;
45
import ch.wisv.events.core.model.event.Event;
6+
import ch.wisv.events.core.service.auth.AuthenticationService;
57
import ch.wisv.events.core.service.event.EventService;
8+
import ch.wisv.events.sales.service.SalesService;
69
import org.springframework.security.access.prepost.PreAuthorize;
710
import org.springframework.stereotype.Controller;
811
import org.springframework.ui.Model;
@@ -30,14 +33,26 @@ public class SalesScanEventController {
3033

3134
/** EventService. */
3235
private final EventService eventService;
36+
/** AuthenticationService. */
37+
private final AuthenticationService authenticationService;
38+
/** SalesService. */
39+
private final SalesService salesService;
3340

3441
/**
3542
* SalesScanEventController.
3643
*
37-
* @param eventService of type EventService
44+
* @param eventService of type EventService
45+
* @param authenticationService of type AuthenticationService
46+
* @param salesService of type SalesService
3847
*/
39-
public SalesScanEventController(EventService eventService) {
48+
public SalesScanEventController(
49+
EventService eventService,
50+
AuthenticationService authenticationService,
51+
SalesService salesService
52+
) {
4053
this.eventService = eventService;
54+
this.authenticationService = authenticationService;
55+
this.salesService = salesService;
4156
}
4257

4358
/**
@@ -54,6 +69,12 @@ public SalesScanEventController(EventService eventService) {
5469
public String scanner(Model model, RedirectAttributes redirect, @PathVariable String key, @PathVariable String method) {
5570
try {
5671
Event event = eventService.getByKey(key);
72+
Customer currentUser = authenticationService.getCurrentCustomer();
73+
if (!salesService.hasAccessToEvent(currentUser, event)) {
74+
redirect.addFlashAttribute(ATTR_ERROR, "You do not have access to this event.");
75+
76+
return ERROR_REDIRECT;
77+
}
5778
model.addAttribute(ATTR_EVENT, event);
5879

5980
return "sales/scan/event/" + method;

src/main/java/ch/wisv/events/sales/controller/scan/SalesScanRestController.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
import ch.wisv.events.core.exception.normal.EventsException;
44
import ch.wisv.events.core.exception.normal.TicketNotFoundException;
5+
import ch.wisv.events.core.model.customer.Customer;
56
import ch.wisv.events.core.model.event.Event;
67
import ch.wisv.events.core.model.ticket.Ticket;
78
import ch.wisv.events.core.model.ticket.TicketStatus;
9+
import ch.wisv.events.core.service.auth.AuthenticationService;
810
import ch.wisv.events.core.service.event.EventService;
911
import ch.wisv.events.core.service.ticket.TicketService;
1012
import static ch.wisv.events.utils.ResponseEntityBuilder.createResponseEntity;
1113
import java.util.Objects;
1214

1315
import ch.wisv.events.sales.model.ScanDto;
16+
import ch.wisv.events.sales.service.SalesService;
1417
import org.json.simple.JSONObject;
1518
import org.springframework.http.HttpStatus;
1619
import org.springframework.http.ResponseEntity;
@@ -41,16 +44,29 @@ public class SalesScanRestController {
4144

4245
/** TicketService. */
4346
private final TicketService ticketService;
47+
/** AuthenticationService. */
48+
private final AuthenticationService authenticationService;
49+
/** SalesService. */
50+
private final SalesService salesService;
4451

4552
/**
4653
* SalesScanRestController.
4754
*
48-
* @param eventService of type EventService
49-
* @param ticketService of type TicketService
55+
* @param eventService of type EventService
56+
* @param ticketService of type TicketService
57+
* @param authenticationService of type AuthenticationService
58+
* @param salesService of type SalesService
5059
*/
51-
public SalesScanRestController(EventService eventService, TicketService ticketService) {
60+
public SalesScanRestController(
61+
EventService eventService,
62+
TicketService ticketService,
63+
AuthenticationService authenticationService,
64+
SalesService salesService
65+
) {
5266
this.eventService = eventService;
5367
this.ticketService = ticketService;
68+
this.authenticationService = authenticationService;
69+
this.salesService = salesService;
5470
}
5571

5672
/**
@@ -122,6 +138,10 @@ private ResponseEntity handleScanTicket(String key, String code) {
122138

123139
try {
124140
Event event = eventService.getByKey(key);
141+
Customer currentUser = authenticationService.getCurrentCustomer();
142+
if (!salesService.hasAccessToEvent(currentUser, event)) {
143+
return createResponseEntity(HttpStatus.FORBIDDEN, "You do not have access to this event.");
144+
}
125145
Ticket ticket = this.getTicketByUniqueCode(event, code);
126146
ScanDto scan = new ScanDto(ticket.getProduct().getTitle(), ticket.getOwner().getName());
127147

src/main/java/ch/wisv/events/sales/controller/sell/SalesSellCustomerController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* SalesSellCustomerController class.
2525
*/
2626
@Controller
27-
@PreAuthorize("hasRole('USER')")
27+
@PreAuthorize("hasRole('ADMIN')")
2828
@RequestMapping("/sales/sell/customer/{publicReference}")
2929
public class SalesSellCustomerController {
3030

@@ -97,6 +97,7 @@ public String determineCustomer(RedirectAttributes redirect, @PathVariable Strin
9797

9898
return "redirect:/sales/sell/order/" + order.getPublicReference();
9999
} catch (CustomerNotFoundException e) {
100+
redirect.addFlashAttribute("customer", customer);
100101
return "redirect:/sales/sell/customer/" + order.getPublicReference() + "/create";
101102
} catch (EventsException e) {
102103
redirect.addFlashAttribute("error", e.getMessage());
@@ -139,7 +140,7 @@ public String create(RedirectAttributes redirect, @ModelAttribute Customer custo
139140

140141
redirect.addFlashAttribute("success", "Customer successfully created!");
141142

142-
return "redirect:/sales/order/" + order.getPublicReference();
143+
return "redirect:/sales/sell/order/" + order.getPublicReference();
143144
} catch (CustomerInvalidException e) {
144145
redirect.addFlashAttribute("error", e.getMessage());
145146
redirect.addFlashAttribute("customer", customer);

src/main/java/ch/wisv/events/sales/controller/sell/SalesSellMainController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
@Controller
2525
@RequestMapping(value = "/sales/sell")
26-
@PreAuthorize("hasRole('USER')")
26+
@PreAuthorize("hasRole('ADMIN')")
2727
public class SalesSellMainController {
2828

2929
/** AuthenticationService. */

src/main/java/ch/wisv/events/sales/controller/sell/SalesSellOrderController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* SalesSellOrderController class.
1616
*/
1717
@Controller
18-
@PreAuthorize("hasRole('USER')")
18+
@PreAuthorize("hasRole('ADMIN')")
1919
@RequestMapping(value = "/sales/sell/order/{publicReference}")
2020
public class SalesSellOrderController {
2121

@@ -73,4 +73,4 @@ public String complete(RedirectAttributes redirect, @PathVariable String publicR
7373
return "redirect:/sales/";
7474
}
7575
}
76-
}
76+
}

src/main/java/ch/wisv/events/sales/controller/sell/SalesSellPaymentController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.springframework.web.bind.annotation.RequestParam;
1515

1616
@Controller
17-
@PreAuthorize("hasRole('USER')")
17+
@PreAuthorize("hasRole('ADMIN')")
1818
@RequestMapping(value = "/sales/sell/payment/{publicReference}")
1919
public class SalesSellPaymentController {
2020

src/main/java/ch/wisv/events/sales/controller/stats/SalesStatsController.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.springframework.web.bind.annotation.GetMapping;
1717
import org.springframework.web.bind.annotation.PathVariable;
1818
import org.springframework.web.bind.annotation.RequestMapping;
19+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
1920

2021
import java.util.List;
2122
import java.util.stream.Collectors;
@@ -83,8 +84,14 @@ public String indexView(Model model) {
8384
* @return String
8485
*/
8586
@GetMapping("/products/{key}")
86-
public String ticketSalesindex(Model model, @PathVariable String key) throws EventNotFoundException {
87+
public String ticketSalesindex(Model model, RedirectAttributes redirect, @PathVariable String key) throws EventNotFoundException {
8788
Event event = eventService.getByKey(key);
89+
Customer currentUser = authenticationService.getCurrentCustomer();
90+
if (!salesService.hasAccessToEvent(currentUser, event)) {
91+
redirect.addFlashAttribute("error", "You do not have access to this event.");
92+
93+
return ERROR_REDIRECT;
94+
}
8895

8996
model.addAttribute("products", event.getProducts());
9097
model.addAttribute("target", event.getTarget());
@@ -101,8 +108,15 @@ public String ticketSalesindex(Model model, @PathVariable String key) throws Eve
101108
* @return String
102109
*/
103110
@GetMapping("/event/{key}")
104-
public String eventSalesView(Model model, @PathVariable String key) throws EventNotFoundException {
111+
public String eventSalesView(Model model, RedirectAttributes redirect, @PathVariable String key) throws EventNotFoundException {
105112
Event event = eventService.getByKey(key);
113+
Customer currentUser = authenticationService.getCurrentCustomer();
114+
if (!salesService.hasAccessToEvent(currentUser, event)) {
115+
redirect.addFlashAttribute("error", "You do not have access to this event.");
116+
117+
return ERROR_REDIRECT;
118+
}
119+
106120
List<Order> orders = salesService.getAllOrdersByEvent(event).stream().peek((Order order) -> {
107121
order.setOwner(null);
108122
order.setChPaymentsReference(null);

src/main/java/ch/wisv/events/sales/service/SalesService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public interface SalesService {
2525
*/
2626
List<Product> getAllGrantedProductByCustomer(Customer customer);
2727

28+
/**
29+
* Check whether the given customer can access data for a specific event.
30+
*
31+
* @param customer of type Customer
32+
* @param event of type Event
33+
* @return true when the customer is admin or is in the event organizer LDAP group
34+
*/
35+
boolean hasAccessToEvent(Customer customer, Event event);
36+
2837
/**
2938
* Get all orders of an event.
3039
*

0 commit comments

Comments
 (0)