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