Skip to content

Commit

Permalink
Get user id from token
Browse files Browse the repository at this point in the history
  • Loading branch information
kamnyborg committed Oct 3, 2023
1 parent e0b6fda commit 2287b02
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using kabinizer_api.Dtos.BookingRequest;
using kabinizer_api.Model;
using kabinizer_api.Services;
using kabinizer_data;
using kabinizer_data.Entities;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -12,16 +13,19 @@ namespace kabinizer_api.Controllers;
public class BookingRequestController : ControllerBase
{
private readonly EntityContext _entityContext;
private readonly ITokenService _tokenService;

public BookingRequestController(EntityContext entityContext)
public BookingRequestController(EntityContext entityContext, ITokenService tokenService)
{
_entityContext = entityContext;
_tokenService = tokenService;
}

[HttpGet]
public IEnumerable<BookingRequest> GetBookingRequests()
{
return _entityContext.BookingRequests.Select(BookingRequest.FromEntity);
var currentUserId = _tokenService.GetUserId();
return _entityContext.BookingRequests.Where(b => b.UserId == currentUserId).Select(BookingRequest.FromModel);
}

[HttpGet]
Expand All @@ -31,16 +35,16 @@ public IEnumerable<BookingRequest> GetBookingRequestsByUserId(Guid userId)
return _entityContext.BookingRequests
.Where(e => e.UserId == userId)
.ToList()
.Select(BookingRequest.FromEntity);
.Select(BookingRequest.FromModel);
}


[HttpPost]
public void AddBookingRequests([Required] IEnumerable<CreateBookingRequestDto> r)
{
// TODO: Use authenticated user
var currentUserId = _tokenService.GetUserId();
IEnumerable<BookingRequestEntity> bookingRequestEntities =
r.Select(e => new BookingRequestEntity(e.UserId, e.FromDate, e.ToDate));
r.Select(e => new BookingRequestEntity(currentUserId, e.FromDate, e.ToDate));

_entityContext.BookingRequests.AddRange(bookingRequestEntities);
_entityContext.SaveChanges();
Expand All @@ -49,7 +53,15 @@ public void AddBookingRequests([Required] IEnumerable<CreateBookingRequestDto> r
[HttpDelete]
public bool DeleteBookingRequest([Required] Guid bookingRequestId)
{
var currentUserId = _tokenService.GetUserId();

BookingRequestEntity entityToRemove = _entityContext.BookingRequests.Single(br => br.Id == bookingRequestId);

if (entityToRemove.UserId != currentUserId)
{
throw new Exception("You cannot remove a booking request for another user");
}

_entityContext.BookingRequests.Remove(entityToRemove);
_entityContext.SaveChanges();
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace kabinizer_api.Dtos.BookingRequest;

public record CreateBookingRequestDto(Guid UserId, DateOnly FromDate, DateOnly ToDate);
public record CreateBookingRequestDto(DateOnly FromDate, DateOnly ToDate);
2 changes: 1 addition & 1 deletion kabinizer-back-end/kabinizer-api/Model/BookingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace kabinizer_api.Model;

public record BookingRequest(Guid Id, Guid UserId, DateOnly FromDate, DateOnly ToDate)
{
public static BookingRequest FromEntity(BookingRequestEntity e)
public static BookingRequest FromModel(BookingRequestEntity e)
{
return new BookingRequest(e.Id, e.UserId, DateOnly.FromDateTime(e.FromDate), DateOnly.FromDateTime(e.ToDate));
}
Expand Down
8 changes: 5 additions & 3 deletions kabinizer-back-end/kabinizer-api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using kabinizer_api.Services;
using kabinizer_data;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
Expand All @@ -6,15 +7,16 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("EntraID"));

builder.Services.AddControllers();

builder.Services.AddDbContext<EntityContext>(o =>
o.UseSqlServer(builder.Configuration.GetConnectionString("KabinizerConnection")));

builder.Services.AddScoped<ITokenService, TokenService>();

builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

Expand Down
6 changes: 6 additions & 0 deletions kabinizer-back-end/kabinizer-api/Services/ITokenService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace kabinizer_api.Services;

public interface ITokenService
{
Guid GetUserId();
}
17 changes: 17 additions & 0 deletions kabinizer-back-end/kabinizer-api/Services/TokenService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace kabinizer_api.Services;

public class TokenService : ITokenService
{
private readonly IHttpContextAccessor _httpContextAccessor;

public TokenService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}

public Guid GetUserId()
{
var guid = _httpContextAccessor.HttpContext.User.Claims.First(c => c.Type == "http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
return new Guid(guid);
}
}

0 comments on commit 2287b02

Please sign in to comment.