Changes in / [80ddcae:0c049e9]


Ignore:
Location:
sources/app/src/main/java/parkup
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sources/app/src/main/java/parkup/ParkUpApplication.java

    r80ddcae r0c049e9  
    33import org.springframework.boot.SpringApplication;
    44import org.springframework.boot.autoconfigure.SpringBootApplication;
     5import org.springframework.context.annotation.Bean;
     6import org.springframework.security.core.Authentication;
     7import org.springframework.security.core.context.SecurityContextHolder;
    58
    69@SpringBootApplication
     
    1215        SpringApplication.run(ParkUpApplication.class, args);
    1316    }
     17
     18    @Bean
     19    public static Authentication getToken() {
     20        Authentication token = null;
     21        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
     22        if (authentication != null && !authentication.getAuthorities().stream().findFirst().get().getAuthority().equals("ROLE_ANONYMOUS")) {
     23            token = authentication;
     24        }
     25        return token;
     26    }
    1427}
  • sources/app/src/main/java/parkup/services/ParkingZoneService.java

    r80ddcae r0c049e9  
    22
    33import org.springframework.beans.factory.annotation.Autowired;
     4import org.springframework.security.core.Authentication;
    45import org.springframework.stereotype.Service;
     6import parkup.ParkUpApplication;
    57import parkup.data.ParkingZoneAdminView;
    68import parkup.data.ParkingZoneLocation;
     
    1315import parkup.repositories.ParkingZoneRepository;
    1416import parkup.repositories.WorkerRepository;
    15 
     17import static parkup.ParkUpApplication.getToken;
    1618import javax.transaction.Transactional;
    1719import java.util.ArrayList;
     
    3739
    3840    public List<ParkingZone> getAllParkingZones() {
    39         List<ParkingZone> parkingZones= parkingZoneRepository.findAll();
     41        Authentication user = getToken();
     42        String role =user.getAuthorities().stream().findFirst().get().getAuthority();
     43        String email = user.getName();
     44        List<ParkingZone> parkingZones;
     45        if(role.equals("ROLE_REG_USER"))
     46            parkingZones= parkingZoneRepository.findAll();
     47        else if(role.equals("ROLE_WORKER")){
     48            Worker loggedInWorker = workerRepository.findWorkerByEmail(email).orElseThrow(null);
     49            parkingZones= parkingZoneRepository.findAll().stream().filter(pz->loggedInWorker.getParkingZones().contains(pz)).collect(Collectors.toList());
     50        }else if(role.equals("ROLE_ADMIN"))
     51            parkingZones= parkingZoneRepository.findAll();
     52        else
     53            parkingZones=parkingZoneRepository.findAll();
    4054        for(ParkingZone pz:parkingZones){
    4155            setTransientVariables(pz);
Note: See TracChangeset for help on using the changeset viewer.