| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 5 | import edu.ucsb.cs156.frontiers.entities.DokkuAccountTranslation; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.Team; | |
| 7 | import edu.ucsb.cs156.frontiers.entities.TeamMember; | |
| 8 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 9 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 10 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 11 | import edu.ucsb.cs156.frontiers.repositories.DokkuAccountTranslationRepository; | |
| 12 | import edu.ucsb.cs156.frontiers.repositories.TeamRepository; | |
| 13 | import edu.ucsb.cs156.frontiers.utilities.CanonicalFormConverter; | |
| 14 | import io.swagger.v3.oas.annotations.Operation; | |
| 15 | import io.swagger.v3.oas.annotations.Parameter; | |
| 16 | import io.swagger.v3.oas.annotations.media.Content; | |
| 17 | import io.swagger.v3.oas.annotations.media.Schema; | |
| 18 | import io.swagger.v3.oas.annotations.responses.ApiResponse; | |
| 19 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 20 | import java.io.IOException; | |
| 21 | import java.io.OutputStreamWriter; | |
| 22 | import java.io.Writer; | |
| 23 | import java.nio.charset.StandardCharsets; | |
| 24 | import java.util.Collection; | |
| 25 | import java.util.HashMap; | |
| 26 | import java.util.LinkedHashMap; | |
| 27 | import java.util.List; | |
| 28 | import java.util.Map; | |
| 29 | import java.util.Optional; | |
| 30 | import java.util.regex.Matcher; | |
| 31 | import java.util.regex.Pattern; | |
| 32 | import org.apache.commons.csv.CSVFormat; | |
| 33 | import org.apache.commons.csv.CSVPrinter; | |
| 34 | import org.springframework.beans.factory.annotation.Autowired; | |
| 35 | import org.springframework.http.HttpHeaders; | |
| 36 | import org.springframework.http.MediaType; | |
| 37 | import org.springframework.http.ResponseEntity; | |
| 38 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 39 | import org.springframework.web.bind.annotation.GetMapping; | |
| 40 | import org.springframework.web.bind.annotation.PutMapping; | |
| 41 | import org.springframework.web.bind.annotation.RequestBody; | |
| 42 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 43 | import org.springframework.web.bind.annotation.RequestParam; | |
| 44 | import org.springframework.web.bind.annotation.RestController; | |
| 45 | import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; | |
| 46 | ||
| 47 | /** | |
| 48 | * Endpoints that produce files for setting up access to the UCSB Dokku servers (dokku-00 through | |
| 49 | * dokku-18). | |
| 50 | */ | |
| 51 | @Tag(name = "Dokku") | |
| 52 | @RequestMapping("/api/dokku") | |
| 53 | @RestController | |
| 54 | public class DokkuController extends ApiController { | |
| 55 | ||
| 56 | /** The lowest numbered dokku instance, i.e. dokku-00. */ | |
| 57 | public static final int LOWEST_DOKKU_NUMBER = 0; | |
| 58 | ||
| 59 | /** The highest numbered dokku instance, i.e. dokku-18. */ | |
| 60 | public static final int HIGHEST_DOKKU_NUMBER = 18; | |
| 61 | ||
| 62 | /** | |
| 63 | * Matches team names of the form {@code xxx-yy}: anything, a literal hyphen, then exactly two | |
| 64 | * digits at the end. The two digits are captured as group 1 and are the dokku number. | |
| 65 | */ | |
| 66 | static final Pattern TEAM_NAME_PATTERN = Pattern.compile("^.*-(\\d{2})$"); | |
| 67 | ||
| 68 | /** | |
| 69 | * Every dokku instance name, keyed by its two digit suffix and ordered from {@link | |
| 70 | * #LOWEST_DOKKU_NUMBER} to {@link #HIGHEST_DOKKU_NUMBER}, e.g. {@code "07" -> "dokku-07"}. This | |
| 71 | * is the single place the range of dokku numbers is expanded. | |
| 72 | */ | |
| 73 | static final Map<String, String> DOKKU_NAME_BY_SUFFIX = dokkuNamesBySuffix(); | |
| 74 | ||
| 75 | private static Map<String, String> dokkuNamesBySuffix() { | |
| 76 | Map<String, String> names = new LinkedHashMap<>(); | |
| 77 | for (int n = LOWEST_DOKKU_NUMBER; n <= HIGHEST_DOKKU_NUMBER; n++) { | |
| 78 | String suffix = String.format("%02d", n); | |
| 79 | names.put(suffix, "dokku-" + suffix); | |
| 80 | } | |
| 81 | return names; | |
| 82 | } | |
| 83 | ||
| 84 | @Autowired private CourseRepository courseRepository; | |
| 85 | ||
| 86 | @Autowired private CourseStaffRepository courseStaffRepository; | |
| 87 | ||
| 88 | @Autowired private TeamRepository teamRepository; | |
| 89 | ||
| 90 | @Autowired private DokkuAccountTranslationRepository dokkuAccountTranslationRepository; | |
| 91 | ||
| 92 | /** | |
| 93 | * Returns every dokku instance name in order, {@code dokku-00} through {@code dokku-18}. | |
| 94 | * | |
| 95 | * @return the dokku instance names | |
| 96 | */ | |
| 97 | public static Collection<String> allDokkuNames() { | |
| 98 |
1
1. allDokkuNames : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/DokkuController::allDokkuNames → KILLED |
return DOKKU_NAME_BY_SUFFIX.values(); |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * Extracts the dokku username from an email address: everything before the first {@code @}. An | |
| 103 | * email with no {@code @} is returned unchanged. | |
| 104 | * | |
| 105 | * @param email the email address | |
| 106 | * @return the username | |
| 107 | */ | |
| 108 | public static String usernameFromEmail(String email) { | |
| 109 | int at = email.indexOf('@'); | |
| 110 |
3
1. usernameFromEmail : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/DokkuController::usernameFromEmail → KILLED 2. usernameFromEmail : changed conditional boundary → KILLED 3. usernameFromEmail : negated conditional → KILLED |
return at < 0 ? email : email.substring(0, at); |
| 111 | } | |
| 112 | ||
| 113 | /** | |
| 114 | * Returns the dokku username for an email: the translated username if there is a {@link | |
| 115 | * DokkuAccountTranslation} for the email (compared in canonical form), otherwise the part of the | |
| 116 | * email before the {@code @}. | |
| 117 | * | |
| 118 | * @param email the email address | |
| 119 | * @param translations map from canonical email to dokku username | |
| 120 | * @return the dokku username | |
| 121 | */ | |
| 122 | public static String usernameFor(String email, Map<String, String> translations) { | |
| 123 | String translated = translations.get(CanonicalFormConverter.convertToValidEmail(email)); | |
| 124 |
2
1. usernameFor : negated conditional → KILLED 2. usernameFor : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/DokkuController::usernameFor → KILLED |
return translated != null ? translated : usernameFromEmail(email); |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * Returns the dokku instance named by a team, if the team name has the form {@code xxx-yy} and | |
| 129 | * {@code yy} is between {@link #LOWEST_DOKKU_NUMBER} and {@link #HIGHEST_DOKKU_NUMBER} inclusive. | |
| 130 | * | |
| 131 | * @param teamName the team name, e.g. {@code "s26-07"} | |
| 132 | * @return the dokku instance name, e.g. {@code "dokku-07"}, or empty if the team does not | |
| 133 | * correspond to a dokku instance | |
| 134 | */ | |
| 135 | public static Optional<String> dokkuNameForTeam(String teamName) { | |
| 136 |
1
1. dokkuNameForTeam : negated conditional → KILLED |
if (teamName == null) { |
| 137 | return Optional.empty(); | |
| 138 | } | |
| 139 | Matcher matcher = TEAM_NAME_PATTERN.matcher(teamName); | |
| 140 |
1
1. dokkuNameForTeam : negated conditional → KILLED |
if (!matcher.matches()) { |
| 141 | return Optional.empty(); | |
| 142 | } | |
| 143 |
1
1. dokkuNameForTeam : replaced return value with Optional.empty for edu/ucsb/cs156/frontiers/controllers/DokkuController::dokkuNameForTeam → KILLED |
return Optional.ofNullable(DOKKU_NAME_BY_SUFFIX.get(matcher.group(1))); |
| 144 | } | |
| 145 | ||
| 146 | @Operation( | |
| 147 | summary = "Download dokku_users_list.csv for a course", | |
| 148 | description = | |
| 149 | "Returns a CSV file with no header line and one `username,dokku-nn` line per grant of" | |
| 150 | + " access. Every staff member of the course is granted access to every dokku" | |
| 151 | + " instance from dokku-" | |
| 152 | + LOWEST_DOKKU_NUMBER | |
| 153 | + " through dokku-" | |
| 154 | + HIGHEST_DOKKU_NUMBER | |
| 155 | + ". Each member of a team whose name ends in `-nn` (nn between " | |
| 156 | + LOWEST_DOKKU_NUMBER | |
| 157 | + " and " | |
| 158 | + HIGHEST_DOKKU_NUMBER | |
| 159 | + ") is granted access to dokku-nn; other teams are ignored. Usernames are the part" | |
| 160 | + " of the email before the @, unless the email has a dokku account translation, in" | |
| 161 | + " which case the translated username is used.", | |
| 162 | responses = { | |
| 163 | @ApiResponse( | |
| 164 | responseCode = "200", | |
| 165 | description = "CSV file", | |
| 166 | content = | |
| 167 | @Content( | |
| 168 | mediaType = "text/csv", | |
| 169 | schema = @Schema(type = "string", format = "binary"))), | |
| 170 | @ApiResponse(responseCode = "404", description = "Course not found"), | |
| 171 | @ApiResponse(responseCode = "500", description = "Internal Server Error") | |
| 172 | }) | |
| 173 | @GetMapping(value = "/dokku_users_list", produces = "text/csv") | |
| 174 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 175 | public ResponseEntity<StreamingResponseBody> dokkuUsersList( | |
| 176 | @Parameter(name = "courseId", description = "course id", example = "1") @RequestParam | |
| 177 | Long courseId) | |
| 178 | throws EntityNotFoundException { | |
| 179 | Course course = | |
| 180 | courseRepository | |
| 181 | .findById(courseId) | |
| 182 |
1
1. lambda$dokkuUsersList$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuController::lambda$dokkuUsersList$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 183 | ||
| 184 | StreamingResponseBody stream = | |
| 185 | (outputStream) -> { | |
| 186 | try (Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); | |
| 187 | CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)) { | |
| 188 |
1
1. lambda$dokkuUsersList$1 : removed call to edu/ucsb/cs156/frontiers/controllers/DokkuController::writeHeaderLines → KILLED |
writeHeaderLines(writer, course.getDokkuUsersListHeader()); |
| 189 | Map<String, String> translations = translationsByEmail(); | |
| 190 |
1
1. lambda$dokkuUsersList$1 : removed call to edu/ucsb/cs156/frontiers/controllers/DokkuController::writeStaffLines → KILLED |
writeStaffLines( |
| 191 | csvPrinter, courseStaffRepository.findByCourseId(courseId), translations); | |
| 192 |
1
1. lambda$dokkuUsersList$1 : removed call to edu/ucsb/cs156/frontiers/controllers/DokkuController::writeTeamLines → KILLED |
writeTeamLines( |
| 193 | csvPrinter, teamRepository.findByCourseIdOrderByNameAsc(courseId), translations); | |
| 194 | } | |
| 195 | }; | |
| 196 | ||
| 197 |
1
1. dokkuUsersList : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuController::dokkuUsersList → KILLED |
return ResponseEntity.ok() |
| 198 | .contentType(MediaType.parseMediaType("text/csv; charset=UTF-8")) | |
| 199 | .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=dokku_users_list.csv") | |
| 200 | .header(HttpHeaders.CONTENT_TYPE, "text/csv; charset=UTF-8") | |
| 201 | .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) | |
| 202 | .body(stream); | |
| 203 | } | |
| 204 | ||
| 205 | @Operation( | |
| 206 | summary = "Get the extra lines placed at the start of dokku_users_list.csv for a course", | |
| 207 | description = | |
| 208 | "Returns the course's dokkuUsersListHeader as plain text, or an empty body if none is" | |
| 209 | + " set.") | |
| 210 | @GetMapping(value = "/users_list_header", produces = "text/plain") | |
| 211 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 212 | public String getUsersListHeader( | |
| 213 | @Parameter(name = "courseId", description = "course id", example = "1") @RequestParam | |
| 214 | Long courseId) | |
| 215 | throws EntityNotFoundException { | |
| 216 | Course course = | |
| 217 | courseRepository | |
| 218 | .findById(courseId) | |
| 219 |
1
1. lambda$getUsersListHeader$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuController::lambda$getUsersListHeader$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 220 |
2
1. getUsersListHeader : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/DokkuController::getUsersListHeader → KILLED 2. getUsersListHeader : negated conditional → KILLED |
return course.getDokkuUsersListHeader() == null ? "" : course.getDokkuUsersListHeader(); |
| 221 | } | |
| 222 | ||
| 223 | @Operation( | |
| 224 | summary = "Set the extra lines placed at the start of dokku_users_list.csv for a course", | |
| 225 | description = | |
| 226 | "The request body (plain text, line breaks preserved) replaces the course's" | |
| 227 | + " dokkuUsersListHeader. A blank body clears it. Returns the saved text.") | |
| 228 | @PutMapping(value = "/users_list_header", consumes = "text/plain", produces = "text/plain") | |
| 229 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 230 | public String setUsersListHeader( | |
| 231 | @Parameter(name = "courseId", description = "course id", example = "1") @RequestParam | |
| 232 | Long courseId, | |
| 233 | @Parameter(name = "header") @RequestBody(required = false) String header) | |
| 234 | throws EntityNotFoundException { | |
| 235 | Course course = | |
| 236 | courseRepository | |
| 237 | .findById(courseId) | |
| 238 |
1
1. lambda$setUsersListHeader$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/DokkuController::lambda$setUsersListHeader$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Course.class, courseId)); |
| 239 |
2
1. setUsersListHeader : negated conditional → KILLED 2. setUsersListHeader : negated conditional → KILLED |
String normalized = header == null || header.isBlank() ? null : header; |
| 240 |
1
1. setUsersListHeader : removed call to edu/ucsb/cs156/frontiers/entities/Course::setDokkuUsersListHeader → KILLED |
course.setDokkuUsersListHeader(normalized); |
| 241 | courseRepository.save(course); | |
| 242 |
2
1. setUsersListHeader : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/DokkuController::setUsersListHeader → KILLED 2. setUsersListHeader : negated conditional → KILLED |
return normalized == null ? "" : normalized; |
| 243 | } | |
| 244 | ||
| 245 | /** | |
| 246 | * Writes the course's dokkuUsersListHeader before any generated lines: one line per non-blank | |
| 247 | * line of the header, stripped, with line endings normalized to the CSV record separator. The | |
| 248 | * lines are written verbatim (not as quoted CSV fields), since each is already a | |
| 249 | * username,dokku-nn entry. Nothing is written when the header is null. | |
| 250 | */ | |
| 251 | private static void writeHeaderLines(Writer writer, String header) throws IOException { | |
| 252 |
1
1. writeHeaderLines : negated conditional → KILLED |
if (header == null) { |
| 253 | return; | |
| 254 | } | |
| 255 | for (String line : header.split("\\R")) { | |
| 256 | String trimmed = line.strip(); | |
| 257 |
1
1. writeHeaderLines : negated conditional → KILLED |
if (!trimmed.isEmpty()) { |
| 258 |
1
1. writeHeaderLines : removed call to java/io/Writer::write → KILLED |
writer.write(trimmed); |
| 259 |
1
1. writeHeaderLines : removed call to java/io/Writer::write → KILLED |
writer.write(CSVFormat.DEFAULT.getRecordSeparator()); |
| 260 | } | |
| 261 | } | |
| 262 | } | |
| 263 | ||
| 264 | /** Loads every {@link DokkuAccountTranslation} into a map from canonical email to username. */ | |
| 265 | private Map<String, String> translationsByEmail() { | |
| 266 | Map<String, String> translations = new HashMap<>(); | |
| 267 | for (DokkuAccountTranslation translation : dokkuAccountTranslationRepository.findAll()) { | |
| 268 | translations.put( | |
| 269 | CanonicalFormConverter.convertToValidEmail(translation.getEmail()), | |
| 270 | translation.getUsername()); | |
| 271 | } | |
| 272 |
1
1. translationsByEmail : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/DokkuController::translationsByEmail → KILLED |
return translations; |
| 273 | } | |
| 274 | ||
| 275 | /** | |
| 276 | * Writes one line per staff member per dokku instance: each staff member gets access to every | |
| 277 | * instance from {@link #LOWEST_DOKKU_NUMBER} through {@link #HIGHEST_DOKKU_NUMBER}. | |
| 278 | */ | |
| 279 | private static void writeStaffLines( | |
| 280 | CSVPrinter csvPrinter, Iterable<CourseStaff> staff, Map<String, String> translations) | |
| 281 | throws IOException { | |
| 282 | for (CourseStaff member : staff) { | |
| 283 | String username = usernameFor(member.getEmail(), translations); | |
| 284 | for (String dokku : allDokkuNames()) { | |
| 285 |
1
1. writeStaffLines : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord(username, dokku); |
| 286 | } | |
| 287 | } | |
| 288 | } | |
| 289 | ||
| 290 | /** | |
| 291 | * Writes one line per member of each team whose name maps to a dokku instance (see {@link | |
| 292 | * #dokkuNameForTeam(String)}); teams that do not map to a dokku instance are skipped. | |
| 293 | */ | |
| 294 | private static void writeTeamLines( | |
| 295 | CSVPrinter csvPrinter, Iterable<Team> teams, Map<String, String> translations) | |
| 296 | throws IOException { | |
| 297 | for (Team team : teams) { | |
| 298 | Optional<String> dokku = dokkuNameForTeam(team.getName()); | |
| 299 |
1
1. writeTeamLines : negated conditional → KILLED |
if (dokku.isEmpty()) { |
| 300 | continue; | |
| 301 | } | |
| 302 | List<TeamMember> members = team.getTeamMembers(); | |
| 303 |
1
1. writeTeamLines : negated conditional → KILLED |
if (members == null) { |
| 304 | continue; | |
| 305 | } | |
| 306 | for (TeamMember member : members) { | |
| 307 |
1
1. writeTeamLines : removed call to org/apache/commons/csv/CSVPrinter::printRecord → KILLED |
csvPrinter.printRecord( |
| 308 | usernameFor(member.getRosterStudent().getEmail(), translations), dokku.get()); | |
| 309 | } | |
| 310 | } | |
| 311 | } | |
| 312 | } | |
Mutations | ||
| 98 |
1.1 |
|
| 110 |
1.1 2.2 3.3 |
|
| 124 |
1.1 2.2 |
|
| 136 |
1.1 |
|
| 140 |
1.1 |
|
| 143 |
1.1 |
|
| 182 |
1.1 |
|
| 188 |
1.1 |
|
| 190 |
1.1 |
|
| 192 |
1.1 |
|
| 197 |
1.1 |
|
| 219 |
1.1 |
|
| 220 |
1.1 2.2 |
|
| 238 |
1.1 |
|
| 239 |
1.1 2.2 |
|
| 240 |
1.1 |
|
| 242 |
1.1 2.2 |
|
| 252 |
1.1 |
|
| 257 |
1.1 |
|
| 258 |
1.1 |
|
| 259 |
1.1 |
|
| 272 |
1.1 |
|
| 285 |
1.1 |
|
| 299 |
1.1 |
|
| 303 |
1.1 |
|
| 307 |
1.1 |