| 1 | package edu.ucsb.cs156.dining.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.dining.entities.User; | |
| 4 | import edu.ucsb.cs156.dining.models.CurrentUser; | |
| 5 | import edu.ucsb.cs156.dining.models.CurrentUserDTO; | |
| 6 | import edu.ucsb.cs156.dining.models.UserDTO; | |
| 7 | import edu.ucsb.cs156.dining.repositories.AdminRepository; | |
| 8 | import edu.ucsb.cs156.dining.repositories.ModeratorRepository; | |
| 9 | import io.swagger.v3.oas.annotations.Operation; | |
| 10 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 11 | import java.util.ArrayList; | |
| 12 | import java.util.List; | |
| 13 | import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | import org.springframework.beans.factory.annotation.Value; | |
| 15 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 16 | import org.springframework.web.bind.annotation.GetMapping; | |
| 17 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 18 | import org.springframework.web.bind.annotation.RestController; | |
| 19 | ||
| 20 | /** This is a REST controller for getting information about the current user. */ | |
| 21 | @Tag(name = "Current User Information") | |
| 22 | @RequestMapping("/api/currentUser") | |
| 23 | @RestController | |
| 24 | public class UserInfoController extends ApiController { | |
| 25 | ||
| 26 | @Value("#{'${app.admin.emails}'.split(',')}") | |
| 27 | private final List<String> adminEmails = new ArrayList<>(); | |
| 28 | ||
| 29 | @Autowired AdminRepository adminRepository; | |
| 30 | ||
| 31 | @Autowired ModeratorRepository moderatorRepository; | |
| 32 | ||
| 33 | /** | |
| 34 | * This method returns the current user. | |
| 35 | * | |
| 36 | * @return the current user | |
| 37 | */ | |
| 38 | @Operation(summary = "Get information about current user") | |
| 39 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 40 | @GetMapping("") | |
| 41 | public CurrentUserDTO currentUser() { | |
| 42 | CurrentUser currentUser = super.getCurrentUser(); | |
| 43 | User user = currentUser.getUser(); | |
| 44 | UserDTO userDTO = | |
| 45 | new UserDTO( | |
| 46 | user, | |
| 47 |
2
1. currentUser : negated conditional → KILLED 2. currentUser : negated conditional → KILLED |
adminEmails.contains(user.getEmail()) || adminRepository.existsByEmail(user.getEmail()), |
| 48 | moderatorRepository.existsByEmail(user.getEmail())); | |
| 49 |
1
1. currentUser : replaced return value with null for edu/ucsb/cs156/dining/controllers/UserInfoController::currentUser → KILLED |
return new CurrentUserDTO(userDTO, currentUser.getRoles()); |
| 50 | } | |
| 51 | } | |
Mutations | ||
| 47 |
1.1 2.2 |
|
| 49 |
1.1 |