Skip to content

Commit f74864c

Browse files
committed
Added UserContext
1 parent b23d8d4 commit f74864c

7 files changed

Lines changed: 65 additions & 3 deletions

File tree

samples/EasyWay.Samples/Commands/SampleCommandHandler.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,30 @@ internal sealed class SampleCommandHandler : ICommandHandler<SampleModule, Sampl
1717

1818
private readonly IEnumerable<ISamplePolicy> _policies;
1919

20+
private readonly IUserContext _userContext;
21+
2022
public SampleCommandHandler(
2123
ICancellationContext cancellationContext,
2224
ISampleAggragateRootRepository repository,
2325
SampleAggregateRootFactory factory,
2426
SampleDomainService domainService,
2527
IConcurrencyConflictValidator concurrencyTokenValidator,
26-
IEnumerable<ISamplePolicy> policies)
28+
IEnumerable<ISamplePolicy> policies,
29+
IUserContext userContext)
2730
{
2831
_cancellationContext = cancellationContext;
2932
_repository = repository;
3033
_factory = factory;
3134
_domainService = domainService;
3235
_concurrencyTokenValidator = concurrencyTokenValidator;
3336
_policies = policies;
37+
_userContext = userContext;
3438
}
3539

3640
public async Task Handle(SampleCommand command)
3741
{
42+
var userId = _userContext.UserId;
43+
3844
var token = _cancellationContext.Token;
3945

4046
var data = _policies
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.AspNetCore.Http;
2+
3+
namespace EasyWay.Internals.Contexts
4+
{
5+
internal sealed class UserContext : IUserContext
6+
{
7+
public UserContext(IHttpContextAccessor httpContextAccessor)
8+
{
9+
var userId = httpContextAccessor
10+
.HttpContext?
11+
.User?
12+
.Claims?
13+
.SingleOrDefault(x => x.Type == "sub")?
14+
.Value;
15+
16+
UserId = userId is null ? null : new Guid(userId);
17+
}
18+
19+
public Guid? UserId { get; }
20+
21+
public bool IsAuthenticated => UserId is not null;
22+
}
23+
}

source/EasyWay.WebApi/Internals/Extensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
using EasyWay.Internals.Exceptions;
1+
using EasyWay.Internals.Contexts;
2+
using EasyWay.Internals.Exceptions;
23
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.Extensions.DependencyInjection;
35

46
namespace EasyWay.Internals
57
{
68
internal static class Extensions
79
{
10+
internal static void AddEasyWayWebApi(
11+
this IServiceCollection services)
12+
{
13+
services.AddHttpContextAccessor();
14+
15+
services.AddScoped<IUserContext, UserContext>();
16+
}
17+
818
internal static void UseEasyWay(this IApplicationBuilder app)
919
{
1020
app.UseHttpsRedirection();

source/EasyWay.WebApi/WebKernelBuilder.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Builder;
1+
using EasyWay.Internals;
2+
using Microsoft.AspNetCore.Builder;
23

34
namespace EasyWay
45
{
@@ -25,6 +26,8 @@ public void AddModule<TModule, TModuleConfigurator>()
2526

2627
public WebKernel Build()
2728
{
29+
AppBuilder.Services.AddEasyWayWebApi();
30+
2831
return new WebKernel(AppBuilder.Build());
2932
}
3033
}

source/EasyWay/IUserContext.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace EasyWay
2+
{
3+
public interface IUserContext
4+
{
5+
Guid? UserId { get; }
6+
7+
bool IsAuthenticated { get; }
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace EasyWay.Internals.Contexts
2+
{
3+
internal class DefaultUserContext : IUserContext
4+
{
5+
public Guid? UserId => null;
6+
7+
public bool IsAuthenticated => false;
8+
}
9+
}

source/EasyWay/Internals/Contexts/Extensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ internal static IServiceCollection AddContexts(this IServiceCollection services)
99
services.AddScoped<CancellationContext>();
1010
services.AddScoped<ICancellationContext, CancellationContext>();
1111

12+
services.AddScoped<IUserContext, DefaultUserContext>();
13+
1214
return services;
1315
}
1416
}

0 commit comments

Comments
 (0)