Skip to content
Snippets Groups Projects

Update active course filter for dashboard

2 files
+ 49
21
Compare changes
  • Side-by-side
  • Inline

Files

/*
/**
* Queue - A Queueing system that can be used to handle labs in higher education Copyright (C) 2016-2019 Delft
* University of Technology
*
@@ -19,7 +19,10 @@ import java.util.List;
import java.util.stream.Collectors;
import nl.tudelft.ewi.queue.annotation.AuthenticatedUser;
import nl.tudelft.ewi.queue.model.*;
import nl.tudelft.ewi.queue.model.Course;
import nl.tudelft.ewi.queue.model.Role;
import nl.tudelft.ewi.queue.model.User;
import nl.tudelft.ewi.queue.model.UserPrincipal;
import nl.tudelft.ewi.queue.repository.CourseRepository;
import nl.tudelft.ewi.queue.repository.UserRepository;
@@ -46,41 +49,59 @@ public class HomeController {
return "main";
}
/**
* Maps the index/root url to a page. When the user is logged in correctly with an
* AnonymousAuthenticationToken, the user is directed to a dashboard view of the Queue. When the user is
* not correctly logged in, they are redirected to the introductory page of the Queue.
*
* @param model The model to be filled out when the user has courses and/or labs to show on their
* dashboard.
* @return The path to the corresponding Thymeleaf template.
*/
@RequestMapping("/")
public String index(Model model) {
if (SecurityContextHolder.getContext().getAuthentication() != null) {
if (SecurityContextHolder.getContext().getAuthentication().isAuthenticated()) {
if (!(SecurityContextHolder.getContext()
if (SecurityContextHolder.getContext().getAuthentication() != null
&& SecurityContextHolder.getContext().getAuthentication().isAuthenticated()
&& !(SecurityContextHolder.getContext()
.getAuthentication() instanceof AnonymousAuthenticationToken)) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
return dashboard(userRepository.findByUsername(userPrincipal.getUsername()), model);
}
}
return dashboard(userRepository.findByUsername(userPrincipal.getUsername()), model);
}
return "home/index";
}
/**
* Maps the user to the dashboard page and inserts the courses and user attributes corresponding to the
* given user.
*
* @param user The user to fill the dashboard model for.
* @param model The model to fill.
* @return The path to the dashboard Thymeleaf template.
*/
private String dashboard(@AuthenticatedUser User user, Model model) {
model.addAttribute("user", user);
List<Role> availableCourses = user.getRoles().stream()
.filter(role -> !role.getCourse().getIsArchived()
|| role.getUser().isTeacher())
.filter(role -> !role.getCourse().isDeleted()
&& (!role.getCourse().getIsArchived()
|| role.getUser().isTeacher()))
.collect(Collectors.toList());
// System.out.println(availableCourses);
model.addAttribute("availableCourses", availableCourses);
// TODO this is probably not efficient
if (user.isAdmin()) {
List<Course> runningCourses = courseRepository.findAll().stream()
.filter(course -> !course.getTodaysLabs().isEmpty())
.collect(Collectors.toList());
model.addAttribute("runningCourses", runningCourses);
}
model.addAttribute("user", user);
model.addAttribute("availableCourses", availableCourses);
return "home/dashboard";
}
}
Loading