| 1 | package edu.ucsb.cs156.example.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | import edu.ucsb.cs156.example.entities.User; | |
| 6 | import edu.ucsb.cs156.example.repositories.UserRepository; | |
| 7 | import io.swagger.v3.oas.annotations.Operation; | |
| 8 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.http.ResponseEntity; | |
| 11 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 12 | import org.springframework.web.bind.annotation.GetMapping; | |
| 13 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 14 | import org.springframework.web.bind.annotation.RestController; | |
| 15 | ||
| 16 | /** | |
| 17 |  * This is a REST controller for getting information about the users. | |
| 18 |  * | |
| 19 |  * <p>These endpoints are only accessible to users with the role "ROLE_ADMIN". | |
| 20 |  */ | |
| 21 | @Tag(name = "User information (admin only)") | |
| 22 | @RequestMapping("/api/admin/users") | |
| 23 | @RestController | |
| 24 | public class UsersController extends ApiController { | |
| 25 |   @Autowired UserRepository userRepository; | |
| 26 | ||
| 27 |   @Autowired ObjectMapper mapper; | |
| 28 | ||
| 29 |   /** | |
| 30 |    * This method returns a list of all users. Accessible only to users with the role "ROLE_ADMIN". | |
| 31 |    * | |
| 32 |    * @return a list of all users | |
| 33 |    * @throws JsonProcessingException if there is an error processing the JSON | |
| 34 |    */ | |
| 35 |   @Operation(summary = "Get a list of all users") | |
| 36 |   @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 37 |   @GetMapping("") | |
| 38 |   public ResponseEntity<String> users() throws JsonProcessingException { | |
| 39 |     Iterable<User> users = userRepository.findAll(); | |
| 40 |     String body = mapper.writeValueAsString(users); | |
| 41 | 1
1. users : replaced return value with null for edu/ucsb/cs156/example/controllers/UsersController::users → KILLED |     return ResponseEntity.ok().body(body); | 
| 42 |   } | |
| 43 | } | |
| Mutations | ||
| 41 | 1.1 |