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