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