| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 5 | import edu.ucsb.cs156.frontiers.entities.User; | |
| 6 | import edu.ucsb.cs156.frontiers.enums.OrgStatus; | |
| 7 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 8 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 9 | import edu.ucsb.cs156.frontiers.repositories.UserRepository; | |
| 10 | import java.util.ArrayList; | |
| 11 | import java.util.List; | |
| 12 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 13 | import org.springframework.security.core.Authentication; | |
| 14 | import org.springframework.security.core.GrantedAuthority; | |
| 15 | import org.springframework.security.core.context.SecurityContext; | |
| 16 | import org.springframework.security.core.context.SecurityContextHolder; | |
| 17 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | |
| 18 | import org.springframework.security.oauth2.core.oidc.user.OidcUser; | |
| 19 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 20 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 21 | import org.springframework.web.bind.annotation.RestController; | |
| 22 | ||
| 23 | @RestController | |
| 24 | @RequestMapping("/api/github") | |
| 25 | public class GithubController extends ApiController { | |
| 26 | ||
| 27 | private final UserRepository userRepository; | |
| 28 | private final RosterStudentRepository rosterStudentRepository; | |
| 29 | private final CourseStaffRepository courseStaffRepository; | |
| 30 | ||
| 31 | public GithubController( | |
| 32 | UserRepository userRepository, | |
| 33 | RosterStudentRepository rosterStudentRepository, | |
| 34 | CourseStaffRepository courseStaffRepository) { | |
| 35 | super(); | |
| 36 | this.userRepository = userRepository; | |
| 37 | this.rosterStudentRepository = rosterStudentRepository; | |
| 38 | this.courseStaffRepository = courseStaffRepository; | |
| 39 | } | |
| 40 | ||
| 41 | @PreAuthorize("hasRole('ROLE_GITHUB')") | |
| 42 | @DeleteMapping("/disconnect") | |
| 43 | public Object disconnect(SecurityContext context) { | |
| 44 | User currentUser = getCurrentUser().getUser(); | |
| 45 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/User::setGithubId → KILLED |
currentUser.setGithubId(null); |
| 46 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/User::setGithubLogin → KILLED |
currentUser.setGithubLogin(null); |
| 47 | userRepository.save(currentUser); | |
| 48 | ||
| 49 | List<RosterStudent> invitedStudents = new ArrayList<>(); | |
| 50 | for (RosterStudent rs : rosterStudentRepository.findAllByUser(currentUser)) { | |
| 51 |
1
1. disconnect : negated conditional → KILLED |
if (rs.getOrgStatus() == OrgStatus.INVITED) { |
| 52 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setOrgStatus → KILLED |
rs.setOrgStatus(OrgStatus.JOINCOURSE); |
| 53 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setGithubId → KILLED |
rs.setGithubId(null); |
| 54 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setGithubLogin → KILLED |
rs.setGithubLogin(null); |
| 55 | invitedStudents.add(rs); | |
| 56 | } | |
| 57 | } | |
| 58 | rosterStudentRepository.saveAll(invitedStudents); | |
| 59 | ||
| 60 | List<CourseStaff> invitedStaff = new ArrayList<>(); | |
| 61 | for (CourseStaff cs : courseStaffRepository.findAllByUser(currentUser)) { | |
| 62 |
1
1. disconnect : negated conditional → KILLED |
if (cs.getOrgStatus() == OrgStatus.INVITED) { |
| 63 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED |
cs.setOrgStatus(OrgStatus.JOINCOURSE); |
| 64 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setGithubId → KILLED |
cs.setGithubId(null); |
| 65 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setGithubLogin → KILLED |
cs.setGithubLogin(null); |
| 66 | invitedStaff.add(cs); | |
| 67 | } | |
| 68 | } | |
| 69 | courseStaffRepository.saveAll(invitedStaff); | |
| 70 | ||
| 71 | Authentication auth = context.getAuthentication(); | |
| 72 | List<? extends GrantedAuthority> removedAuthority = | |
| 73 | auth.getAuthorities().stream() | |
| 74 |
2
1. lambda$disconnect$0 : replaced boolean return with true for edu/ucsb/cs156/frontiers/controllers/GithubController::lambda$disconnect$0 → KILLED 2. lambda$disconnect$0 : negated conditional → KILLED |
.filter(r -> !"ROLE_GITHUB".equals(r.getAuthority())) |
| 75 | .toList(); | |
| 76 | OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) context.getAuthentication(); | |
| 77 |
1
1. disconnect : removed call to org/springframework/security/core/context/SecurityContext::setAuthentication → KILLED |
context.setAuthentication( |
| 78 | new OAuth2AuthenticationToken( | |
| 79 | (OidcUser) auth.getPrincipal(), | |
| 80 | removedAuthority, | |
| 81 | token.getAuthorizedClientRegistrationId())); | |
| 82 | SecurityContextHolder.setContext(context); | |
| 83 |
1
1. disconnect : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/GithubController::disconnect → KILLED |
return genericMessage("Disconnected from GitHub. You may now log in with a different account."); |
| 84 | } | |
| 85 | } | |
Mutations | ||
| 45 |
1.1 |
|
| 46 |
1.1 |
|
| 51 |
1.1 |
|
| 52 |
1.1 |
|
| 53 |
1.1 |
|
| 54 |
1.1 |
|
| 62 |
1.1 |
|
| 63 |
1.1 |
|
| 64 |
1.1 |
|
| 65 |
1.1 |
|
| 74 |
1.1 2.2 |
|
| 77 |
1.1 |
|
| 83 |
1.1 |