Search in sources :

Example 1 with Person

use of com.adja.evchargerappserver.api.person.Person in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class ElectricCarController method hasRightForUpdate.

@Override
public boolean hasRightForUpdate(Long id, ElectricCar car, HttpHeaders headers) {
    String username = JwtUtil.getUsernameFromJwt(headers.getFirst(HttpHeaders.AUTHORIZATION));
    Person personByName = this.personService.getByUsername(username);
    return Objects.equals(car.getId(), personByName.getCar().getId());
}
Also used : Person(com.adja.evchargerappserver.api.person.Person)

Example 2 with Person

use of com.adja.evchargerappserver.api.person.Person in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class NotificationService method chargingEnded.

@Transactional(isolation = Isolation.REPEATABLE_READ)
public void chargingEnded(Long chargerId) {
    NotificationFilter filter = new NotificationFilter();
    filter.setChargerId(chargerId);
    filter.setPersonId(null);
    Collection<Notification> notificationsOfCharger = this.search(filter);
    notificationsOfCharger.forEach(notification -> {
        Person person = this.personService.getById(notification.getPersonToNotifyId());
        Charger charger = this.chargerService.getById(notification.getObservedChargerId());
        this.emailSendingService.sendChargingEndedNotificationMail(person, charger);
        this.deleteById(notification.getId());
    });
}
Also used : Charger(com.adja.evchargerappserver.api.charger.Charger) Person(com.adja.evchargerappserver.api.person.Person) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Person

use of com.adja.evchargerappserver.api.person.Person in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class NotificationService method validateEntity.

@Override
protected boolean validateEntity(Notification entity) {
    if (entity.getPersonToNotifyId() == null || entity.getObservedChargerId() == null)
        return false;
    try {
        Person person = this.personService.getById(entity.getPersonToNotifyId());
        Charger charger = this.chargerService.getById(entity.getObservedChargerId());
        NotificationFilter alreadyExistingNotificationFilter = new NotificationFilter(entity.getPersonToNotifyId(), entity.getObservedChargerId());
        if (!this.search(alreadyExistingNotificationFilter).isEmpty())
            return false;
        return person.getEmail() != null && charger.getCurrentlyChargingCar() != null;
    } catch (EntityNotFoundException e) {
        return false;
    }
}
Also used : Charger(com.adja.evchargerappserver.api.charger.Charger) EntityNotFoundException(javax.persistence.EntityNotFoundException) Person(com.adja.evchargerappserver.api.person.Person)

Example 4 with Person

use of com.adja.evchargerappserver.api.person.Person in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class TokenController method refreshToken.

@PostMapping("/token/refresh")
public void refreshToken(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
    if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
        try {
            Person person = this.personService.getByUsername(JwtUtil.getUsernameFromJwt(authorizationHeader));
            String accessToken = JwtUtil.createAccessToken(request.getRequestURL().toString(), person.getRoles().stream().map(Role::getName).collect(Collectors.toList()), request.getRequestURL().toString());
            String refreshToken = authorizationHeader.substring("Bearer ".length());
            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
            Map<String, String> tokens = new HashMap<>();
            tokens.put("accessToken", accessToken);
            tokens.put("refreshToken", refreshToken);
            new ObjectMapper().writeValue(response.getOutputStream(), tokens);
        } catch (Exception e) {
            response.setHeader("error", e.getMessage());
            response.setStatus(HttpStatus.FORBIDDEN.value());
            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
            Map<String, String> error = new HashMap<>();
            error.put("error_message", e.getMessage());
            new ObjectMapper().writeValue(response.getOutputStream(), error);
        }
    } else {
        throw new RuntimeException("Refresh token is missing");
    }
}
Also used : Role(com.adja.evchargerappserver.api.role.Role) HashMap(java.util.HashMap) Person(com.adja.evchargerappserver.api.person.Person) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException)

Aggregations

Person (com.adja.evchargerappserver.api.person.Person)4 Charger (com.adja.evchargerappserver.api.charger.Charger)2 Role (com.adja.evchargerappserver.api.role.Role)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 Transactional (org.springframework.transaction.annotation.Transactional)1