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