Search in sources :

Example 1 with Moderator

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

the class ProfileControllerTest method getModeratorProfile.

@Test
public void getModeratorProfile() {
    ModeratorInfoDTO moderatorInfoDTO = new ModeratorInfoDTO(1L, "MODERATOR", "moderator@mail.ru");
    when(moderatorServiceMock.getModeratorDTOById(1L)).thenReturn(moderatorInfoDTO);
    User user = new User();
    user.setId(1L);
    user.setRole(Role.MODERATOR);
    UserInfoDTO resultModeratorDTO = profileController.getProfile(user);
    assertEquals(moderatorInfoDTO, resultModeratorDTO);
}
Also used : User(com.ncedu.fooddelivery.api.v1.entities.User) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with Moderator

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

the class AuthServiceImpl method signUp.

@Override
public isCreatedDTO signUp(NewUserDTO userInfo) {
    RegistrationMapper regMapper = RegistrationMapper.INSTANCE;
    userInfo.setPassword(encoder.encode(userInfo.getPassword()));
    User user = regMapper.dtoToUser(userInfo);
    user.setRegDate(Timestamp.valueOf(LocalDateTime.now()));
    if (Role.isCLIENT(userInfo.getRole())) {
        Client client = regMapper.dtoToClient(userInfo);
        user.setClient(client);
    }
    if (Role.isMODERATOR(userInfo.getRole())) {
        Moderator moderator = regMapper.dtoToModerator(userInfo);
        user.setModerator(moderator);
    }
    user = userRepo.save(user);
    Long userId = user.getId();
    return new isCreatedDTO(userId);
}
Also used : RegistrationMapper(com.ncedu.fooddelivery.api.v1.mappers.RegistrationMapper) com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO(com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO)

Example 3 with Moderator

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

the class ProductPositionController method shipProductPositionsFromOrder.

@PatchMapping("/api/v1/order/{id}/productPositions/currentAmount")
@PreAuthorize("hasAnyAuthority('ADMIN', 'MODERATOR')")
public ResponseEntity<?> shipProductPositionsFromOrder(@Min(value = 1) @Max(value = Long.MAX_VALUE) @PathVariable(name = "id") Long id, @Valid @RequestBody ProductPositionsShipmentDTO productPositionsShipmentDTO, @AuthenticationPrincipal User user) {
    Order order = orderService.getOrder(id);
    if (order == null)
        throw new NotFoundEx(String.valueOf(id));
    if (user.getRole() == Role.MODERATOR) {
        if (!user.getModerator().getWarehouseId().equals(order.getWarehouse().getId()))
            throw new CustomAccessDeniedException();
    }
    productPositionService.shipProductPositions(id, productPositionsShipmentDTO);
    return new ResponseEntity<>(HttpStatus.OK);
}
Also used : Order(com.ncedu.fooddelivery.api.v1.entities.order.Order) ResponseEntity(org.springframework.http.ResponseEntity) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with Moderator

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

the class ProductPositionController method acceptSupply.

@PostMapping(path = "/api/v1/productPosition")
@PreAuthorize("hasAnyAuthority('ADMIN', 'MODERATOR')")
public isCreatedDTO acceptSupply(@AuthenticationPrincipal User user, @Valid @RequestBody AcceptSupplyDTO acceptSupplyDTO) {
    if (Role.isMODERATOR(user.getRole().toString())) {
        if (!user.getModerator().getWarehouseId().equals(acceptSupplyDTO.getWarehouseId())) {
            throw new CustomAccessDeniedException();
        }
    }
    Long id = productPositionService.acceptSupply(acceptSupplyDTO);
    isCreatedDTO isCreated = new isCreatedDTO();
    isCreated.setId(id);
    return isCreated;
}
Also used : CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO(com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with Moderator

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

the class ProductPositionController method findFiltered.

@GetMapping("/api/v1/productPositions")
@PreAuthorize("hasAnyAuthority('ADMIN', 'MODERATOR')")
public ResponseEntity<List<ProductPositionInfoDTO>> findFiltered(@AuthenticationPrincipal User user, @Valid ProductPositionFilterDTO filterDTO, Pageable pageable) {
    List<ProductPositionInfoDTO> filteredPositions;
    if (user.getRole() == Role.MODERATOR) {
        Long moderatorWarehouseId = user.getModerator().getWarehouseId();
        if (filterDTO.getWarehouseId() != null) {
            if (!filterDTO.getWarehouseId().equals(moderatorWarehouseId))
                throw new CustomAccessDeniedException();
        }
        filterDTO.setWarehouseId(moderatorWarehouseId);
        Specification<ProductPositionNotHierarchical> spec = ProductPositionSpecifications.getFilterSpecification(filterDTO);
        filteredPositions = productPositionService.findFiltered(spec, pageable);
    } else {
        Specification<ProductPositionNotHierarchical> spec = ProductPositionSpecifications.getFilterSpecification(filterDTO);
        filteredPositions = productPositionService.findFiltered(spec, pageable);
    }
    return ResponseEntity.status(HttpStatus.OK).body(filteredPositions);
}
Also used : CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) ProductPositionNotHierarchical(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPositionNotHierarchical) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)6 User (com.ncedu.fooddelivery.api.v1.entities.User)4 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)4 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)3 Test (org.junit.jupiter.api.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 ResponseEntity (org.springframework.http.ResponseEntity)3 com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO (com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO)2 Moderator (com.ncedu.fooddelivery.api.v1.entities.Moderator)2 ModeratorInfoDTO (com.ncedu.fooddelivery.api.v1.dto.user.ModeratorInfoDTO)1 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)1 ProductPositionNotHierarchical (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPositionNotHierarchical)1 RegistrationMapper (com.ncedu.fooddelivery.api.v1.mappers.RegistrationMapper)1 ArrayList (java.util.ArrayList)1