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