| 1 | package edu.ucsb.cs156.dining.interceptors; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.dining.repositories.AdminRepository; | |
| 4 | import edu.ucsb.cs156.dining.repositories.ModeratorRepository; | |
| 5 | import jakarta.servlet.http.HttpServletRequest; | |
| 6 | import jakarta.servlet.http.HttpServletResponse; | |
| 7 | import java.util.Collection; | |
| 8 | import java.util.Set; | |
| 9 | import java.util.stream.Collectors; | |
| 10 | import lombok.extern.slf4j.Slf4j; | |
| 11 | import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | import org.springframework.security.core.Authentication; | |
| 13 | import org.springframework.security.core.GrantedAuthority; | |
| 14 | import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
| 15 | import org.springframework.security.core.context.SecurityContextHolder; | |
| 16 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | |
| 17 | import org.springframework.security.oauth2.core.user.OAuth2User; | |
| 18 | import org.springframework.stereotype.Component; | |
| 19 | import org.springframework.web.servlet.HandlerInterceptor; | |
| 20 | ||
| 21 | @Slf4j | |
| 22 | @Component | |
| 23 | public class RoleInterceptor implements HandlerInterceptor { | |
| 24 | ||
| 25 | @Autowired AdminRepository adminRepository; | |
| 26 | ||
| 27 | @Autowired ModeratorRepository moderatorRepository; | |
| 28 | ||
| 29 | @Override | |
| 30 | public boolean preHandle( | |
| 31 | HttpServletRequest request, HttpServletResponse response, Object handler) { | |
| 32 | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | |
| 33 | ||
| 34 |
1
1. preHandle : negated conditional → KILLED |
if (authentication instanceof OAuth2AuthenticationToken) { |
| 35 | OAuth2User principal = ((OAuth2AuthenticationToken) authentication).getPrincipal(); | |
| 36 | String email = principal.getAttribute("email"); | |
| 37 | ||
| 38 | Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); | |
| 39 | ||
| 40 | Set<GrantedAuthority> revisedAuthorities = | |
| 41 | authorities.stream() | |
| 42 | .filter( | |
| 43 | grantedAuth -> | |
| 44 |
2
1. lambda$preHandle$0 : replaced boolean return with true for edu/ucsb/cs156/dining/interceptors/RoleInterceptor::lambda$preHandle$0 → KILLED 2. lambda$preHandle$0 : negated conditional → KILLED |
!grantedAuth.getAuthority().equals("ROLE_ADMIN") |
| 45 |
1
1. lambda$preHandle$0 : negated conditional → KILLED |
&& !grantedAuth.getAuthority().equals("ROLE_MODERATOR")) |
| 46 | .collect(Collectors.toSet()); | |
| 47 | ||
| 48 |
1
1. preHandle : negated conditional → KILLED |
if (adminRepository.existsByEmail(email)) { |
| 49 | revisedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN")); | |
| 50 | } | |
| 51 | ||
| 52 |
1
1. preHandle : negated conditional → KILLED |
if (moderatorRepository.existsByEmail(email)) { |
| 53 | revisedAuthorities.add(new SimpleGrantedAuthority("ROLE_MODERATOR")); | |
| 54 | } | |
| 55 | ||
| 56 | Authentication newAuth = | |
| 57 | new OAuth2AuthenticationToken( | |
| 58 | principal, | |
| 59 | revisedAuthorities, | |
| 60 | (((OAuth2AuthenticationToken) authentication).getAuthorizedClientRegistrationId())); | |
| 61 | ||
| 62 | SecurityContextHolder.getContext().setAuthentication(newAuth); | |
| 63 | } | |
| 64 | ||
| 65 |
1
1. preHandle : replaced boolean return with false for edu/ucsb/cs156/dining/interceptors/RoleInterceptor::preHandle → KILLED |
return true; |
| 66 | } | |
| 67 | } | |
Mutations | ||
| 34 |
1.1 |
|
| 44 |
1.1 2.2 |
|
| 45 |
1.1 |
|
| 48 |
1.1 |
|
| 52 |
1.1 |
|
| 65 |
1.1 |