Skip to content
Snippets Groups Projects

Redirect to enroll page when not enrolled

Files

@@ -20,8 +20,12 @@ package nl.tudelft.queue.controller;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import nl.tudelft.labracore.api.dto.AssignmentSummaryDTO;
@@ -47,6 +51,7 @@ import nl.tudelft.queue.model.enums.LabType;
import nl.tudelft.queue.model.labs.ExamLab;
import nl.tudelft.queue.model.labs.RegularLab;
import nl.tudelft.queue.model.labs.SlottedLab;
import nl.tudelft.queue.repository.LabRepository;
import nl.tudelft.queue.service.LabService;
import nl.tudelft.queue.service.PermissionService;
import nl.tudelft.queue.service.RequestService;
@@ -54,16 +59,17 @@ import nl.tudelft.queue.service.RequestTableService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
@Controller
public class LabController {
@Autowired
private LabRepository labRepository;
@Autowired
private LabService ls;
@@ -499,4 +505,25 @@ public class LabController {
.map(EditionSummaryDTO::getId).collect(Collectors.toList()), model);
}
/**
* Handles any {@link AccessDeniedException} thrown in this controller.
*
* @param request The HTTP request associated with this exception.
*
* @return The enrol page when the exception is thrown from a /lab/* page. Otherwise return the
* 403 error page.
*/
@ExceptionHandler(AccessDeniedException.class)
public String redirectToEnrolPage(HttpServletRequest request) {
Pattern testPattern = Pattern.compile("/lab/([0-9]+)/*");
Matcher matcher = testPattern.matcher(request.getRequestURI());
if (matcher.matches() && matcher.groupCount() == 1) {
Optional<Lab> lab = labRepository.findById(Long.parseLong(matcher.group(1)));
var session = sCache.getOrThrow(lab.orElseThrow().getSession());
var edition = session.getEdition().getId();
return "redirect:/edition/" + edition + "/enrol";
}
return "error/403";
}
}
Loading