-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change commands to records, change IPrincipal to ClaimsPrincipal
- Loading branch information
Showing
35 changed files
with
305 additions
and
127 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
src/MediatR.CommandQuery.Audit/MediatR.CommandQuery.Audit.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/MediatR.CommandQuery.Cosmos/MediatR.CommandQuery.Cosmos.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/MediatR.CommandQuery.Hangfire/MediatR.CommandQuery.Hangfire.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/MediatR.CommandQuery.MongoDB/MediatR.CommandQuery.MongoDB.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/MediatR.CommandQuery/Commands/EntityIdentifiersCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
src/MediatR.CommandQuery/Converters/ClaimsPrincipalConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
4 changes: 3 additions & 1 deletion
4
...andQuery/Queries/EntityFilterConverter.cs → ...Query/Converters/EntityFilterConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.