| 1 | package edu.ucsb.cs156.courses.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | import edu.ucsb.cs156.courses.entities.User; | |
| 6 | import edu.ucsb.cs156.courses.errors.EntityNotFoundException; | |
| 7 | import edu.ucsb.cs156.courses.repositories.UserRepository; | |
| 8 | import io.swagger.v3.oas.annotations.Operation; | |
| 9 | import io.swagger.v3.oas.annotations.Parameter; | |
| 10 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 11 | import java.util.Arrays; | |
| 12 | import java.util.List; | |
| 13 | import java.util.Locale; | |
| 14 | import org.springframework.beans.factory.annotation.Autowired; | |
| 15 | import org.springframework.data.domain.PageRequest; | |
| 16 | import org.springframework.data.domain.Sort.Direction; | |
| 17 | import org.springframework.http.ResponseEntity; | |
| 18 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 19 | import org.springframework.web.bind.annotation.GetMapping; | |
| 20 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 21 | import org.springframework.web.bind.annotation.RequestParam; | |
| 22 | import org.springframework.web.bind.annotation.RestController; | |
| 23 | ||
| 24 | @Tag(name = "User information (admin only)") | |
| 25 | @RequestMapping("/api/admin/users") | |
| 26 | @RestController | |
| 27 | public class UsersController extends ApiController { | |
| 28 | @Autowired UserRepository userRepository; | |
| 29 | ||
| 30 | @Autowired ObjectMapper mapper; | |
| 31 | ||
| 32 | @Operation(summary = "Get a list of all users") | |
| 33 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 34 | @GetMapping("") | |
| 35 | public ResponseEntity<String> users() throws JsonProcessingException { | |
| 36 | Iterable<User> users = userRepository.findAll(); | |
| 37 | String body = mapper.writeValueAsString(users); | |
| 38 |
1
1. users : replaced return value with null for edu/ucsb/cs156/courses/controllers/UsersController::users → KILLED |
return ResponseEntity.ok().body(body); |
| 39 | } | |
| 40 | ||
| 41 | @Operation(summary = "Get a list of all users with pages") | |
| 42 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 43 | @GetMapping(params = {"page", "pageSize", "sortField", "sortDirection"}) | |
| 44 | public Iterable<User> usersPaged( | |
| 45 | @Parameter( | |
| 46 | name = "page", | |
| 47 | description = "what page of the data", | |
| 48 | example = "0", | |
| 49 | required = true) | |
| 50 | @RequestParam | |
| 51 | int page, | |
| 52 | @Parameter( | |
| 53 | name = "pageSize", | |
| 54 | description = "size of each page", | |
| 55 | example = "5", | |
| 56 | required = true) | |
| 57 | @RequestParam | |
| 58 | int pageSize, | |
| 59 | @Parameter( | |
| 60 | name = "sortField", | |
| 61 | description = "field to sort by", | |
| 62 | example = "email", | |
| 63 | required = true) | |
| 64 | @RequestParam | |
| 65 | String sortField, | |
| 66 | @Parameter( | |
| 67 | name = "sortDirection", | |
| 68 | description = "sort direction", | |
| 69 | example = "ASC", | |
| 70 | required = true) | |
| 71 | @RequestParam | |
| 72 | String sortDirection) | |
| 73 | throws JsonProcessingException { | |
| 74 | List<String> allowedSortFields = Arrays.asList("givenName", "familyName", "email"); | |
| 75 |
1
1. usersPaged : negated conditional → KILLED |
if (!allowedSortFields.contains(sortField)) { |
| 76 | throw new IllegalArgumentException( | |
| 77 | String.format( | |
| 78 | "%s is not a valid sort field. Valid values are %s", sortField, allowedSortFields)); | |
| 79 | } | |
| 80 | ||
| 81 | List<String> allowedSortDirections = Arrays.asList("ASC", "DESC"); | |
| 82 |
1
1. usersPaged : negated conditional → KILLED |
if (!allowedSortDirections.contains(sortDirection)) { |
| 83 | throw new IllegalArgumentException( | |
| 84 | String.format( | |
| 85 | "%s is not a valid sort direction. Valid values are %s", | |
| 86 | sortDirection, allowedSortDirections)); | |
| 87 | } | |
| 88 | ||
| 89 | Direction sortDirectionObject = Direction.ASC; | |
| 90 |
1
1. usersPaged : negated conditional → KILLED |
if (sortDirection.equals("DESC")) { |
| 91 | sortDirectionObject = Direction.DESC; | |
| 92 | } | |
| 93 | ||
| 94 | PageRequest pageRequest = PageRequest.of(page, pageSize, sortDirectionObject, sortField); | |
| 95 | ||
| 96 | Iterable<User> users = userRepository.findAll(pageRequest); | |
| 97 |
1
1. usersPaged : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/UsersController::usersPaged → KILLED |
return users; |
| 98 | } | |
| 99 | ||
| 100 | public static final int DEFAULT_SEARCH_LIMIT = 10; | |
| 101 | public static final int MAX_SEARCH_LIMIT = 50; | |
| 102 | ||
| 103 | @Operation( | |
| 104 | summary = "Search users by name or email (for type-ahead / autocomplete)", | |
| 105 | description = | |
| 106 | "Case-insensitive substring match on fullName, givenName, familyName and email. " | |
| 107 | + "Users with a field starting with the query are listed first. " | |
| 108 | + "A blank query returns an empty list.") | |
| 109 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 110 | @GetMapping("/search") | |
| 111 | public List<User> searchUsers( | |
| 112 | @Parameter( | |
| 113 | name = "query", | |
| 114 | description = "text to match against name or email", | |
| 115 | example = "con", | |
| 116 | required = true) | |
| 117 | @RequestParam | |
| 118 | String query, | |
| 119 | @Parameter( | |
| 120 | name = "limit", | |
| 121 | description = "maximum number of results (1-" + MAX_SEARCH_LIMIT + ")", | |
| 122 | example = "10") | |
| 123 | @RequestParam(defaultValue = "" + DEFAULT_SEARCH_LIMIT) | |
| 124 | int limit) { | |
| 125 |
4
1. searchUsers : negated conditional → KILLED 2. searchUsers : negated conditional → KILLED 3. searchUsers : changed conditional boundary → KILLED 4. searchUsers : changed conditional boundary → KILLED |
if (limit < 1 || limit > MAX_SEARCH_LIMIT) { |
| 126 | throw new IllegalArgumentException( | |
| 127 | String.format("limit must be between 1 and %d, but was %d", MAX_SEARCH_LIMIT, limit)); | |
| 128 | } | |
| 129 | String trimmed = query.trim(); | |
| 130 |
1
1. searchUsers : negated conditional → KILLED |
if (trimmed.isEmpty()) { |
| 131 | return List.of(); | |
| 132 | } | |
| 133 | String pattern = escapeLikePattern(trimmed.toLowerCase(Locale.ROOT)); | |
| 134 |
1
1. searchUsers : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/controllers/UsersController::searchUsers → KILLED |
return userRepository.searchByNameOrEmail(pattern, PageRequest.of(0, limit)); |
| 135 | } | |
| 136 | ||
| 137 | @Operation(summary = "Get a single user by id") | |
| 138 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 139 | @GetMapping(params = {"id"}) | |
| 140 | public User getUserById( | |
| 141 | @Parameter(name = "id", description = "id of the user", example = "1", required = true) | |
| 142 | @RequestParam | |
| 143 | long id) { | |
| 144 |
1
1. getUserById : replaced return value with null for edu/ucsb/cs156/courses/controllers/UsersController::getUserById → KILLED |
return userRepository |
| 145 | .findById(id) | |
| 146 |
1
1. lambda$getUserById$0 : replaced return value with null for edu/ucsb/cs156/courses/controllers/UsersController::lambda$getUserById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(User.class, id)); |
| 147 | } | |
| 148 | ||
| 149 | /** Escape the LIKE wildcards so user input is matched literally (escape char is backslash). */ | |
| 150 | static String escapeLikePattern(String text) { | |
| 151 |
1
1. escapeLikePattern : replaced return value with "" for edu/ucsb/cs156/courses/controllers/UsersController::escapeLikePattern → KILLED |
return text.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_"); |
| 152 | } | |
| 153 | } | |
Mutations | ||
| 38 |
1.1 |
|
| 75 |
1.1 |
|
| 82 |
1.1 |
|
| 90 |
1.1 |
|
| 97 |
1.1 |
|
| 125 |
1.1 2.2 3.3 4.4 |
|
| 130 |
1.1 |
|
| 134 |
1.1 |
|
| 144 |
1.1 |
|
| 146 |
1.1 |
|
| 151 |
1.1 |