Skip to content

Commit

Permalink
change commands to records, change IPrincipal to ClaimsPrincipal
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Oct 11, 2024
1 parent 3b9eb13 commit d07643e
Show file tree
Hide file tree
Showing 35 changed files with 305 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<NoWarn>1591</NoWarn>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<NoWarn>1591</NoWarn>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<NoWarn>1591</NoWarn>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<NoWarn>1591</NoWarn>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<OutputType>Library</OutputType>
<NoWarn>1591</NoWarn>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Security.Principal;
using System.Security.Claims;

using MediatR.CommandQuery.Definitions;
using MediatR.CommandQuery.Queries;
Expand All @@ -18,7 +18,7 @@ protected DeletedFilterBehaviorBase(ILoggerFactory loggerFactory) : base(loggerF
{
}

protected virtual EntityFilter? RewriteFilter(EntityFilter? originalFilter, IPrincipal? principal)
protected virtual EntityFilter? RewriteFilter(EntityFilter? originalFilter, ClaimsPrincipal? principal)
{
if (!_supportsDelete.Value)
return originalFilter;
Expand Down
6 changes: 3 additions & 3 deletions src/MediatR.CommandQuery/Commands/EntityCreateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Principal;
using System.Security.Claims;

namespace MediatR.CommandQuery.Commands;

public class EntityCreateCommand<TCreateModel, TReadModel>
public record EntityCreateCommand<TCreateModel, TReadModel>
: EntityModelCommand<TCreateModel, TReadModel>
{
public EntityCreateCommand(IPrincipal? principal, [NotNull] TCreateModel model) : base(principal, model)
public EntityCreateCommand(ClaimsPrincipal? principal, [NotNull] TCreateModel model) : base(principal, model)
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/MediatR.CommandQuery/Commands/EntityDeleteCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Principal;
using System.Security.Claims;

namespace MediatR.CommandQuery.Commands;

public class EntityDeleteCommand<TKey, TReadModel>
public record EntityDeleteCommand<TKey, TReadModel>
: EntityIdentifierCommand<TKey, TReadModel>
{
public EntityDeleteCommand(IPrincipal? principal, [NotNull] TKey id) : base(principal, id)
public EntityDeleteCommand(ClaimsPrincipal? principal, [NotNull] TKey id) : base(principal, id)
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/MediatR.CommandQuery/Commands/EntityIdentifierCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Principal;
using System.Security.Claims;

namespace MediatR.CommandQuery.Commands;

public abstract class EntityIdentifierCommand<TKey, TResponse>
public abstract record EntityIdentifierCommand<TKey, TResponse>
: PrincipalCommandBase<TResponse>
{
protected EntityIdentifierCommand(IPrincipal? principal, [NotNull] TKey id)
protected EntityIdentifierCommand(ClaimsPrincipal? principal, [NotNull] TKey id)
: base(principal)
{
if (id == null)
Expand Down
6 changes: 3 additions & 3 deletions src/MediatR.CommandQuery/Commands/EntityIdentifiersCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Principal;
using System.Security.Claims;

namespace MediatR.CommandQuery.Commands;

public abstract class EntityIdentifiersCommand<TKey, TResponse>
public abstract record EntityIdentifiersCommand<TKey, TResponse>
: PrincipalCommandBase<TResponse>
{
protected EntityIdentifiersCommand(IPrincipal? principal, [NotNull] IEnumerable<TKey> ids)
protected EntityIdentifiersCommand(ClaimsPrincipal? principal, [NotNull] IEnumerable<TKey> ids)
: base(principal)
{
if (ids is null)
Expand Down
6 changes: 3 additions & 3 deletions src/MediatR.CommandQuery/Commands/EntityModelCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Principal;
using System.Security.Claims;

namespace MediatR.CommandQuery.Commands;

public abstract class EntityModelCommand<TEntityModel, TReadModel>
public abstract record EntityModelCommand<TEntityModel, TReadModel>
: PrincipalCommandBase<TReadModel>
{
protected EntityModelCommand(IPrincipal? principal, [NotNull] TEntityModel model)
protected EntityModelCommand(ClaimsPrincipal? principal, [NotNull] TEntityModel model)
: base(principal)
{
if (model == null)
Expand Down
13 changes: 3 additions & 10 deletions src/MediatR.CommandQuery/Commands/EntityPatchCommand.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Principal;
using System.Security.Claims;

using SystemTextJsonPatch;

namespace MediatR.CommandQuery.Commands;

public class EntityPatchCommand<TKey, TReadModel>
public record EntityPatchCommand<TKey, TReadModel>
: EntityIdentifierCommand<TKey, TReadModel>
{
public EntityPatchCommand(IPrincipal? principal, [NotNull] TKey id, [NotNull] JsonPatchDocument patch) : base(principal, id)
public EntityPatchCommand(ClaimsPrincipal? principal, [NotNull] TKey id, [NotNull] JsonPatchDocument patch) : base(principal, id)
{
Patch = patch ?? throw new ArgumentNullException(nameof(patch));
}

public JsonPatchDocument Patch { get; }


public override string ToString()
{
return $"Entity Patch Command; Model: {typeof(TReadModel).Name}; {base.ToString()}";
}

}
6 changes: 3 additions & 3 deletions src/MediatR.CommandQuery/Commands/EntityUpdateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Principal;
using System.Security.Claims;

namespace MediatR.CommandQuery.Commands;

public class EntityUpdateCommand<TKey, TUpdateModel, TReadModel>
public record EntityUpdateCommand<TKey, TUpdateModel, TReadModel>
: EntityModelCommand<TUpdateModel, TReadModel>
{
public EntityUpdateCommand(IPrincipal? principal, [NotNull] TKey id, TUpdateModel model) : base(principal, model)
public EntityUpdateCommand(ClaimsPrincipal? principal, [NotNull] TKey id, TUpdateModel model) : base(principal, model)
{
if (id == null)
throw new ArgumentNullException(nameof(id));
Expand Down
12 changes: 3 additions & 9 deletions src/MediatR.CommandQuery/Commands/EntityUpsertCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Principal;
using System.Security.Claims;

namespace MediatR.CommandQuery.Commands;

public class EntityUpsertCommand<TKey, TUpdateModel, TReadModel>
public record EntityUpsertCommand<TKey, TUpdateModel, TReadModel>
: EntityModelCommand<TUpdateModel, TReadModel>
{
public EntityUpsertCommand(IPrincipal? principal, [NotNull] TKey id, TUpdateModel model) : base(principal, model)
public EntityUpsertCommand(ClaimsPrincipal? principal, [NotNull] TKey id, TUpdateModel model) : base(principal, model)
{
if (id == null)
throw new ArgumentNullException(nameof(id));
Expand All @@ -16,10 +16,4 @@ public EntityUpsertCommand(IPrincipal? principal, [NotNull] TKey id, TUpdateMode

[NotNull]
public TKey Id { get; }

public override string ToString()
{
return $"Entity Upsert Command; Model: {typeof(TUpdateModel).Name}; Id: {Id}; {base.ToString()}";
}

}
30 changes: 9 additions & 21 deletions src/MediatR.CommandQuery/Commands/PrincipalCommandBase.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
using System.Runtime.Serialization;
using System.Security.Principal;
using System.Security.Claims;
using System.Text.Json.Serialization;

using MediatR.CommandQuery.Converters;

namespace MediatR.CommandQuery.Commands;

public abstract class PrincipalCommandBase<TResponse> : IRequest<TResponse>
public abstract record PrincipalCommandBase<TResponse> : IRequest<TResponse>
{
protected PrincipalCommandBase(IPrincipal? principal)
: this(DateTimeOffset.UtcNow, principal?.Identity?.Name)
protected PrincipalCommandBase(ClaimsPrincipal? principal)
{
Principal = principal;
}

protected PrincipalCommandBase(DateTimeOffset activated, string? activatedBy)
{
Activated = activated;
ActivatedBy = activatedBy;
Activated = DateTimeOffset.UtcNow;
ActivatedBy = principal?.Identity?.Name ?? "system";
}

[JsonIgnore]
[IgnoreDataMember]
public IPrincipal? Principal { get; }
[JsonConverter(typeof(ClaimsPrincipalConverter))]
public ClaimsPrincipal? Principal { get; }

public DateTimeOffset Activated { get; }

public string? ActivatedBy { get; }

public override string ToString()
{
return $"Activated: {Activated}; ActivatedBy: {ActivatedBy}";
}

// ignore Principal property without attribute for JSON.NET
public bool ShouldSerializePrincipal() => false;
}
61 changes: 61 additions & 0 deletions src/MediatR.CommandQuery/Converters/ClaimsPrincipalConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Security.Claims;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace MediatR.CommandQuery.Converters;

public class ClaimsPrincipalConverter : JsonConverter<ClaimsPrincipal>
{
public override ClaimsPrincipal? Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
var claimsPrincipalProxy = JsonSerializer.Deserialize<ClaimsPrincipalProxy>(
ref reader,
options
);

if (claimsPrincipalProxy is null)
{
return null;
}

return new(
new ClaimsIdentity(
claimsPrincipalProxy.Claims.Select(c => new Claim(c.Type, c.Value)),
claimsPrincipalProxy.AuthenticationType,
claimsPrincipalProxy.NameType,
claimsPrincipalProxy.RoleType
)
);
}

public override void Write(
Utf8JsonWriter writer,
ClaimsPrincipal value,
JsonSerializerOptions options
)
{
var identity = value.Identity as ClaimsIdentity;

var claimsPrincipalProxy = new ClaimsPrincipalProxy(
value.Claims.Select(c => new ClaimProxy(c.Type, c.Value)).ToList(),
identity?.AuthenticationType,
identity?.NameClaimType,
identity?.RoleClaimType
);

JsonSerializer.Serialize(writer, claimsPrincipalProxy, options);
}

private sealed record ClaimsPrincipalProxy(
List<ClaimProxy> Claims,
string? AuthenticationType,
string? NameType,
string? RoleType
);

private sealed record ClaimProxy(string Type, string Value);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace MediatR.CommandQuery.Queries;
using MediatR.CommandQuery.Queries;

namespace MediatR.CommandQuery.Converters;

public sealed class EntityFilterConverter : JsonConverter<EntityFilter>
{
Expand Down
Loading

0 comments on commit d07643e

Please sign in to comment.