Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cc.serviceops.ticket;
- import cc.serviceops.Tools;
- import cc.serviceops.account.User;
- import cc.serviceops.account.dao.SignupService;
- import cc.serviceops.account.dao.UserService;
- import cc.serviceops.organisation.Organisation;
- import cc.serviceops.organisation.dao.OrganisationService;
- import cc.serviceops.organisation.Team;
- import cc.serviceops.ticket.dao.TicketService;
- import cc.serviceops.ticket.helpers.*;
- import lombok.AllArgsConstructor;
- import org.springframework.http.HttpStatus;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.validation.BindingResult;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.server.ResponseStatusException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.validation.Valid;
- import java.security.Principal;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.stream.Collectors;
- @Controller @AllArgsConstructor
- public class TicketController {
- private final UserService userService;
- private final OrganisationService organisationService;
- private final TicketService ticketService;
- private final SignupService signupService;
- @GetMapping("/{organisation:^(?!css$|scripts$|img$|fonts$).*}/{ticket}")
- public String showTicket(@PathVariable("organisation") String organisationName,
- @PathVariable("ticket") String ticketId,
- Model model, Principal principal) {
- ...
- if (principal == null) {
- model.addAttribute("anonymousTicketDto", getAnonymousDtoFromTicket(ticket));
- return "ticket/anonymous";
- }
- model.addAttribute("ticketDto", getDtoFromTicket(ticket));
- return "ticket/ticket";
- }
- ...
- @PostMapping("/{organisation:^(?!css$|scripts$|img$|fonts$).*}/anonymous")
- public String doTicket(@Valid @ModelAttribute AnonymousTicketDto anonymousTicketDto, BindingResult bindingResult,
- @PathVariable("organisation") String organisationName, Model model,
- HttpServletRequest request) {
- Organisation activeOrganisation = organisationService.getOrganisationByName(organisationName);
- User activeUser = userService.getByEmail(anonymousTicketDto.getCreatorEmail());
- if (activeOrganisation == null) {
- throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Organisation cannot be found");
- }
- if (anonymousTicketDto.getCategory() == null || anonymousTicketDto.getCategory().getId() < 1) {
- bindingResult.rejectValue("category.id", "ticket.category");
- }
- if (activeUser != null) {
- bindingResult.rejectValue("creatorEmail", "user.email.duplicate");
- }
- if (!anonymousTicketDto.getPassword().equals(anonymousTicketDto.getPasswordConfirmation()) &&
- !anonymousTicketDto.getPasswordConfirmation().isBlank()) {
- bindingResult.rejectValue("passwordConfirmation", "user.password.confirmation");
- }
- if (bindingResult.hasErrors()) {
- fillTicketAttributes(activeOrganisation, model);
- model.addAttribute("anonymousTicketDto", anonymousTicketDto);
- return "ticket/anonymous";
- }
- activeUser = new User() {{
- setEmail(anonymousTicketDto.getCreatorEmail());
- setPassword(anonymousTicketDto.getPassword());
- }};
- activeUser = signupService.saveGuest(activeUser);
- // Password in activeUser is encrypted
- authWithHttpServletRequest(request, activeUser.getEmail(), anonymousTicketDto.getPassword());
- Ticket ticket = getTicketFromDto(anonymousTicketDto);
- ticket.setCreator(activeUser);
- ticket = ticketService.saveTicket(ticket);
- return String.format("redirect:/%s/%s?success", organisationName, ticket.getId());
- }
- ...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement