Skip to content

Commit 8139abe

Browse files
authored
feat:update event (#719)
* 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
1 parent 31f9fe4 commit 8139abe

File tree

31 files changed

+823
-68
lines changed

31 files changed

+823
-68
lines changed

src/BuildingBlocks/Authentication/OpenIdConnect/Masa.BuildingBlocks.Authentication.OpenIdConnect.Domain/Masa - Backup.BuildingBlocks.Authentication.OpenIdConnect.Domain.csproj

-14
This file was deleted.

src/BuildingBlocks/Ddd/Domain/Masa.BuildingBlocks.Ddd.Domain.Tests/EventTest.cs

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

44
namespace Masa.BuildingBlocks.Ddd.Domain.Tests;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Ddd.Domain.Events
5+
{
6+
public abstract record class EntityChangedEvent<TEntity> : DomainCommand
7+
{
8+
public TEntity Entity { get; set; }
9+
10+
public EntityChangedEvent(TEntity entity)
11+
{
12+
Entity = entity;
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Ddd.Domain.Events
5+
{
6+
public record class EntityCreatedDomainEvent<TEntity> : EntityChangedEvent<TEntity>
7+
{
8+
public EntityCreatedDomainEvent(TEntity entity) : base(entity)
9+
{
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Ddd.Domain.Events
5+
{
6+
public record class EntityDeletedDomainEvent<TEntity> : EntityChangedEvent<TEntity>
7+
{
8+
public EntityDeletedDomainEvent(TEntity entity) : base(entity)
9+
{
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.BuildingBlocks.Ddd.Domain.Events
5+
{
6+
public record class EntityModifiedDomainEvent<TEntity> : EntityChangedEvent<TEntity>
7+
{
8+
public EntityModifiedDomainEvent(TEntity entity) : base(entity)
9+
{
10+
}
11+
}
12+
}

src/BuildingBlocks/Development/Masa.BuildingBlocks.Development.DaprStarter/Masa - Backup.BuildingBlocks.Development.DaprStarters.csproj

-13
This file was deleted.

src/BuildingBlocks/Dispatcher/Masa.BuildingBlocks.Dispatcher.IntegrationEvents/IntegrationEvent.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ namespace Masa.BuildingBlocks.Dispatcher.IntegrationEvents;
55

66
public abstract record IntegrationEvent : IIntegrationEvent
77
{
8-
[JsonInclude]public Guid EventId { private get; set; }
8+
[JsonInclude] public Guid EventId { private get; set; }
99

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

13-
[NotMapped] [JsonIgnore] public IUnitOfWork? UnitOfWork { get; set; }
13+
[NotMapped][JsonIgnore] public IUnitOfWork? UnitOfWork { get; set; }
1414

1515
public virtual string Topic { get; set; }
1616

src/BuildingBlocks/Dispatcher/Masa.BuildingBlocks.Dispatcher.IntegrationEvents/Logs/IIntegrationEventLogService.cs

+6
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ Task SaveEventAsync(
3737

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

40+
Task<List<Guid>> BulkMarkEventAsPublishedAsync(IEnumerable<Guid> eventIds, CancellationToken cancellationToken = default);
41+
4042
Task MarkEventAsInProgressAsync(Guid eventId, int minimumRetryInterval, CancellationToken cancellationToken = default);
4143

44+
Task<List<Guid>> BulkMarkEventAsInProgressAsync(IEnumerable<Guid> eventIds, int minimumRetryInterval, CancellationToken cancellationToken = default);
45+
4246
Task MarkEventAsFailedAsync(Guid eventId, CancellationToken cancellationToken = default);
4347

48+
Task<List<Guid>> BulkMarkEventAsFailedAsync(IEnumerable<Guid> eventIds, CancellationToken cancellationToken = default);
49+
4450
/// <summary>
4551
/// Delete successfully published and expired data
4652
/// </summary>

src/BuildingBlocks/Exception/Masa.BuildingBlocks.Exceptions.Tests/UserFriendlyExceptionTest.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Copyright (c) MASA Stack All rights reserved.
1+
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4+
using Microsoft.Extensions.Logging;
5+
46
namespace Masa.BuildingBlocks.Exceptions.Tests;
57

68
[TestClass]

src/BuildingBlocks/Exception/Masa.BuildingBlocks.Exceptions/MasaException.cs

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

44
// ReSharper disable once CheckNamespace
@@ -91,16 +91,16 @@ public MasaException(string errorCode, LogLevel? logLevel, params object[] param
9191
{
9292
}
9393

94-
public MasaException(string message, Exception? innerException, string errorCode, LogLevel? logLevel = null, params object[] parameters)
95-
: base(message, innerException)
94+
public MasaException(Exception? innerException, string errorCode, LogLevel? logLevel = null, params object[] parameters)
95+
: base(null, innerException)
9696
{
9797
_errorCode = errorCode;
9898
_parameters = parameters;
9999
_logLevel = logLevel;
100100
}
101101

102-
public MasaException(Exception? innerException, string errorCode, LogLevel? logLevel = null, params object[] parameters)
103-
: base(null, innerException)
102+
public MasaException(string message, Exception? innerException, string errorCode, LogLevel? logLevel = null, params object[] parameters)
103+
: base(message, innerException)
104104
{
105105
_errorCode = errorCode;
106106
_parameters = parameters;

src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/DefaultUserContext.cs

+45-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4+
using System.Collections;
5+
46
namespace Masa.Contrib.Authentication.Identity;
57

68
internal class DefaultUserContext : UserContext
@@ -44,26 +46,55 @@ public DefaultUserContext(
4446
if (claimType == null)
4547
continue;
4648

47-
string? claimValue = null;
48-
if (property.PropertyType != typeof(string) && typeof(System.Collections.IEnumerable).IsAssignableFrom(property.PropertyType))
49-
{
50-
var claimsValues = ClaimsPrincipal?.Claims.Where(claim => claim.Type == claimType)
51-
.Select(claim => claim.Value);
52-
if (claimsValues?.Any() == true)
53-
claimValue = JsonSerializer.Serialize(claimsValues);
54-
}
55-
else
56-
{
57-
claimValue = ClaimsPrincipal?.FindClaimValue(claimType);
58-
}
59-
49+
string? claimValue = ClaimsPrincipal?.FindClaimValue(claimType);
6050
if (claimValue != null)
6151
{
52+
object? claimTypeValue = null;
53+
54+
try
55+
{
56+
claimTypeValue = TypeConvertProvider.ConvertTo(claimValue, property.PropertyType);
57+
}
58+
catch
59+
{
60+
claimTypeValue = this.ParseNonJson(property);
61+
}
62+
6263
modelRelation.Setters[property]
63-
.Invoke(userModel, new[] { TypeConvertProvider.ConvertTo(claimValue, property.PropertyType) });
64+
.Invoke(userModel, new[] { claimTypeValue });
6465
}
6566
}
6667

6768
return userModel;
6869
}
70+
71+
private object? ParseNonJson(PropertyInfo property)
72+
{
73+
var claimValues = new List<string>();
74+
var claimType = _optionsMonitor.CurrentValue.GetClaimType(property.Name);
75+
if (claimType == null)
76+
return null;
77+
78+
if (property.PropertyType != typeof(string) && typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
79+
{
80+
var claimsValues = ClaimsPrincipal?.Claims.Where(claim => claim.Type == claimType)
81+
.Select(claim => claim.Value).ToList();
82+
83+
claimsValues?.ForEach(item =>
84+
{
85+
try
86+
{
87+
var claimsValue = JsonSerializer.Deserialize<List<string>>(item);
88+
if (claimsValue?.Any() == true)
89+
claimValues.AddRange(claimsValue);
90+
}
91+
catch
92+
{
93+
claimValues.Add(item);
94+
}
95+
});
96+
}
97+
98+
return TypeConvertProvider.ConvertTo(JsonSerializer.Serialize(claimValues), property.PropertyType);
99+
}
69100
}

src/Contrib/Authentication/Identity/Tests/Masa.Contrib.Authentication.Identity.BlazorServer.Tests/IdentityTest.cs

+90-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Copyright (c) MASA Stack All rights reserved.
1+
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4+
using Newtonsoft.Json;
5+
46
namespace Masa.Contrib.Authentication.Identity.BlazorServer.Tests;
57

68
[TestClass]
@@ -62,6 +64,93 @@ public void TestMasaIdentity2()
6264
Assert.AreEqual(1, userRoles[0]);
6365
}
6466

67+
[TestMethod]
68+
public void TestMasaIdentity3()
69+
{
70+
var services = new ServiceCollection();
71+
var claimsPrincipal = new ClaimsPrincipal(new List<ClaimsIdentity>()
72+
{
73+
new(new List<Claim>()
74+
{
75+
new("sub", "1"),
76+
new(ClaimType.DEFAULT_USER_NAME, "Jim"),
77+
new(ClaimType.DEFAULT_USER_ROLE, "1")//"[\"1\"]"
78+
})
79+
});
80+
Mock<AuthenticationStateProvider> authenticationStateProvider = new();
81+
authenticationStateProvider
82+
.Setup(provider => provider.GetAuthenticationStateAsync())
83+
.ReturnsAsync(new AuthenticationState(claimsPrincipal));
84+
85+
services.AddScoped(_ => authenticationStateProvider.Object);
86+
services.AddMasaIdentity(option =>
87+
{
88+
option.UserId = "sub";
89+
});
90+
91+
Assert.IsTrue(services.Any<ICurrentPrincipalAccessor, BlazorCurrentPrincipalAccessor>(ServiceLifetime.Scoped));
92+
Assert.IsTrue(services.Any<IUserSetter>(ServiceLifetime.Scoped));
93+
Assert.IsTrue(services.Any<IUserContext>(ServiceLifetime.Scoped));
94+
Assert.IsTrue(services.Any<IMultiTenantUserContext>(ServiceLifetime.Scoped));
95+
Assert.IsTrue(services.Any<IMultiEnvironmentUserContext>(ServiceLifetime.Scoped));
96+
Assert.IsTrue(services.Any<IIsolatedUserContext>(ServiceLifetime.Scoped));
97+
98+
var serviceProvider = services.BuildServiceProvider();
99+
var userContext = serviceProvider.GetService<IUserContext>();
100+
Assert.IsNotNull(userContext);
101+
Assert.AreEqual("1", userContext.UserId);
102+
Assert.AreEqual("Jim", userContext.UserName);
103+
104+
var userRoles = userContext.GetUserRoles<int>().ToList();
105+
Assert.AreEqual(1, userRoles.Count);
106+
Assert.AreEqual(1, userRoles[0]);
107+
}
108+
109+
[TestMethod]
110+
public void TestMasaIdentity4()
111+
{
112+
var roles = new List<string>()
113+
{
114+
"admin", "admin2", "admin3","admin4"
115+
};
116+
var services = new ServiceCollection();
117+
var claimsPrincipal = new ClaimsPrincipal(new List<ClaimsIdentity>()
118+
{
119+
new(new List<Claim>()
120+
{
121+
new("sub", "1"),
122+
new(ClaimType.DEFAULT_USER_NAME, "Jim"),
123+
new(ClaimType.DEFAULT_USER_ROLE, JsonConvert.SerializeObject(roles))//"[\"1\"]"
124+
})
125+
});
126+
Mock<AuthenticationStateProvider> authenticationStateProvider = new();
127+
authenticationStateProvider
128+
.Setup(provider => provider.GetAuthenticationStateAsync())
129+
.ReturnsAsync(new AuthenticationState(claimsPrincipal));
130+
131+
services.AddScoped(_ => authenticationStateProvider.Object);
132+
services.AddMasaIdentity(option =>
133+
{
134+
option.UserId = "sub";
135+
});
136+
137+
Assert.IsTrue(services.Any<ICurrentPrincipalAccessor, BlazorCurrentPrincipalAccessor>(ServiceLifetime.Scoped));
138+
Assert.IsTrue(services.Any<IUserSetter>(ServiceLifetime.Scoped));
139+
Assert.IsTrue(services.Any<IUserContext>(ServiceLifetime.Scoped));
140+
Assert.IsTrue(services.Any<IMultiTenantUserContext>(ServiceLifetime.Scoped));
141+
Assert.IsTrue(services.Any<IMultiEnvironmentUserContext>(ServiceLifetime.Scoped));
142+
Assert.IsTrue(services.Any<IIsolatedUserContext>(ServiceLifetime.Scoped));
143+
144+
var serviceProvider = services.BuildServiceProvider();
145+
var userContext = serviceProvider.GetService<IUserContext>();
146+
Assert.IsNotNull(userContext);
147+
Assert.AreEqual("1", userContext.UserId);
148+
Assert.AreEqual("Jim", userContext.UserName);
149+
150+
var userRoles = userContext.GetUserRoles<string>().ToList();
151+
Assert.AreEqual(4, userRoles.Count);
152+
}
153+
65154
[TestMethod]
66155
public void TestIdentityByYaml()
67156
{

0 commit comments

Comments
 (0)