Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Api/Controllers/BorrowsController.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,7 +51,7 @@ public async Task<ActionResult<Result<BorrowDto>>> BorrowDocument(
/// Get a borrow request by id
/// </summary>
/// <returns>A BorrowDto of the retrieved borrow</returns>
[RequiresRole(IdentityData.Roles.Staff, IdentityData.Roles.Employee)]
[RequiresRole(IdentityData.Roles.Admin ,IdentityData.Roles.Staff, IdentityData.Roles.Employee)]
[HttpGet("{borrowId:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
Expand Down
32 changes: 30 additions & 2 deletions src/Application/Borrows/Queries/GetBorrowRequestById.cs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
using Application.Common.Exceptions;
using Application.Common.Extensions;
using Application.Common.Interfaces;
using Application.Common.Models.Dtos;
using Application.Common.Models.Dtos.Physical;
Expand DownExpand Up@@ -34,20 +35,47 @@ public async Task<BorrowDto> Handle(Query request, CancellationToken cancellatio
.Include(x => x.Borrower)
.Include(x => x.Document)
.ThenInclude(y => y.Department)
.Include(x => x.Document)
.ThenInclude(x => x.Folder)
.ThenInclude(x => x.Locker)
.ThenInclude(x => x.Room)
.FirstOrDefaultAsync(x => x.Id == request.BorrowId, cancellationToken);

if (borrow is null)
{
throw new KeyNotFoundException("Borrow request does not exist.");
}

if (!request.User.Role.Equals(IdentityData.Roles.Employee))
if (request.User.Role.IsAdmin())
{
return _mapper.Map<BorrowDto>(borrow);
}

if (request.User.Role.IsStaff())
{
var staff = _context.Staffs
.Include(x => x.Room)
.FirstOrDefault(x => x.Id == request.User.Id);
if (staff is null)
{
throw new KeyNotFoundException("Staff does not exist.");
}

if (staff.Room is null)
{
throw new ConflictException("Staff does not manage a room.");
}

if (staff.Room.Id != borrow.Document.Folder!.Locker.Room.Id )
{
throw new UnauthorizedAccessException("User can not access this resource.");
}

return _mapper.Map<BorrowDto>(borrow);
}

return borrow.Borrower.Id != request.User.Id
? throw new UnauthorizedAccessException()
? throw new UnauthorizedAccessException("User can not access this resource")
: _mapper.Map<BorrowDto>(borrow);
}
}
Expand Down