| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import java.security.NoSuchAlgorithmException; | |
| 4 | import java.security.spec.InvalidKeySpecException; | |
| 5 | import java.util.Map; | |
| 6 | import java.util.Optional; | |
| 7 | import java.util.List; | |
| 8 | ||
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.http.HttpHeaders; | |
| 11 | import org.springframework.http.HttpStatus; | |
| 12 | import org.springframework.http.ResponseEntity; | |
| 13 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 14 | import org.springframework.web.bind.annotation.ExceptionHandler; | |
| 15 | import org.springframework.web.bind.annotation.GetMapping; | |
| 16 | import org.springframework.web.bind.annotation.PostMapping; | |
| 17 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 18 | import org.springframework.web.bind.annotation.RequestParam; | |
| 19 | import org.springframework.web.bind.annotation.ResponseStatus; | |
| 20 | import org.springframework.web.bind.annotation.RestController; | |
| 21 | ||
| 22 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 23 | ||
| 24 | import edu.ucsb.cs156.frontiers.entities.Admin; | |
| 25 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 26 | import edu.ucsb.cs156.frontiers.errors.InvalidInstallationTypeException; | |
| 27 | import edu.ucsb.cs156.frontiers.models.CurrentUser; | |
| 28 | import edu.ucsb.cs156.frontiers.repositories.AdminRepository; | |
| 29 | import edu.ucsb.cs156.frontiers.services.OrganizationLinkerService; | |
| 30 | import io.swagger.v3.oas.annotations.Operation; | |
| 31 | import io.swagger.v3.oas.annotations.Parameter; | |
| 32 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 33 | import lombok.extern.slf4j.Slf4j; | |
| 34 | ||
| 35 | import org.springframework.beans.factory.annotation.Value; | |
| 36 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 37 | ||
| 38 | @Tag(name = "Admins") | |
| 39 | @RequestMapping("/api/admins") | |
| 40 | @RestController | |
| 41 | @Slf4j | |
| 42 | public class AdminsController extends ApiController { | |
| 43 | | |
| 44 | @Autowired | |
| 45 | private AdminRepository adminRepository; | |
| 46 | ||
| 47 | @Value("${admin.emails:}") | |
| 48 | private String adminEmails; | |
| 49 | ||
| 50 | /** | |
| 51 | * This method creates a new Admin. | |
| 52 | * | |
| 53 | * @param email the email of the admin | |
| 54 | * @return the created admin | |
| 55 | */ | |
| 56 | ||
| 57 | @Operation(summary = "Create a new admin") | |
| 58 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 59 | @PostMapping("/post") | |
| 60 | public Admin postAdmin(@Parameter(name = "email") @RequestParam String email) { | |
| 61 | Admin admin = Admin.builder() | |
| 62 | .email(email) | |
| 63 | .build(); | |
| 64 | Admin savedAdmin = adminRepository.save(admin); | |
| 65 |
1
1. postAdmin : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminsController::postAdmin → KILLED |
return savedAdmin; |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * This method returns a list of admins. | |
| 70 | * @return a list of all admins. | |
| 71 | */ | |
| 72 | @Operation(summary = "List all admins") | |
| 73 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 74 | @GetMapping("/all") | |
| 75 | public Iterable<Admin> allAdmins( | |
| 76 | ) { | |
| 77 | Iterable<Admin> admins = adminRepository.findAll(); | |
| 78 |
1
1. allAdmins : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/AdminsController::allAdmins → KILLED |
return admins; |
| 79 | } | |
| 80 | ||
| 81 | /** | |
| 82 | * This method deletes an admin. | |
| 83 | * @param email the email of the admin | |
| 84 | * @return the deleted admin | |
| 85 | */ | |
| 86 | @Operation(summary = "Delete admin by email (unless in ADMIN_EMAILS)") | |
| 87 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 88 | @DeleteMapping("") | |
| 89 | public String deleteAdmin( | |
| 90 | @Parameter(name = "email") @RequestParam String email) { | |
| 91 | ||
| 92 | List<String> permanentAdmins = List.of(adminEmails.split(",")); | |
| 93 | ||
| 94 |
1
1. deleteAdmin : negated conditional → KILLED |
if (permanentAdmins.contains(email)) { |
| 95 | throw new UnsupportedOperationException("Cannot delete permanent admin: " + email); | |
| 96 | } | |
| 97 | ||
| 98 | Admin admin = adminRepository.findById(email) | |
| 99 |
1
1. lambda$deleteAdmin$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminsController::lambda$deleteAdmin$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Admin.class, email)); |
| 100 | ||
| 101 |
1
1. deleteAdmin : removed call to edu/ucsb/cs156/frontiers/repositories/AdminRepository::delete → KILLED |
adminRepository.delete(admin); |
| 102 |
1
1. deleteAdmin : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/AdminsController::deleteAdmin → KILLED |
return String.format("Admin with email %s deleted", email); |
| 103 | } | |
| 104 | ||
| 105 | @ExceptionHandler(UnsupportedOperationException.class) | |
| 106 | @ResponseStatus(HttpStatus.FORBIDDEN) | |
| 107 | public Object handleUnsupportedOperation(Throwable e) { | |
| 108 |
1
1. handleUnsupportedOperation : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminsController::handleUnsupportedOperation → KILLED |
return Map.of( |
| 109 | "type", e.getClass().getSimpleName(), | |
| 110 | "message", e.getMessage() | |
| 111 | ); | |
| 112 | } | |
| 113 | ||
| 114 | } | |
Mutations | ||
| 65 |
1.1 |
|
| 78 |
1.1 |
|
| 94 |
1.1 |
|
| 99 |
1.1 |
|
| 101 |
1.1 |
|
| 102 |
1.1 |
|
| 108 |
1.1 |