Search in sources :

Example 11 with CustomAccessDeniedException

use of com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceImpl1 method changeClientRating.

@Override
public void changeClientRating(Order order, ChangeRatingDTO dto, User user) {
    if (order.getCourier() == null || !user.getId().equals(order.getCourier().getId()))
        throw new CustomAccessDeniedException();
    order.setClientRating(dto.getRating());
    orderRepo.save(order);
}
Also used : CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)

Example 12 with CustomAccessDeniedException

use of com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method deleteImgNotAdminNotOwner.

@Test
public void deleteImgNotAdminNotOwner() throws IOException {
    // prepare
    Long ownerId = 1L;
    User howardOwner = UserUtils.courierHowardWolowitz(ownerId);
    Long notOwnerId = 2L;
    User leonardNotOwner = UserUtils.moderatorLeonardHofstadter(notOwnerId);
    String imgName = "test.jpeg";
    Path pathTestPng = UPLOAD_PATH.resolve(imgName);
    // file name, where we locate our test.png
    final String fileUuid = "55420882-1e23-4559-b84e-03c3f4d597af";
    Path fileUuidPath = createFileUuidPath(fileUuid);
    Files.copy(pathTestPng, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
    File fileEntity = new File(UUID.fromString(fileUuid), FileType.JPEG, imgName, Files.size(fileUuidPath), Timestamp.valueOf(LocalDateTime.now()), howardOwner);
    doNothing().when(fileRepoMock).delete(fileEntity);
    Exception exception = assertThrows(CustomAccessDeniedException.class, () -> {
        fileService.delete(fileEntity, leonardNotOwner);
    });
    String perfectMessage = new CustomAccessDeniedException().getMessage();
    String resultMessage = exception.getMessage();
    assertEquals(perfectMessage, resultMessage);
    verify(fileRepoMock, never()).delete(fileEntity);
}
Also used : Path(java.nio.file.Path) User(com.ncedu.fooddelivery.api.v1.entities.User) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 13 with CustomAccessDeniedException

use of com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceTest method replaceJpgNotOwnerNotAdmin.

@Test
public void replaceJpgNotOwnerNotAdmin() throws IOException {
    // prepare
    Long userId = 1L;
    User pennyClient = UserUtils.clientPennyTeller(userId);
    Long notOwnerId = 2L;
    User rajeshNotOwner = UserUtils.clientRajeshKoothrappali(notOwnerId);
    String oldImgName = "test.jpeg";
    Path pathTestJpeg = UPLOAD_PATH.resolve(oldImgName);
    final String fileUuid = "e897e931-12ce-4b3f-afbc-42f6c165dfc8";
    Path fileUuidPath = createFileUuidPath(fileUuid);
    Files.copy(pathTestJpeg, fileUuidPath, StandardCopyOption.REPLACE_EXISTING);
    File oldFileEntity = new File(UUID.fromString(fileUuid), FileType.JPEG, oldImgName, Files.size(fileUuidPath), Timestamp.valueOf(LocalDateTime.now()), pennyClient);
    MultipartFile newFile = getImgWithType("testLargeWidth", "png");
    when(fileRepoMock.save(any(File.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    Exception exception = assertThrows(CustomAccessDeniedException.class, () -> {
        fileService.replace(newFile, oldFileEntity, rajeshNotOwner);
    });
    String perfectMessage = new CustomAccessDeniedException().getMessage();
    String resultMessage = exception.getMessage();
    assertEquals(perfectMessage, resultMessage);
    verify(fileRepoMock, never()).save(any(File.class));
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) User(com.ncedu.fooddelivery.api.v1.entities.User) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) File(com.ncedu.fooddelivery.api.v1.entities.File) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) IOException(java.io.IOException) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) BadFileExtensionException(com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 14 with CustomAccessDeniedException

use of com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException in project 2021-msk-food-delivery by netcracker-edu.

the class ProductPositionController method nullifyProductPosition.

@PatchMapping("/api/v1/productPosition/{id}/currentAmount")
@PreAuthorize("hasAnyAuthority('ADMIN', 'MODERATOR')")
public ResponseEntity<?> nullifyProductPosition(@Min(value = 1) @Max(value = Long.MAX_VALUE) @PathVariable Long id, @AuthenticationPrincipal User user) {
    ProductPosition productPositionToNullify = productPositionService.getProductPosition(id);
    if (productPositionToNullify == null)
        throw new NotFoundEx(String.valueOf(id));
    if (Role.isMODERATOR(user.getRole().toString())) {
        if (!user.getModerator().getWarehouseId().equals(productPositionToNullify.getWarehouse().getId())) {
            throw new CustomAccessDeniedException();
        }
    }
    productPositionService.nullifyProductPosition(id);
    return new ResponseEntity<>(HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 15 with CustomAccessDeniedException

use of com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException in project 2021-msk-food-delivery by netcracker-edu.

the class ProductPositionController method updatePaymentStatus.

@PatchMapping("/api/v1/productPositions/paymentState")
@PreAuthorize("hasAnyAuthority('ADMIN', 'MODERATOR')")
public ResponseEntity<?> updatePaymentStatus(@AuthenticationPrincipal User user, @Valid @RequestBody UpdatePaymentStatusDTO updatePaymentStatusDTO) {
    List<ProductPosition> productPositionList = new ArrayList<>();
    for (Long id : updatePaymentStatusDTO.getProductPositions()) {
        ProductPosition productPosition = productPositionService.getProductPosition(id);
        if (productPosition == null)
            throw new NotFoundEx(String.valueOf(id));
        productPositionList.add(productPosition);
    }
    if (Role.isMODERATOR(user.getRole().toString())) {
        Long moderatorBindedWarehouseId = user.getModerator().getWarehouseId();
        for (ProductPosition productPosition : productPositionList) {
            if (!productPosition.getWarehouse().getId().equals(moderatorBindedWarehouseId))
                throw new CustomAccessDeniedException();
        }
    }
    productPositionService.updatePaymentStatus(updatePaymentStatusDTO.getProductPositions());
    return new ResponseEntity<>(HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) ArrayList(java.util.ArrayList) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)16 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)5 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)4 IOException (java.io.IOException)4 Path (java.nio.file.Path)4 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)3 BadFileExtensionException (com.ncedu.fooddelivery.api.v1.errors.badrequest.BadFileExtensionException)3 ResponseEntity (org.springframework.http.ResponseEntity)3 File (com.ncedu.fooddelivery.api.v1.entities.File)2 User (com.ncedu.fooddelivery.api.v1.entities.User)2 FileDeleteException (com.ncedu.fooddelivery.api.v1.errors.badrequest.FileDeleteException)2 Test (org.junit.jupiter.api.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 CoordsDTO (com.ncedu.fooddelivery.api.v1.dto.CoordsDTO)1 com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO (com.ncedu.fooddelivery.api.v1.dto.areCreatedDTO)1 FileLinkDTO (com.ncedu.fooddelivery.api.v1.dto.file.FileLinkDTO)1 com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO (com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO)1