-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into Feature-Validate-Adoption
- Loading branch information
Showing
19 changed files
with
721 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/cat/udl/eps/softarch/demo/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cat.udl.eps.softarch.demo.controller; | ||
|
||
import cat.udl.eps.softarch.demo.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
|
||
@Controller | ||
public class UserController { | ||
|
||
@Autowired | ||
UserRepository userRepository; | ||
|
||
@PreAuthorize("hasRole('ROLE_ADMIN')") | ||
@PostMapping("/users/{username}/lock") | ||
public ResponseEntity<Object> lockUser(@PathVariable String username) { | ||
// If the user is present, lock it and save it. If not, return 404. | ||
return this.userRepository.findById(username) | ||
.map(user -> { | ||
user.lock(); | ||
this.userRepository.save(user); | ||
return ResponseEntity.ok().build(); | ||
}) | ||
.orElse(ResponseEntity.notFound().build()); | ||
} | ||
|
||
@PreAuthorize("hasRole('ROLE_ADMIN')") | ||
@PostMapping("/users/{username}/unlock") | ||
public ResponseEntity<Object> unlockUser(@PathVariable String username) { | ||
// If the user is present, unlock it and save it. If not, return 404. | ||
return this.userRepository.findById(username) | ||
.map(user -> { | ||
user.unlock(); | ||
this.userRepository.save(user); | ||
return ResponseEntity.ok().build(); | ||
}) | ||
.orElse(ResponseEntity.notFound().build()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/cat/udl/eps/softarch/demo/domain/FavouritedPets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cat.udl.eps.softarch.demo.domain; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
||
@EqualsAndHashCode(callSuper = true) | ||
@Entity | ||
@Data | ||
public class FavouritedPets extends UriEntity<Long> { | ||
@Id | ||
@NotNull | ||
Long id; | ||
|
||
Long petId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/cat/udl/eps/softarch/demo/handler/MedicalRecordEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package cat.udl.eps.softarch.demo.handler; | ||
|
||
import cat.udl.eps.softarch.demo.domain.MedicalRecord; | ||
import cat.udl.eps.softarch.demo.exceptions.UnauthorizedAccessException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.data.rest.core.annotation.HandleAfterSave; | ||
import org.springframework.data.rest.core.annotation.HandleBeforeCreate; | ||
import org.springframework.data.rest.core.annotation.HandleBeforeSave; | ||
import org.springframework.data.rest.core.annotation.RepositoryEventHandler; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Component | ||
@RepositoryEventHandler() // Ensure this handler is for MedicalRecord entity | ||
public class MedicalRecordEventHandler { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(MedicalRecordEventHandler.class); | ||
|
||
private static final String ROLE_SHELTER_VOLUNTEER = "ROLE_SHELTER_VOLUNTEER"; | ||
private static final String ROLE_ADMIN = "ROLE_ADMIN"; | ||
|
||
/** | ||
* Handles actions before creating a medical record. | ||
* @param medicalRecord the medical record to be created | ||
* @throws UnauthorizedAccessException if the user is not authorized to create a medical record | ||
*/ | ||
@HandleBeforeCreate | ||
public void handleMedicalRecordBeforeCreate(MedicalRecord medicalRecord) throws UnauthorizedAccessException { | ||
checkAuthorization(); | ||
logger.info("Authorized creation of a new medical record by user: {}", getCurrentUsername()); | ||
} | ||
|
||
/** | ||
* Handles actions before saving (updating) a medical record. | ||
*/ | ||
@HandleBeforeSave | ||
public void handleMedicalRecordBeforeSave(MedicalRecord medicalRecord) throws UnauthorizedAccessException { | ||
checkAuthorization(); | ||
logger.info("Authorized save of medical record by user: {}", getCurrentUsername()); | ||
} | ||
|
||
/** | ||
* Handles actions after saving a medical record. | ||
* @param medicalRecord the saved medical record | ||
*/ | ||
@HandleAfterSave | ||
public void handleMedicalRecordPostSave(MedicalRecord medicalRecord) { | ||
logger.info("Medical record for pet {} saved successfully", medicalRecord.getPet().getName()); | ||
} | ||
|
||
private void checkAuthorization() throws UnauthorizedAccessException { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
if (!isAuthorized(authentication)) { | ||
throw new UnauthorizedAccessException(); | ||
} | ||
} | ||
|
||
private boolean isAuthorized(Authentication authentication) { | ||
if (authentication == null || !authentication.isAuthenticated()) { | ||
return false; | ||
} | ||
List<String> requiredAuthorities = Arrays.asList(ROLE_SHELTER_VOLUNTEER, ROLE_ADMIN); | ||
return authentication.getAuthorities().stream() | ||
.anyMatch(grantedAuthority -> requiredAuthorities.contains(grantedAuthority.getAuthority())); | ||
} | ||
|
||
private String getCurrentUsername() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
return authentication != null ? authentication.getName() : "anonymous"; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/cat/udl/eps/softarch/demo/repository/MedicalRecordRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package cat.udl.eps.softarch.demo.repository; | ||
|
||
import cat.udl.eps.softarch.demo.domain.MedicalRecord; | ||
import cat.udl.eps.softarch.demo.domain.Pet; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.PagingAndSortingRepository; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.data.rest.core.annotation.RepositoryRestResource; | ||
|
||
import java.util.List; | ||
|
||
@RepositoryRestResource | ||
public interface MedicalRecordRepository extends CrudRepository < MedicalRecord, Long > , PagingAndSortingRepository < MedicalRecord, Long > { | ||
|
||
List < MedicalRecord > findByPet(@Param("pet") Pet pet); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/test/java/cat/udl/eps/softarch/demo/steps/AddMedicalRecordStepDefs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cat.udl.eps.softarch.demo.steps; | ||
|
||
import cat.udl.eps.softarch.demo.domain.MedicalRecord; | ||
import cat.udl.eps.softarch.demo.domain.Pet; | ||
import cat.udl.eps.softarch.demo.repository.PetRepository; | ||
import io.cucumber.java.en.Given; | ||
import io.cucumber.java.en.When; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.ZonedDateTime; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
|
||
|
||
public class AddMedicalRecordStepDefs { | ||
|
||
@Autowired | ||
private StepDefs stepDefs; | ||
|
||
@Autowired | ||
PetRepository petRepository; | ||
|
||
|
||
@Given("a pet exists in the system") | ||
public void aPetExistsInTheSystem() { | ||
var pet = new Pet(); | ||
|
||
petRepository.save(pet); // Save the pet to the database | ||
} | ||
|
||
@When("I add a new medical record for a pet with issue {string}, description {string}, and date {string}") | ||
public void iAddANewMedicalRecordForAPetWithIssueDescriptionAndDate(String issue, String description, String date) throws Throwable { | ||
MedicalRecord newRecord = new MedicalRecord(); | ||
newRecord.setDescription(description); | ||
newRecord.setIssue(issue); | ||
newRecord.setDate(ZonedDateTime.parse(date)); | ||
newRecord.setPet(petRepository.findAll().iterator().next()); | ||
|
||
// Mock a POST request to /medicalRecords | ||
stepDefs.result = stepDefs.mockMvc.perform( | ||
post("/medicalRecords") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(newRecord)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
|
||
} | ||
|
||
|
||
|
||
@When("I add a new medical record for a pet with issue {string}, description {string} and no date") | ||
public void iAddANewMedicalRecordForAPetWithIssueDescriptionAndNoDate(String issue, String description) throws Throwable { | ||
MedicalRecord recordWithoutDate = new MedicalRecord(); | ||
recordWithoutDate.setDescription(description); | ||
recordWithoutDate.setIssue(issue); | ||
//No data added | ||
recordWithoutDate.setPet(petRepository.findAll().iterator().next()); | ||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
post("/medicalRecords") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(recordWithoutDate)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.with(AuthenticationStepDefs.authenticate())) | ||
.andDo(print()); | ||
} | ||
|
||
@When("I try to add a medical record for a pet") | ||
public void iTryToAddAMedicalRecordForAPet() throws Exception { | ||
MedicalRecord medicalRecord = new MedicalRecord(); | ||
medicalRecord.setDescription("Description"); | ||
medicalRecord.setIssue("Issue"); | ||
medicalRecord.setDate(ZonedDateTime.now()); | ||
medicalRecord.setPet(petRepository.findAll().iterator().next()); | ||
|
||
stepDefs.result = stepDefs.mockMvc.perform( | ||
post("/medicalRecords") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(stepDefs.mapper.writeValueAsString(medicalRecord)) | ||
.characterEncoding(StandardCharsets.UTF_8) | ||
.with(AuthenticationStepDefs.authenticate()) | ||
) | ||
.andDo(print()); | ||
} | ||
} |
Oops, something went wrong.