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