| 1 | package edu.ucsb.cs156.dining.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | import edu.ucsb.cs156.dining.entities.User; | |
| 6 | import edu.ucsb.cs156.dining.errors.EntityNotFoundException; | |
| 7 | import edu.ucsb.cs156.dining.models.CurrentUser; | |
| 8 | import edu.ucsb.cs156.dining.models.UserDTO; | |
| 9 | import edu.ucsb.cs156.dining.repositories.AdminRepository; | |
| 10 | import edu.ucsb.cs156.dining.repositories.ModeratorRepository; | |
| 11 | import edu.ucsb.cs156.dining.repositories.UserRepository; | |
| 12 | import edu.ucsb.cs156.dining.statuses.ModerationStatus; | |
| 13 | import io.swagger.v3.oas.annotations.Operation; | |
| 14 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 15 | import java.time.LocalDate; | |
| 16 | import java.util.ArrayList; | |
| 17 | import java.util.List; | |
| 18 | import java.util.stream.StreamSupport; | |
| 19 | import org.springframework.beans.factory.annotation.Autowired; | |
| 20 | import org.springframework.beans.factory.annotation.Value; | |
| 21 | import org.springframework.http.HttpStatus; | |
| 22 | import org.springframework.http.ResponseEntity; | |
| 23 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 24 | import org.springframework.web.bind.annotation.*; | |
| 25 | import org.springframework.web.bind.annotation.GetMapping; | |
| 26 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 27 | import org.springframework.web.bind.annotation.RestController; | |
| 28 | import org.springframework.web.server.ResponseStatusException; | |
| 29 | ||
| 30 | /** | |
| 31 | * This is a REST controller for getting information about the users. | |
| 32 | * | |
| 33 | * <p>These endpoints are only accessible to users with the role "ROLE_ADMIN". | |
| 34 | */ | |
| 35 | @Tag(name = "User information (admin only)") | |
| 36 | @RequestMapping("/api") | |
| 37 | @RestController | |
| 38 | public class UsersController extends ApiController { | |
| 39 | ||
| 40 | @Value("#{'${app.admin.emails}'.split(',')}") | |
| 41 | private final List<String> adminEmails = new ArrayList<>(); | |
| 42 | ||
| 43 | @Autowired UserRepository userRepository; | |
| 44 | ||
| 45 | @Autowired AdminRepository adminRepository; | |
| 46 | ||
| 47 | @Autowired ModeratorRepository moderatorRepository; | |
| 48 | ||
| 49 | @Autowired ObjectMapper mapper; | |
| 50 | ||
| 51 | /** | |
| 52 | * This method returns a list of all users. Accessible only to users with the role "ROLE_ADMIN". | |
| 53 | * | |
| 54 | * @return a list of all users | |
| 55 | * @throws JsonProcessingException if there is an error processing the JSON | |
| 56 | */ | |
| 57 | @Operation(summary = "Get a list of all users") | |
| 58 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 59 | @GetMapping("/admin/users") | |
| 60 | public ResponseEntity<String> users() throws JsonProcessingException { | |
| 61 | ||
| 62 | Iterable<User> users = userRepository.findAll(); | |
| 63 | List<UserDTO> userDTOs = | |
| 64 | StreamSupport.stream(users.spliterator(), false).map(this::userDTO).toList(); | |
| 65 | String body = mapper.writeValueAsString(userDTOs); | |
| 66 |
1
1. users : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::users → KILLED |
return ResponseEntity.ok().body(body); |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * This method returns list of all users with a proposed alias. | |
| 71 | * | |
| 72 | * @return a list of users with a proposed alias | |
| 73 | * @throws JsonProcessingException if there is an error processing the JSON | |
| 74 | */ | |
| 75 | @Operation(summary = "Get a list of all users with a proposed alias") | |
| 76 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 77 | @GetMapping("/admin/usersWithProposedAlias") | |
| 78 | public ResponseEntity<String> getUsersWithProposedAlias() throws JsonProcessingException { | |
| 79 | Iterable<User> users = userRepository.findByProposedAliasNotNull(); | |
| 80 | String body = mapper.writeValueAsString(users); | |
| 81 |
1
1. getUsersWithProposedAlias : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::getUsersWithProposedAlias → KILLED |
return ResponseEntity.ok().body(body); |
| 82 | } | |
| 83 | ||
| 84 | /** Get all users whose proposed alias is awaiting moderation. */ | |
| 85 | @Operation(summary = "Get all aliases needing moderation") | |
| 86 | @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')") | |
| 87 | @GetMapping("/admin/users/needsmoderation") | |
| 88 | public ResponseEntity<String> getAliasesNeedingModeration() throws JsonProcessingException { | |
| 89 | ||
| 90 | Iterable<User> users = | |
| 91 | userRepository.findByStatusAndProposedAliasNotNull(ModerationStatus.AWAITING_REVIEW); | |
| 92 | ||
| 93 | String body = mapper.writeValueAsString(users); | |
| 94 | ||
| 95 |
1
1. getAliasesNeedingModeration : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::getAliasesNeedingModeration → KILLED |
return ResponseEntity.ok().body(body); |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * This method allows the user to update their alias. | |
| 100 | * | |
| 101 | * @param proposedAlias the new alias | |
| 102 | * @return the updated user | |
| 103 | */ | |
| 104 | @Operation(summary = "Update proposed alias of the current user") | |
| 105 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 106 | @PostMapping("/currentUser/updateAlias") | |
| 107 | public ResponseEntity<User> updateProposedAlias(@RequestParam String proposedAlias) { | |
| 108 | CurrentUser currentUser = super.getCurrentUser(); | |
| 109 | User user = currentUser.getUser(); | |
| 110 | ||
| 111 |
1
1. updateProposedAlias : negated conditional → KILLED |
if (userRepository.findByAlias(proposedAlias).isPresent()) { |
| 112 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Alias already in use."); | |
| 113 | } | |
| 114 | ||
| 115 |
1
1. updateProposedAlias : removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED |
user.setProposedAlias(proposedAlias); |
| 116 |
1
1. updateProposedAlias : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED |
user.setStatus(ModerationStatus.AWAITING_REVIEW); |
| 117 | User savedUser = userRepository.save(user); | |
| 118 | ||
| 119 |
1
1. updateProposedAlias : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::updateProposedAlias → KILLED |
return ResponseEntity.ok(savedUser); |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * This method allows an admin to update the moderation status of a user's alias. | |
| 124 | * | |
| 125 | * @param id the id of the user to update | |
| 126 | * @param approved the new moderation status | |
| 127 | * @return the updated user | |
| 128 | */ | |
| 129 | @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_MODERATOR')") | |
| 130 | @PutMapping("/currentUser/updateAliasModeration") | |
| 131 | public User updateAliasModeration(@RequestParam long id, @RequestParam Boolean approved) { | |
| 132 | ||
| 133 | User user = | |
| 134 |
1
1. lambda$updateAliasModeration$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::lambda$updateAliasModeration$0 → KILLED |
userRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
| 135 | ||
| 136 |
1
1. updateAliasModeration : negated conditional → KILLED |
if (approved) { |
| 137 |
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setAlias → KILLED |
user.setAlias(user.getProposedAlias()); |
| 138 |
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED |
user.setStatus(ModerationStatus.APPROVED); |
| 139 |
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setDateApproved → KILLED |
user.setDateApproved(LocalDate.now()); |
| 140 |
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED |
user.setProposedAlias(null); |
| 141 | } else { | |
| 142 |
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED |
user.setStatus(ModerationStatus.REJECTED); |
| 143 | } | |
| 144 | ||
| 145 | userRepository.save(user); | |
| 146 | ||
| 147 |
1
1. updateAliasModeration : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::updateAliasModeration → KILLED |
return user; |
| 148 | } | |
| 149 | ||
| 150 | private UserDTO userDTO(User user) { | |
| 151 | String email = user.getEmail(); | |
| 152 |
2
1. userDTO : negated conditional → KILLED 2. userDTO : negated conditional → KILLED |
boolean isAdmin = adminEmails.contains(email) || adminRepository.existsByEmail(email); |
| 153 | boolean isModerator = moderatorRepository.existsByEmail(email); | |
| 154 |
1
1. userDTO : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::userDTO → KILLED |
return new UserDTO(user, isAdmin, isModerator); |
| 155 | } | |
| 156 | } | |
Mutations | ||
| 66 |
1.1 |
|
| 81 |
1.1 |
|
| 95 |
1.1 |
|
| 111 |
1.1 |
|
| 115 |
1.1 |
|
| 116 |
1.1 |
|
| 119 |
1.1 |
|
| 134 |
1.1 |
|
| 136 |
1.1 |
|
| 137 |
1.1 |
|
| 138 |
1.1 |
|
| 139 |
1.1 |
|
| 140 |
1.1 |
|
| 142 |
1.1 |
|
| 147 |
1.1 |
|
| 152 |
1.1 2.2 |
|
| 154 |
1.1 |