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