Skip to content

Commit

Permalink
fix(Identity): Fix the problem that IUserSetter temporarily changes t… (
Browse files Browse the repository at this point in the history
#226)

* fix(Identity): Fix the problem that IUserSetter temporarily changes the user to obtain user information error

* chore: remove invalid parameters
  • Loading branch information
zhenlei520 authored Sep 2, 2022
1 parent c2f0504 commit 244ab48
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ protected UserContext(ITypeConvertProvider typeConvertProvider)

public IDisposable Change<TIdentityUser>(TIdentityUser identityUser) where TIdentityUser : IIdentityUser
{
var userModelType = typeof(TIdentityUser);
ArgumentNullException.ThrowIfNull(identityUser);

var userModelType = identityUser.GetType();
var user = GetUser(userModelType);
CurrentUser[userModelType] = identityUser;
return new DisposeAction(() => CurrentUser[userModelType] = user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,37 @@ public void TestTemporarilyUserReturnUserIdEqual1()
Assert.AreEqual(null, isolationUserContext.GetTenantId<int?>());
}

[TestMethod]
public void TestChange()
{
var services = new ServiceCollection();
services.AddMasaIdentityModel();

var serviceProvider = services.BuildServiceProvider();
var userSetter = serviceProvider.GetService<IUserSetter>();
Assert.IsNotNull(userSetter);

IIdentityUser identityUser = new IdentityUser()
{
Id = "1"
};
using (userSetter.Change(identityUser))
{
var userContext = serviceProvider.GetService<IUserContext>();
Assert.IsNotNull(userContext);
Assert.AreEqual("1", userContext.UserId);

Assert.AreEqual(1, userContext.GetUserId<int>());
}

IIdentityUser? identityUser2 = null;

Assert.ThrowsException<ArgumentNullException>(() =>
{
userSetter.Change(identityUser2!);
});
}

[TestMethod]
public void TestCustomerUserModelReturnTrueNameEqualLisi()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static class RedisHelper

public static RedisValue ConvertFromValue<T>(T value)
{
var type = typeof(T);
var type = value?.GetType() ?? typeof(T);
dynamic redisValue;
switch (GetCompressMode(type))
{
Expand Down

0 comments on commit 244ab48

Please sign in to comment.