| 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.ArrayList; | |
| 8 | import java.util.Collection; | |
| 9 | import java.util.HashSet; | |
| 10 | import java.util.List; | |
| 11 | import java.util.Set; | |
| 12 | import lombok.extern.slf4j.Slf4j; | |
| 13 | import org.springframework.beans.factory.annotation.Value; | |
| 14 | import org.springframework.security.core.Authentication; | |
| 15 | import org.springframework.security.core.GrantedAuthority; | |
| 16 | import org.springframework.security.core.authority.SimpleGrantedAuthority; | |
| 17 | import org.springframework.security.core.context.SecurityContext; | |
| 18 | import org.springframework.security.core.context.SecurityContextHolder; | |
| 19 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | |
| 20 | import org.springframework.security.oauth2.core.oidc.user.OidcUser; | |
| 21 | import org.springframework.security.oauth2.core.user.OAuth2User; | |
| 22 | import org.springframework.stereotype.Component; | |
| 23 | import org.springframework.web.servlet.HandlerInterceptor; | |
| 24 | ||
| 25 | @Slf4j | |
| 26 | @Component | |
| 27 | public class RoleInterceptor implements HandlerInterceptor { | |
| 28 | ||
| 29 | private final AdminRepository adminRepository; | |
| 30 | ||
| 31 | private final ModeratorRepository moderatorRepository; | |
| 32 | ||
| 33 | @Value("#{'${app.admin.emails}'.split(',')}") | |
| 34 | private final List<String> adminEmails = new ArrayList<>(); | |
| 35 | ||
| 36 | public RoleInterceptor(AdminRepository adminRepository, ModeratorRepository moderatorRepository) { | |
| 37 | this.adminRepository = adminRepository; | |
| 38 | this.moderatorRepository = moderatorRepository; | |
| 39 | } | |
| 40 | ||
| 41 | @Override | |
| 42 | public boolean preHandle( | |
| 43 | HttpServletRequest request, HttpServletResponse response, Object handler) { | |
| 44 | SecurityContext securityContext = SecurityContextHolder.getContext(); | |
| 45 | Authentication authentication = securityContext.getAuthentication(); | |
| 46 | ||
| 47 |
1
1. preHandle : negated conditional → KILLED |
if (authentication instanceof OAuth2AuthenticationToken oauthToken) { |
| 48 | OAuth2User oauthUser = oauthToken.getPrincipal(); | |
| 49 | String email = | |
| 50 |
1
1. preHandle : negated conditional → KILLED |
oauthUser instanceof OidcUser oidcUser |
| 51 | ? oidcUser.getEmail() | |
| 52 | : oauthUser.getAttribute("email"); | |
| 53 | Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); | |
| 54 | Set<GrantedAuthority> revisedAuthorities = new HashSet<>(); | |
| 55 | ||
| 56 | authorities.stream() | |
| 57 | .filter( | |
| 58 | grantedAuth -> | |
| 59 |
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") |
| 60 |
1
1. lambda$preHandle$0 : negated conditional → KILLED |
&& !grantedAuth.getAuthority().equals("ROLE_MODERATOR")) |
| 61 |
1
1. preHandle : removed call to java/util/stream/Stream::forEach → KILLED |
.forEach(revisedAuthorities::add); |
| 62 | ||
| 63 |
2
1. preHandle : negated conditional → KILLED 2. preHandle : negated conditional → KILLED |
if (adminEmails.contains(email) || adminRepository.existsByEmail(email)) { |
| 64 | revisedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN")); | |
| 65 | } | |
| 66 |
1
1. preHandle : negated conditional → KILLED |
if (moderatorRepository.existsByEmail(email)) { |
| 67 | revisedAuthorities.add(new SimpleGrantedAuthority("ROLE_MODERATOR")); | |
| 68 | } | |
| 69 | ||
| 70 | Authentication newAuth = | |
| 71 | new OAuth2AuthenticationToken( | |
| 72 | oauthUser, revisedAuthorities, oauthToken.getAuthorizedClientRegistrationId()); | |
| 73 | ||
| 74 |
1
1. preHandle : removed call to org/springframework/security/core/context/SecurityContext::setAuthentication → KILLED |
securityContext.setAuthentication(newAuth); |
| 75 | } | |
| 76 |
1
1. preHandle : replaced boolean return with false for edu/ucsb/cs156/dining/interceptors/RoleInterceptor::preHandle → KILLED |
return true; |
| 77 | } | |
| 78 | } | |
Mutations | ||
| 47 |
1.1 |
|
| 50 |
1.1 |
|
| 59 |
1.1 2.2 |
|
| 60 |
1.1 |
|
| 61 |
1.1 |
|
| 63 |
1.1 2.2 |
|
| 66 |
1.1 |
|
| 74 |
1.1 |
|
| 76 |
1.1 |