Search in sources :

Example 71 with User

use of com.ncedu.fooddelivery.api.v1.entities.User in project youtubechannel by lspil.

the class JPAUserDetailsService method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) {
    Optional<User> user = userRepository.findUserByUsername(username);
    User u = user.orElseThrow(() -> new UsernameNotFoundException("Error!"));
    return new SecurityUser(u);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(com.laurentiuspilca.springsecurityc2.entities.User)

Example 72 with User

use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.

the class DeliverySessionServiceImpl method finishSession.

@Override
public void finishSession(User user, User targetUser) {
    if (targetUser.getRole() != Role.COURIER)
        throw new IncorrectUserRoleRequestException();
    Courier courier = targetUser.getCourier();
    if (user.getRole() == Role.MODERATOR)
        checkModeratorAccess(user.getModerator(), courier);
    DeliverySession currentSession = deliverySessionRepo.getActiveSession(courier.getId());
    if (currentSession == null)
        throw new NoActiveDeliverySessionException();
    Order activeOrder = orderService.findCouriersActiveOrder(courier);
    if (activeOrder != null)
        throw new DeliverySessionFinishException(activeOrder.getId());
    currentSession.setEndTime(LocalDateTime.now());
    // db trigger does other work
    deliverySessionRepo.save(currentSession);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order)

Example 73 with User

use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.

the class DeliverySessionServiceImpl method finishSession.

@Override
public void finishSession(User user, DeliverySession deliverySession) {
    if (user.getRole() == Role.MODERATOR)
        checkModeratorAccess(user.getModerator(), deliverySession.getCourier());
    if (deliverySession.getEndTime() != null)
        throw new DeliverySessionAlreadyFinishedException(deliverySession.getId());
    Order activeOrder = orderService.findCouriersActiveOrder(deliverySession.getCourier());
    if (activeOrder != null)
        throw new DeliverySessionFinishException(activeOrder.getId());
    deliverySession.setEndTime(LocalDateTime.now());
    // db trigger does other work
    deliverySessionRepo.save(deliverySession);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order)

Example 74 with User

use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceImpl method save.

@Override
public FileLinkDTO save(MultipartFile file, User owner) {
    try {
        String originalFileName = file.getOriginalFilename();
        String fileName = getFileNameWithoutExt(originalFileName);
        FileType fileType = getFileType(originalFileName);
        Long fileSize = file.getSize();
        UUID fileUuid = UUID.randomUUID();
        Path fullPathToFile = createFullPathToFile(fileUuid);
        if (owner.getRole() == Role.CLIENT || owner.getRole() == Role.COURIER) {
            InputStream is = file.getInputStream();
            BufferedImage bufferedImage = ImageIO.read(is);
            boolean isExceedImageSize = checkExceedImageSize(bufferedImage);
            if (isExceedImageSize) {
                bufferedImage = resizeImage(bufferedImage);
            }
            if (fileType == FileType.PNG) {
                bufferedImage = convertPNGtoJPG(bufferedImage);
            }
            ImageIO.write(bufferedImage, "jpg", fullPathToFile.toFile());
            fileSize = Files.size(fullPathToFile);
            fileType = FileType.JPEG;
        } else {
            Files.copy(file.getInputStream(), fullPathToFile, StandardCopyOption.REPLACE_EXISTING);
        }
        File fileEntity = new File(fileUuid, fileType, fileName, fileSize, Timestamp.valueOf(LocalDateTime.now()), owner);
        fileRepo.save(fileEntity);
        String fileLink = createFileLink(fileUuid);
        return new FileLinkDTO(fileLink, fileUuid.toString());
    } catch (BadFileExtensionException e) {
        throw e;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new FileStorageException();
    }
}
Also used : Path(java.nio.file.Path) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) InputStream(java.io.InputStream) FileStorageException(com.ncedu.fooddelivery.api.v1.errors.badrequest.FileStorageException) FileLinkDTO(com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO) BufferedImage(java.awt.image.BufferedImage) FileStorageException(com.ncedu.fooddelivery.api.v1.errors.badrequest.FileStorageException) FileDeleteException(com.ncedu.fooddelivery.api.v1.errors.badrequest.FileDeleteException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) FileType(com.ncedu.fooddelivery.api.v1.entities.FileType) File(com.ncedu.fooddelivery.api.v1.entities.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 75 with User

use of com.ncedu.fooddelivery.api.v1.entities.User in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceImpl1 method changeOrderStatus.

@Override
public void changeOrderStatus(Order order, User user, ChangeOrderStatusDTO dto) {
    if (user.getRole() == Role.MODERATOR) {
        if (!order.getWarehouse().getId().equals(user.getModerator().getWarehouseId()))
            throw new CustomAccessDeniedException();
    } else if (user.getRole() == Role.COURIER) {
        if (!order.getCourier().getId().equals(user.getCourier().getId()))
            throw new CustomAccessDeniedException();
    } else if (user.getRole() == Role.CLIENT) {
        if (!order.getClient().getId().equals(user.getClient().getId()))
            throw new CustomAccessDeniedException();
    }
    OrderStatus oldStatus = order.getStatus();
    OrderStatus newStatus = dto.getNewStatus();
    if (oldStatus == OrderStatus.CANCELLED || oldStatus == OrderStatus.DELIVERED || (oldStatus.ordinal() > newStatus.ordinal()))
        throw new OrderStatusChangeException(order.getId());
    order.setStatus(newStatus);
    orderRepo.save(order);
}
Also used : CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)

Aggregations

User (com.ncedu.fooddelivery.api.v1.entities.User)58 Test (org.junit.jupiter.api.Test)55 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)55 File (com.ncedu.fooddelivery.api.v1.entities.File)21 Path (java.nio.file.Path)19 MultipartFile (org.springframework.web.multipart.MultipartFile)19 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)18 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)18 FileLinkDTO (com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)15 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)14 BufferedImage (java.awt.image.BufferedImage)10 UserInfoDTO (com.ncedu.fooddelivery.api.v1.dto.user.UserInfoDTO)9 Role (com.ncedu.fooddelivery.api.v1.entities.Role)7 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)7 IOException (java.io.IOException)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)6 AlreadyExistsException (com.ncedu.fooddelivery.api.v1.errors.badrequest.AlreadyExistsException)6 PasswordsMismatchException (com.ncedu.fooddelivery.api.v1.errors.badrequest.PasswordsMismatchException)6 CommitAfter (org.apache.tapestry5.jpa.annotations.CommitAfter)6