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