RoleInterceptor.java

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

Mutations

42

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriversControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriversControllerTests]/[method:users__logged_out_all()]
negated conditional → KILLED

46

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests]/[method:updates_nothing_when_user_not_present()]
negated conditional → KILLED

50

1.1
Location : lambda$preHandle$0
Killed by : edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests]/[method:updates_driver_role_when_user_driver_false()]
negated conditional → KILLED

2.2
Location : lambda$preHandle$0
Killed by : edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests]/[method:updates_driver_role_when_user_driver_false()]
replaced boolean return with true for edu/ucsb/cs156/gauchoride/interceptors/RoleInterceptor::lambda$preHandle$0 → KILLED

51

1.1
Location : lambda$preHandle$0
Killed by : edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests]/[method:updates_driver_role_when_user_driver_false()]
negated conditional → KILLED

52

1.1
Location : lambda$preHandle$0
Killed by : edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests]/[method:updates_driver_role_when_user_driver_false()]
negated conditional → KILLED

54

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests]/[method:updates_driver_role_when_user_driver_false()]
negated conditional → KILLED

57

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests]/[method:updates_driver_role_when_user_driver_false()]
negated conditional → KILLED

60

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests]/[method:updates_driver_role_when_user_driver_false()]
negated conditional → KILLED

65

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.interceptors.RoleInterceptorTests]/[method:updates_driver_role_when_user_driver_false()]
removed call to org/springframework/security/core/context/SecurityContext::setAuthentication → KILLED

68

1.1
Location : preHandle
Killed by : edu.ucsb.cs156.gauchoride.controllers.DriversControllerTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.gauchoride.controllers.DriversControllerTests]/[method:users__logged_out_all()]
replaced boolean return with false for edu/ucsb/cs156/gauchoride/interceptors/RoleInterceptor::preHandle → KILLED

Active mutators

Tests examined


Report generated by PIT 1.7.3