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