| 1 | package edu.ucsb.cs156.happiercows.services; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.Game; | |
| 4 | import edu.ucsb.cs156.happiercows.entities.Staff; | |
| 5 | import edu.ucsb.cs156.happiercows.entities.Student; | |
| 6 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 7 | import edu.ucsb.cs156.happiercows.repositories.StaffRepository; | |
| 8 | import edu.ucsb.cs156.happiercows.repositories.StudentRepository; | |
| 9 | import edu.ucsb.cs156.happiercows.utilities.CanonicalFormConverter; | |
| 10 | import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | import org.springframework.stereotype.Service; | |
| 12 | ||
| 13 | import java.util.ArrayList; | |
| 14 | import java.util.List; | |
| 15 | import java.util.Optional; | |
| 16 | ||
| 17 | /** | |
| 18 | * Determines whether a user has access to a course-linked Game, based on | |
| 19 | * whether their email appears on the roster (as a Student or Staff member) | |
| 20 | * of the course the Game is linked to. See issue #251. | |
| 21 | */ | |
| 22 | @Service | |
| 23 | public class CourseAccessService { | |
| 24 | ||
| 25 | @Autowired | |
| 26 | private StudentRepository studentRepository; | |
| 27 | ||
| 28 | @Autowired | |
| 29 | private StaffRepository staffRepository; | |
| 30 | ||
| 31 | /** | |
| 32 | * A user is eligible for a course-linked game if they are an admin, or | |
| 33 | * if their email appears on the course's roster of students or staff. | |
| 34 | * A game with no course (courseId == null) is open to everyone. | |
| 35 | */ | |
| 36 | public boolean isEligibleForGame(User user, Game game) { | |
| 37 |
1
1. isEligibleForGame : negated conditional → KILLED |
if (game.getCourseId() == null) { |
| 38 |
1
1. isEligibleForGame : replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED |
return true; |
| 39 | } | |
| 40 |
1
1. isEligibleForGame : negated conditional → KILLED |
if (user.isAdmin()) { |
| 41 |
1
1. isEligibleForGame : replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED |
return true; |
| 42 | } | |
| 43 |
2
1. isEligibleForGame : replaced boolean return with false for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED 2. isEligibleForGame : replaced boolean return with true for edu/ucsb/cs156/happiercows/services/CourseAccessService::isEligibleForGame → KILLED |
return getCourseIdsForUser(user).contains(game.getCourseId()); |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * Returns the distinct list of course ids for which this user's email | |
| 48 | * appears on the roster as a student or as staff. | |
| 49 | * | |
| 50 | * <p>Roster emails are compared to the user's email using | |
| 51 | * {@link CanonicalFormConverter#areEquivalentEmails}, not exact string | |
| 52 | * equality, so that a roster row uploaded with a stale/non-canonical | |
| 53 | * email (e.g. an old {@code @umail.ucsb.edu} entry that predates this | |
| 54 | * fix, or one that slips in from a school whose export format isn't | |
| 55 | * fully normalized) still matches a student logging in with the | |
| 56 | * canonical form of the same address. See issue #278. | |
| 57 | */ | |
| 58 | public List<Long> getCourseIdsForUser(User user) { | |
| 59 | List<Long> courseIds = new ArrayList<>(); | |
| 60 | String email = user.getEmail(); | |
| 61 | ||
| 62 | for (Student student : studentRepository.findAll()) { | |
| 63 |
1
1. getCourseIdsForUser : negated conditional → KILLED |
if (student.getCourseId() != null |
| 64 |
1
1. getCourseIdsForUser : negated conditional → KILLED |
&& !courseIds.contains(student.getCourseId()) |
| 65 |
1
1. getCourseIdsForUser : negated conditional → KILLED |
&& CanonicalFormConverter.areEquivalentEmails(student.getEmail(), email)) { |
| 66 | courseIds.add(student.getCourseId()); | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | for (Staff staff : staffRepository.findAll()) { | |
| 71 |
1
1. getCourseIdsForUser : negated conditional → KILLED |
if (staff.getCourseId() != null |
| 72 |
1
1. getCourseIdsForUser : negated conditional → KILLED |
&& !courseIds.contains(staff.getCourseId()) |
| 73 |
1
1. getCourseIdsForUser : negated conditional → KILLED |
&& CanonicalFormConverter.areEquivalentEmails(staff.getEmail(), email)) { |
| 74 | courseIds.add(staff.getCourseId()); | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 |
1
1. getCourseIdsForUser : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/services/CourseAccessService::getCourseIdsForUser → KILLED |
return courseIds; |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * Returns the Student roster row that corresponds to this user, for the | |
| 83 | * course linked to the given game - but only if the game is actually | |
| 84 | * linked to a course. Used to decide whether a FarmerActivity row should | |
| 85 | * be recorded for a play-page view or a buy/sell action: activity is | |
| 86 | * only tracked for course-linked games, and only for users whose email | |
| 87 | * matches a student on that course's roster (see issue #291), using the | |
| 88 | * same canonical-email-matching rule as {@link #getCourseIdsForUser}. | |
| 89 | */ | |
| 90 | public Optional<Student> findMatchingStudentForCourseLinkedGame(User user, Game game) { | |
| 91 |
1
1. findMatchingStudentForCourseLinkedGame : negated conditional → KILLED |
if (game.getCourseId() == null) { |
| 92 | return Optional.empty(); | |
| 93 | } | |
| 94 | ||
| 95 | String email = user.getEmail(); | |
| 96 | ||
| 97 | for (Student student : studentRepository.findByCourseId(game.getCourseId())) { | |
| 98 |
1
1. findMatchingStudentForCourseLinkedGame : negated conditional → KILLED |
if (CanonicalFormConverter.areEquivalentEmails(student.getEmail(), email)) { |
| 99 |
1
1. findMatchingStudentForCourseLinkedGame : replaced return value with Optional.empty for edu/ucsb/cs156/happiercows/services/CourseAccessService::findMatchingStudentForCourseLinkedGame → KILLED |
return Optional.of(student); |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 103 | return Optional.empty(); | |
| 104 | } | |
| 105 | } | |
Mutations | ||
| 37 |
1.1 |
|
| 38 |
1.1 |
|
| 40 |
1.1 |
|
| 41 |
1.1 |
|
| 43 |
1.1 2.2 |
|
| 63 |
1.1 |
|
| 64 |
1.1 |
|
| 65 |
1.1 |
|
| 71 |
1.1 |
|
| 72 |
1.1 |
|
| 73 |
1.1 |
|
| 78 |
1.1 |
|
| 91 |
1.1 |
|
| 98 |
1.1 |
|
| 99 |
1.1 |