Skip to content

Commit

Permalink
feat:update event (#719)
Browse files Browse the repository at this point in the history
* fix:优化

* 1

* feat:add Aggregate root object creation, modification, and deletion events

* feat:optimization topic default value

* feat:init event

* feat:test bulkevent

* feat:update BulkMarkEventAsInProgress

* fix:IntegrationEvents.Tests.Infrastructure.CustomIntegrationEventLogService

* fix:resolve conflicts

* feat:remove test api

* fix:DefaultUserContext

* fix:optimization

* fix:取消集成事件默认Topic,可能引起命名冲突优化

* fix:remove backup.csproj

* fix:TestEntityCreatedEventAsync
  • Loading branch information
duiapro authored May 9, 2024
1 parent 31f9fe4 commit 8139abe
Show file tree
Hide file tree
Showing 31 changed files with 823 additions and 68 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Ddd.Domain.Tests;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Ddd.Domain.Events
{
public abstract record class EntityChangedEvent<TEntity> : DomainCommand
{
public TEntity Entity { get; set; }

public EntityChangedEvent(TEntity entity)
{
Entity = entity;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Ddd.Domain.Events
{
public record class EntityCreatedDomainEvent<TEntity> : EntityChangedEvent<TEntity>
{
public EntityCreatedDomainEvent(TEntity entity) : base(entity)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Ddd.Domain.Events
{
public record class EntityDeletedDomainEvent<TEntity> : EntityChangedEvent<TEntity>
{
public EntityDeletedDomainEvent(TEntity entity) : base(entity)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Ddd.Domain.Events
{
public record class EntityModifiedDomainEvent<TEntity> : EntityChangedEvent<TEntity>
{
public EntityModifiedDomainEvent(TEntity entity) : base(entity)
{
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace Masa.BuildingBlocks.Dispatcher.IntegrationEvents;

public abstract record IntegrationEvent : IIntegrationEvent
{
[JsonInclude]public Guid EventId { private get; set; }
[JsonInclude] public Guid EventId { private get; set; }

[JsonInclude]
public DateTime EvenCreateTime { private get; set; }

[NotMapped] [JsonIgnore] public IUnitOfWork? UnitOfWork { get; set; }
[NotMapped][JsonIgnore] public IUnitOfWork? UnitOfWork { get; set; }

public virtual string Topic { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ Task SaveEventAsync(

Task MarkEventAsPublishedAsync(Guid eventId, CancellationToken cancellationToken = default);

Task<List<Guid>> BulkMarkEventAsPublishedAsync(IEnumerable<Guid> eventIds, CancellationToken cancellationToken = default);

Task MarkEventAsInProgressAsync(Guid eventId, int minimumRetryInterval, CancellationToken cancellationToken = default);

Task<List<Guid>> BulkMarkEventAsInProgressAsync(IEnumerable<Guid> eventIds, int minimumRetryInterval, CancellationToken cancellationToken = default);

Task MarkEventAsFailedAsync(Guid eventId, CancellationToken cancellationToken = default);

Task<List<Guid>> BulkMarkEventAsFailedAsync(IEnumerable<Guid> eventIds, CancellationToken cancellationToken = default);

/// <summary>
/// Delete successfully published and expired data
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Microsoft.Extensions.Logging;

namespace Masa.BuildingBlocks.Exceptions.Tests;

[TestClass]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -91,16 +91,16 @@ public MasaException(string errorCode, LogLevel? logLevel, params object[] param
{
}

public MasaException(string message, Exception? innerException, string errorCode, LogLevel? logLevel = null, params object[] parameters)
: base(message, innerException)
public MasaException(Exception? innerException, string errorCode, LogLevel? logLevel = null, params object[] parameters)
: base(null, innerException)
{
_errorCode = errorCode;
_parameters = parameters;
_logLevel = logLevel;
}

public MasaException(Exception? innerException, string errorCode, LogLevel? logLevel = null, params object[] parameters)
: base(null, innerException)
public MasaException(string message, Exception? innerException, string errorCode, LogLevel? logLevel = null, params object[] parameters)
: base(message, innerException)
{
_errorCode = errorCode;
_parameters = parameters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using System.Collections;

namespace Masa.Contrib.Authentication.Identity;

internal class DefaultUserContext : UserContext
Expand Down Expand Up @@ -44,26 +46,55 @@ public DefaultUserContext(
if (claimType == null)
continue;

string? claimValue = null;
if (property.PropertyType != typeof(string) && typeof(System.Collections.IEnumerable).IsAssignableFrom(property.PropertyType))
{
var claimsValues = ClaimsPrincipal?.Claims.Where(claim => claim.Type == claimType)
.Select(claim => claim.Value);
if (claimsValues?.Any() == true)
claimValue = JsonSerializer.Serialize(claimsValues);
}
else
{
claimValue = ClaimsPrincipal?.FindClaimValue(claimType);
}

string? claimValue = ClaimsPrincipal?.FindClaimValue(claimType);
if (claimValue != null)
{
object? claimTypeValue = null;

try
{
claimTypeValue = TypeConvertProvider.ConvertTo(claimValue, property.PropertyType);
}
catch
{
claimTypeValue = this.ParseNonJson(property);
}

modelRelation.Setters[property]
.Invoke(userModel, new[] { TypeConvertProvider.ConvertTo(claimValue, property.PropertyType) });
.Invoke(userModel, new[] { claimTypeValue });
}
}

return userModel;
}

private object? ParseNonJson(PropertyInfo property)
{
var claimValues = new List<string>();
var claimType = _optionsMonitor.CurrentValue.GetClaimType(property.Name);
if (claimType == null)
return null;

if (property.PropertyType != typeof(string) && typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
var claimsValues = ClaimsPrincipal?.Claims.Where(claim => claim.Type == claimType)
.Select(claim => claim.Value).ToList();

claimsValues?.ForEach(item =>
{
try
{
var claimsValue = JsonSerializer.Deserialize<List<string>>(item);
if (claimsValue?.Any() == true)
claimValues.AddRange(claimsValue);
}
catch
{
claimValues.Add(item);
}
});
}

return TypeConvertProvider.ConvertTo(JsonSerializer.Serialize(claimValues), property.PropertyType);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Newtonsoft.Json;

namespace Masa.Contrib.Authentication.Identity.BlazorServer.Tests;

[TestClass]
Expand Down Expand Up @@ -62,6 +64,93 @@ public void TestMasaIdentity2()
Assert.AreEqual(1, userRoles[0]);
}

[TestMethod]
public void TestMasaIdentity3()
{
var services = new ServiceCollection();
var claimsPrincipal = new ClaimsPrincipal(new List<ClaimsIdentity>()
{
new(new List<Claim>()
{
new("sub", "1"),
new(ClaimType.DEFAULT_USER_NAME, "Jim"),
new(ClaimType.DEFAULT_USER_ROLE, "1")//"[\"1\"]"
})
});
Mock<AuthenticationStateProvider> authenticationStateProvider = new();
authenticationStateProvider
.Setup(provider => provider.GetAuthenticationStateAsync())
.ReturnsAsync(new AuthenticationState(claimsPrincipal));

services.AddScoped(_ => authenticationStateProvider.Object);
services.AddMasaIdentity(option =>
{
option.UserId = "sub";
});

Assert.IsTrue(services.Any<ICurrentPrincipalAccessor, BlazorCurrentPrincipalAccessor>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IUserSetter>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IUserContext>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IMultiTenantUserContext>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IMultiEnvironmentUserContext>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IIsolatedUserContext>(ServiceLifetime.Scoped));

var serviceProvider = services.BuildServiceProvider();
var userContext = serviceProvider.GetService<IUserContext>();
Assert.IsNotNull(userContext);
Assert.AreEqual("1", userContext.UserId);
Assert.AreEqual("Jim", userContext.UserName);

var userRoles = userContext.GetUserRoles<int>().ToList();
Assert.AreEqual(1, userRoles.Count);
Assert.AreEqual(1, userRoles[0]);
}

[TestMethod]
public void TestMasaIdentity4()
{
var roles = new List<string>()
{
"admin", "admin2", "admin3","admin4"
};
var services = new ServiceCollection();
var claimsPrincipal = new ClaimsPrincipal(new List<ClaimsIdentity>()
{
new(new List<Claim>()
{
new("sub", "1"),
new(ClaimType.DEFAULT_USER_NAME, "Jim"),
new(ClaimType.DEFAULT_USER_ROLE, JsonConvert.SerializeObject(roles))//"[\"1\"]"
})
});
Mock<AuthenticationStateProvider> authenticationStateProvider = new();
authenticationStateProvider
.Setup(provider => provider.GetAuthenticationStateAsync())
.ReturnsAsync(new AuthenticationState(claimsPrincipal));

services.AddScoped(_ => authenticationStateProvider.Object);
services.AddMasaIdentity(option =>
{
option.UserId = "sub";
});

Assert.IsTrue(services.Any<ICurrentPrincipalAccessor, BlazorCurrentPrincipalAccessor>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IUserSetter>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IUserContext>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IMultiTenantUserContext>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IMultiEnvironmentUserContext>(ServiceLifetime.Scoped));
Assert.IsTrue(services.Any<IIsolatedUserContext>(ServiceLifetime.Scoped));

var serviceProvider = services.BuildServiceProvider();
var userContext = serviceProvider.GetService<IUserContext>();
Assert.IsNotNull(userContext);
Assert.AreEqual("1", userContext.UserId);
Assert.AreEqual("Jim", userContext.UserName);

var userRoles = userContext.GetUserRoles<string>().ToList();
Assert.AreEqual(4, userRoles.Count);
}

[TestMethod]
public void TestIdentityByYaml()
{
Expand Down
Loading

0 comments on commit 8139abe

Please sign in to comment.