Ignore:
Timestamp:
05/23/26 19:27:12 (4 months ago)
Author:
MBK <marija.karapandzova@…>
Branches:
master
Children:
ccb5a6b
Parents:
43e476a
Message:

Assign roles for doctors and patients and add role permissions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • backend/src/main/java/medora/controller/PatientController.java

    r43e476a r946877f  
    11package medora.controller;
    22
     3import medora.dto.PatientDTO;
    34import medora.dto.CreatePatientRequest;
    4 import medora.dto.PatientDTO;
    55import medora.models.domain.Patient;
    66import medora.service.PatientService;
     7import medora.util.SecurityUtil;
    78import org.slf4j.Logger;
    89import org.slf4j.LoggerFactory;
     
    1011import org.springframework.http.ResponseEntity;
    1112import org.springframework.web.bind.annotation.*;
     13import jakarta.servlet.http.HttpServletRequest;
    1214
    1315import java.util.List;
     
    2224
    2325    private final PatientService patientService;
    24 
    25     public PatientController(PatientService patientService) {
     26    private final SecurityUtil securityUtil;
     27
     28    public PatientController(PatientService patientService, SecurityUtil securityUtil) {
    2629        this.patientService = patientService;
     30        this.securityUtil = securityUtil;
    2731    }
    2832
    2933    @PostMapping
    30     public ResponseEntity<?> createPatient(@RequestBody CreatePatientRequest request) {
    31         try {
     34    public ResponseEntity<?> createPatient(@RequestBody CreatePatientRequest request, HttpServletRequest httpRequest) {
     35        try {
     36            String role = securityUtil.getRoleFromRequest(httpRequest);
     37            if (role == null) {
     38                return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
     39                        .body(Map.of("error", "Unauthorized"));
     40            }
     41
     42            // Only ADMIN can create patients
     43            if (!role.equals("ADMIN")) {
     44                return ResponseEntity.status(HttpStatus.FORBIDDEN)
     45                        .body(Map.of("error", "Only administrators can create patients"));
     46            }
    3247            if (request.getFirstName() == null || request.getFirstName().isBlank()) {
    3348                return ResponseEntity.badRequest()
     
    144159    @PutMapping("/{patientId}")
    145160    public ResponseEntity<?> updatePatient(@PathVariable Long patientId,
    146                                           @RequestBody CreatePatientRequest request) {
    147         try {
     161                                           @RequestBody CreatePatientRequest request,
     162                                           HttpServletRequest httpRequest) {
     163        try {
     164            String role = securityUtil.getRoleFromRequest(httpRequest);
     165            if (role == null) {
     166                return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
     167                        .body(Map.of("error", "Unauthorized"));
     168            }
     169
     170            // Only ADMIN can update patients
     171            if (!role.equals("ADMIN")) {
     172                return ResponseEntity.status(HttpStatus.FORBIDDEN)
     173                        .body(Map.of("error", "Only administrators can update patients"));
     174            }
     175
    148176            logger.info("Updating patient with ID: {}", patientId);
    149177
Note: See TracChangeset for help on using the changeset viewer.