| 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.List; | |
| 11 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 12 | import org.springframework.security.core.Authentication; | |
| 13 | import org.springframework.security.core.GrantedAuthority; | |
| 14 | import org.springframework.security.core.context.SecurityContext; | |
| 15 | import org.springframework.security.core.context.SecurityContextHolder; | |
| 16 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | |
| 17 | import org.springframework.security.oauth2.core.oidc.user.OidcUser; | |
| 18 | import org.springframework.transaction.annotation.Transactional; | |
| 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 | @Transactional | |
| 42 | @PreAuthorize("hasRole('ROLE_GITHUB')") | |
| 43 | @DeleteMapping("/disconnect") | |
| 44 | public Object disconnect(SecurityContext context) { | |
| 45 | User currentUser = getCurrentUser().getUser(); | |
| 46 | ||
| 47 | for (RosterStudent student : rosterStudentRepository.findAllByUser(currentUser)) { | |
| 48 |
1
1. disconnect : negated conditional → KILLED |
if (student.getOrgStatus() == OrgStatus.INVITED) { |
| 49 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setOrgStatus → KILLED |
student.setOrgStatus(OrgStatus.JOINCOURSE); |
| 50 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setGithubId → KILLED |
student.setGithubId(null); |
| 51 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setGithubLogin → KILLED |
student.setGithubLogin(null); |
| 52 | rosterStudentRepository.save(student); | |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 | for (CourseStaff staff : courseStaffRepository.findAllByUser(currentUser)) { | |
| 57 |
1
1. disconnect : negated conditional → KILLED |
if (staff.getOrgStatus() == OrgStatus.INVITED) { |
| 58 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setOrgStatus → KILLED |
staff.setOrgStatus(OrgStatus.JOINCOURSE); |
| 59 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setGithubId → KILLED |
staff.setGithubId(null); |
| 60 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setGithubLogin → KILLED |
staff.setGithubLogin(null); |
| 61 | courseStaffRepository.save(staff); | |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/User::setGithubId → KILLED |
currentUser.setGithubId(null); |
| 66 |
1
1. disconnect : removed call to edu/ucsb/cs156/frontiers/entities/User::setGithubLogin → KILLED |
currentUser.setGithubLogin(null); |
| 67 | userRepository.save(currentUser); | |
| 68 | ||
| 69 | Authentication auth = context.getAuthentication(); | |
| 70 | List<? extends GrantedAuthority> removedAuthority = | |
| 71 | auth.getAuthorities().stream() | |
| 72 |
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())) |
| 73 | .toList(); | |
| 74 | ||
| 75 | OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) context.getAuthentication(); | |
| 76 | ||
| 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 | ||
| 83 | SecurityContextHolder.setContext(context); | |
| 84 | ||
| 85 |
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."); |
| 86 | } | |
| 87 | } | |
Mutations | ||
| 48 |
1.1 |
|
| 49 |
1.1 |
|
| 50 |
1.1 |
|
| 51 |
1.1 |
|
| 57 |
1.1 |
|
| 58 |
1.1 |
|
| 59 |
1.1 |
|
| 60 |
1.1 |
|
| 65 |
1.1 |
|
| 66 |
1.1 |
|
| 72 |
1.1 2.2 |
|
| 77 |
1.1 |
|
| 85 |
1.1 |