1
This commit is contained in:
parent
96724376a6
commit
20b95da384
@ -1,73 +0,0 @@
|
|||||||
using System.Security.Claims;
|
|
||||||
using Fake.Security.Claims;
|
|
||||||
using FluentAssertions;
|
|
||||||
using Oms.Tests.Support;
|
|
||||||
using SimpleAdmin.Api;
|
|
||||||
|
|
||||||
namespace Oms.Tests;
|
|
||||||
|
|
||||||
public class SimpleCurrentUserTests
|
|
||||||
{
|
|
||||||
[Fact]
|
|
||||||
public void GetId_ShouldPreferNumericUserIdClaim()
|
|
||||||
{
|
|
||||||
var currentUser = CreateCurrentUser(
|
|
||||||
[
|
|
||||||
new Claim(FakeClaimTypes.UserId, "42"),
|
|
||||||
new Claim("mapId", "99"),
|
|
||||||
new Claim("customerId", "100")
|
|
||||||
]);
|
|
||||||
|
|
||||||
currentUser.GetId<long>().Should().Be(42);
|
|
||||||
currentUser.GetId<int>().Should().Be(42);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void GetId_ShouldFallBackToMapIdWhenUserIdIsGuid()
|
|
||||||
{
|
|
||||||
var currentUser = CreateCurrentUser(
|
|
||||||
[
|
|
||||||
new Claim(FakeClaimTypes.UserId, Guid.NewGuid().ToString()),
|
|
||||||
new Claim("mapId", "88")
|
|
||||||
]);
|
|
||||||
|
|
||||||
currentUser.GetId<long>().Should().Be(88);
|
|
||||||
currentUser.GetId<int>().Should().Be(88);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void GetId_ShouldFallBackToCustomerIdWhenMapIdMissing()
|
|
||||||
{
|
|
||||||
var currentUser = CreateCurrentUser(
|
|
||||||
[
|
|
||||||
new Claim(FakeClaimTypes.UserId, Guid.NewGuid().ToString()),
|
|
||||||
new Claim("customerId", "77")
|
|
||||||
]);
|
|
||||||
|
|
||||||
currentUser.GetId<long>().Should().Be(77);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Id_ShouldKeepGuidIdentitySemantics()
|
|
||||||
{
|
|
||||||
var guid = Guid.NewGuid();
|
|
||||||
var currentUser = CreateCurrentUser([new Claim(FakeClaimTypes.UserId, guid.ToString())]);
|
|
||||||
|
|
||||||
currentUser.Id.Should().Be(guid);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void GetId_ShouldReturnZeroWhenNoNumericClaimsExist()
|
|
||||||
{
|
|
||||||
var currentUser = CreateCurrentUser([new Claim(FakeClaimTypes.UserId, Guid.NewGuid().ToString())]);
|
|
||||||
|
|
||||||
currentUser.GetId<long>().Should().Be(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static SimpleCurrentUser CreateCurrentUser(params Claim[] claims)
|
|
||||||
{
|
|
||||||
var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, authenticationType: "Test"));
|
|
||||||
var principalAccessor = new TestCurrentPrincipalAccessor(principal);
|
|
||||||
return new SimpleCurrentUser(principalAccessor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,54 +0,0 @@
|
|||||||
using Fake.DependencyInjection;
|
|
||||||
using Fake.Security.Claims;
|
|
||||||
using Fake.Users;
|
|
||||||
|
|
||||||
namespace SimpleAdmin.Api;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 扩展当前用户解析逻辑,支持从 OMS 业务 claims 中读取数值型主体标识。
|
|
||||||
/// </summary>
|
|
||||||
[Dependency(Replace = true)]
|
|
||||||
public class SimpleCurrentUser(ICurrentPrincipalAccessor currentPrincipalAccessor) : CurrentUser(currentPrincipalAccessor)
|
|
||||||
{
|
|
||||||
public override T GetId<T>()
|
|
||||||
{
|
|
||||||
if (typeof(T) == typeof(long))
|
|
||||||
{
|
|
||||||
var userId = base.GetId<long>();
|
|
||||||
if (userId != 0)
|
|
||||||
{
|
|
||||||
return (T)(object)userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (T)(object)GetFallbackNumericId();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof(T) == typeof(int))
|
|
||||||
{
|
|
||||||
var userId = base.GetId<int>();
|
|
||||||
if (userId != 0)
|
|
||||||
{
|
|
||||||
return (T)(object)userId;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (T)(object)(int)GetFallbackNumericId();
|
|
||||||
}
|
|
||||||
|
|
||||||
return base.GetId<T>()!;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long GetFallbackNumericId()
|
|
||||||
{
|
|
||||||
if (long.TryParse(FindClaimOrNull(SimpleOmsClaimTypes.MapId)?.Value, out var mapId) && mapId > 0)
|
|
||||||
{
|
|
||||||
return mapId;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (long.TryParse(FindClaimOrNull(SimpleOmsClaimTypes.CustomerId)?.Value, out var customerId) && customerId > 0)
|
|
||||||
{
|
|
||||||
return customerId;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user