| 1 | package edu.ucsb.cs156.frontiers.config; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 4 | import edu.ucsb.cs156.frontiers.models.CurrentUser; | |
| 5 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 6 | import edu.ucsb.cs156.frontiers.repositories.DownloadRequestRepository; | |
| 7 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 8 | import edu.ucsb.cs156.frontiers.services.CurrentUserService; | |
| 9 | import java.util.Collection; | |
| 10 | import java.util.Objects; | |
| 11 | import java.util.Optional; | |
| 12 | import lombok.extern.slf4j.Slf4j; | |
| 13 | import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations; | |
| 14 | import org.springframework.security.access.hierarchicalroles.RoleHierarchy; | |
| 15 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 16 | import org.springframework.security.core.GrantedAuthority; | |
| 17 | import org.springframework.stereotype.Component; | |
| 18 | ||
| 19 | /** | |
| 20 | * CourseSecurity provides methods to check permissions for managing courses and roster students. It | |
| 21 | * uses the CurrentUserService to get the current user and RoleHierarchy to check roles. | |
| 22 | * | |
| 23 | * <p>The methods defined here are used as annotations (e.g. <code> | |
| 24 | * @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #id)")</code>) in the | |
| 25 | * CourseController and RosterStudentController to enforce security checks. | |
| 26 | * | |
| 27 | * <p>Note that for a method with a courseId, you <em>still</em> need to verify in each method, | |
| 28 | * whether the course exists or not. These annotations will <em>only</em> check whether or not the | |
| 29 | * particular user has access to a particular course, for example. | |
| 30 | * | |
| 31 | * <p>When testing, use <code>@WithInstructorCoursePermissions</code> or <code> | |
| 32 | * @WithStaffCoursePermissions</code> to mock a user with the appropriate roles. | |
| 33 | */ | |
| 34 | @Slf4j | |
| 35 | @Component("CourseSecurity") | |
| 36 | public class CourseSecurity { | |
| 37 | private final CurrentUserService currentUserService; | |
| 38 | private final RoleHierarchy roleHierarchy; | |
| 39 | private final CourseRepository courseRepository; | |
| 40 | private final RosterStudentRepository rosterStudentRepository; | |
| 41 | private final DownloadRequestRepository downloadRequestRepository; | |
| 42 | ||
| 43 | public CourseSecurity( | |
| 44 | CurrentUserService currentUserService, | |
| 45 | RoleHierarchy roleHierarchy, | |
| 46 | CourseRepository courseRepository, | |
| 47 | RosterStudentRepository rosterStudentRepository, | |
| 48 | DownloadRequestRepository downloadRequestRepository) { | |
| 49 | this.currentUserService = currentUserService; | |
| 50 | this.roleHierarchy = roleHierarchy; | |
| 51 | this.courseRepository = courseRepository; | |
| 52 | this.rosterStudentRepository = rosterStudentRepository; | |
| 53 | this.downloadRequestRepository = downloadRequestRepository; | |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * Use this when you want to check whether the user is either a staff member, instructor or admin | |
| 58 | * for the course. | |
| 59 | * | |
| 60 | * @param operations | |
| 61 | * @param courseId | |
| 62 | * @return true if the user has manage permissions for the course, false otherwise. | |
| 63 | */ | |
| 64 | @PreAuthorize("hasRole('ROLE_USER') || hasRole('ROLE_API_KEY')") | |
| 65 | public Boolean hasManagePermissions( | |
| 66 | MethodSecurityExpressionOperations operations, Long courseId) { | |
| 67 |
1
1. hasManagePermissions : negated conditional → KILLED |
if (operations.getAuthentication() instanceof ApiKeyToken token) { |
| 68 |
2
1. hasManagePermissions : replaced Boolean return with True for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasManagePermissions → KILLED 2. hasManagePermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasManagePermissions → KILLED |
return Objects.equals(token.getCourseId(), courseId); |
| 69 | } | |
| 70 | Optional<Course> course = courseRepository.findById(courseId); | |
| 71 |
1
1. hasManagePermissions : negated conditional → KILLED |
if (course.isEmpty()) { |
| 72 |
1
1. hasManagePermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasManagePermissions → KILLED |
return true; |
| 73 | } | |
| 74 |
2
1. hasManagePermissions : replaced Boolean return with True for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasManagePermissions → KILLED 2. hasManagePermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasManagePermissions → KILLED |
return baseHasManagePermissions(operations, course.get()); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * Use this for operations that only an instructor can do, but not a staff member, such as adding | |
| 79 | * or deleting a course staff member. | |
| 80 | * | |
| 81 | * @param operations | |
| 82 | * @param courseId | |
| 83 | * @return true if the user has instructor permissions for the course, false otherwise. | |
| 84 | */ | |
| 85 | @PreAuthorize("hasRole('ROLE_INSTRUCTOR') || hasRole('ROLE_API_KEY')") | |
| 86 | public Boolean hasInstructorPermissions( | |
| 87 | MethodSecurityExpressionOperations operations, Long courseId) { | |
| 88 |
1
1. hasInstructorPermissions : negated conditional → KILLED |
if (operations.getAuthentication() instanceof ApiKeyToken token) { |
| 89 |
2
1. hasInstructorPermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasInstructorPermissions → KILLED 2. hasInstructorPermissions : replaced Boolean return with True for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasInstructorPermissions → KILLED |
return Objects.equals(token.getCourseId(), courseId); |
| 90 | } | |
| 91 | CurrentUser currentUser = currentUserService.getCurrentUser(); | |
| 92 | Collection<? extends GrantedAuthority> authorities = | |
| 93 | roleHierarchy.getReachableGrantedAuthorities(currentUser.getRoles()); | |
| 94 |
3
1. lambda$hasInstructorPermissions$0 : replaced boolean return with false for edu/ucsb/cs156/frontiers/config/CourseSecurity::lambda$hasInstructorPermissions$0 → KILLED 2. lambda$hasInstructorPermissions$0 : replaced boolean return with true for edu/ucsb/cs156/frontiers/config/CourseSecurity::lambda$hasInstructorPermissions$0 → KILLED 3. hasInstructorPermissions : negated conditional → KILLED |
if (authorities.stream().anyMatch(role -> role.getAuthority().equals("ROLE_ADMIN"))) { |
| 95 |
1
1. hasInstructorPermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasInstructorPermissions → KILLED |
return true; |
| 96 | } else { | |
| 97 | Optional<Course> course = courseRepository.findById(courseId); | |
| 98 |
1
1. hasInstructorPermissions : negated conditional → KILLED |
if (course.isEmpty()) { |
| 99 |
1
1. hasInstructorPermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasInstructorPermissions → KILLED |
return true; |
| 100 | } | |
| 101 |
2
1. hasInstructorPermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasInstructorPermissions → KILLED 2. hasInstructorPermissions : replaced Boolean return with True for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasInstructorPermissions → KILLED |
return currentUser.getUser().getEmail().equals(course.get().getInstructorEmail()); |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * This method checks if the current user has management permissions for the course associated | |
| 107 | * with the given rosterStudent. This allows us to create endpoints that just take a roster | |
| 108 | * student id, not a course id, and still check permissions. This one works for both staff and | |
| 109 | * instructor permissions. | |
| 110 | * | |
| 111 | * @param operations | |
| 112 | * @param rosterStudentId | |
| 113 | * @return | |
| 114 | */ | |
| 115 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 116 | public Boolean hasRosterStudentManagementPermissions( | |
| 117 | MethodSecurityExpressionOperations operations, Long rosterStudentId) { | |
| 118 |
2
1. hasRosterStudentManagementPermissions : replaced Boolean return with True for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasRosterStudentManagementPermissions → KILLED 2. hasRosterStudentManagementPermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasRosterStudentManagementPermissions → KILLED |
return rosterStudentRepository |
| 119 | .findById(rosterStudentId) | |
| 120 |
2
1. lambda$hasRosterStudentManagementPermissions$0 : replaced Boolean return with True for edu/ucsb/cs156/frontiers/config/CourseSecurity::lambda$hasRosterStudentManagementPermissions$0 → KILLED 2. lambda$hasRosterStudentManagementPermissions$0 : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::lambda$hasRosterStudentManagementPermissions$0 → KILLED |
.map(rosterStudent -> baseHasManagePermissions(operations, rosterStudent.getCourse())) |
| 121 | .orElse(true); | |
| 122 | } | |
| 123 | ||
| 124 | /** | |
| 125 | * This is a helper method that checks if the current user has management permissions for the | |
| 126 | * given course. | |
| 127 | * | |
| 128 | * @param operations | |
| 129 | * @param course | |
| 130 | * @return | |
| 131 | */ | |
| 132 | public Boolean baseHasManagePermissions( | |
| 133 | MethodSecurityExpressionOperations operations, Course course) { | |
| 134 | CurrentUser currentUser = currentUserService.getCurrentUser(); | |
| 135 | Collection<? extends GrantedAuthority> authorities = | |
| 136 | roleHierarchy.getReachableGrantedAuthorities(currentUser.getRoles()); | |
| 137 |
3
1. lambda$baseHasManagePermissions$0 : replaced boolean return with false for edu/ucsb/cs156/frontiers/config/CourseSecurity::lambda$baseHasManagePermissions$0 → KILLED 2. baseHasManagePermissions : negated conditional → KILLED 3. lambda$baseHasManagePermissions$0 : replaced boolean return with true for edu/ucsb/cs156/frontiers/config/CourseSecurity::lambda$baseHasManagePermissions$0 → KILLED |
if (authorities.stream().anyMatch(role -> role.getAuthority().equals("ROLE_ADMIN"))) { |
| 138 |
1
1. baseHasManagePermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::baseHasManagePermissions → KILLED |
return true; |
| 139 | } else { | |
| 140 | if (course.getCourseStaff().stream() | |
| 141 |
3
1. baseHasManagePermissions : negated conditional → KILLED 2. lambda$baseHasManagePermissions$1 : replaced boolean return with true for edu/ucsb/cs156/frontiers/config/CourseSecurity::lambda$baseHasManagePermissions$1 → KILLED 3. lambda$baseHasManagePermissions$1 : replaced boolean return with false for edu/ucsb/cs156/frontiers/config/CourseSecurity::lambda$baseHasManagePermissions$1 → KILLED |
.anyMatch(staff -> staff.getEmail().equals(currentUser.getUser().getEmail()))) { |
| 142 |
1
1. baseHasManagePermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::baseHasManagePermissions → KILLED |
return true; |
| 143 | } | |
| 144 |
2
1. baseHasManagePermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::baseHasManagePermissions → KILLED 2. baseHasManagePermissions : replaced Boolean return with True for edu/ucsb/cs156/frontiers/config/CourseSecurity::baseHasManagePermissions → KILLED |
return currentUser.getUser().getEmail().equals(course.getInstructorEmail()); |
| 145 | } | |
| 146 | } | |
| 147 | ||
| 148 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 149 | public Boolean hasDownloadPermissions( | |
| 150 | MethodSecurityExpressionOperations operations, Long downloadId) { | |
| 151 |
2
1. hasDownloadPermissions : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasDownloadPermissions → KILLED 2. hasDownloadPermissions : replaced Boolean return with True for edu/ucsb/cs156/frontiers/config/CourseSecurity::hasDownloadPermissions → KILLED |
return downloadRequestRepository |
| 152 | .findById(downloadId) | |
| 153 |
2
1. lambda$hasDownloadPermissions$0 : replaced Boolean return with False for edu/ucsb/cs156/frontiers/config/CourseSecurity::lambda$hasDownloadPermissions$0 → KILLED 2. lambda$hasDownloadPermissions$0 : replaced Boolean return with True for edu/ucsb/cs156/frontiers/config/CourseSecurity::lambda$hasDownloadPermissions$0 → KILLED |
.map(downloadRequest -> baseHasManagePermissions(operations, downloadRequest.getCourse())) |
| 154 | .orElse(true); | |
| 155 | } | |
| 156 | } | |
Mutations | ||
| 67 |
1.1 |
|
| 68 |
1.1 2.2 |
|
| 71 |
1.1 |
|
| 72 |
1.1 |
|
| 74 |
1.1 2.2 |
|
| 88 |
1.1 |
|
| 89 |
1.1 2.2 |
|
| 94 |
1.1 2.2 3.3 |
|
| 95 |
1.1 |
|
| 98 |
1.1 |
|
| 99 |
1.1 |
|
| 101 |
1.1 2.2 |
|
| 118 |
1.1 2.2 |
|
| 120 |
1.1 2.2 |
|
| 137 |
1.1 2.2 3.3 |
|
| 138 |
1.1 |
|
| 141 |
1.1 2.2 3.3 |
|
| 142 |
1.1 |
|
| 144 |
1.1 2.2 |
|
| 151 |
1.1 2.2 |
|
| 153 |
1.1 2.2 |