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