-
-
Save rqx110/1030ad64cf17852101a50f2ee8d14eba to your computer and use it in GitHub Desktop.
Creating an overridable session for ABP
This file contains hidden or 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
using System; | |
using Abp.Runtime.Session; | |
namespace Abp.Temp | |
{ | |
public static class AbpSessionExtensions | |
{ | |
public static IDisposable Override(this IAbpSession abpSession, int? tenantId, long? userId) | |
{ | |
var overridableSession = abpSession as OverridableSession; | |
if (overridableSession == null) | |
{ | |
throw new ArgumentException("abpSession should be instance of OverridableSession", "abpSession"); | |
} | |
return overridableSession.Override(tenantId, userId); | |
} | |
} | |
} |
This file contains hidden or 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
//Replace the session service in PreInitialize of your module | |
Configuration.ReplaceService<IAbpSession, OverridableSession>(DependencyLifeStyle.Singleton); |
This file contains hidden or 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
using Abp.Dependency; | |
using Abp.Runtime.Session; | |
namespace Abp.Temp | |
{ | |
//TEST CODE | |
public class MyService : ITransientDependency | |
{ | |
public IAbpSession AbpSession { get; set; } | |
public MyService() | |
{ | |
AbpSession = NullAbpSession.Instance; | |
} | |
public void Test1() | |
{ | |
//... | |
using (AbpSession.Override(1,2)) | |
{ | |
//using IAbpSession will return tenantId = 1, userId = 2 until using statement ends. | |
} | |
//... | |
} | |
} | |
} |
This file contains hidden or 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
using System; | |
using System.Threading; | |
using Abp.Configuration.Startup; | |
using Abp.Runtime.Session; | |
namespace Abp.Temp | |
{ | |
public class OverridableSession : ClaimsAbpSession | |
{ | |
private AsyncLocal<SessionOverride> OverridedSession { get; set; } | |
public OverridableSession(IMultiTenancyConfig multiTenancy) | |
: base(multiTenancy) | |
{ | |
OverridedSession = new AsyncLocal<SessionOverride>(); | |
OverridedSession.Value = null; | |
} | |
public override long? UserId | |
{ | |
get | |
{ | |
if (OverridedSession.Value != null) | |
{ | |
return OverridedSession.Value.UserId; | |
} | |
return base.UserId; | |
} | |
} | |
public override int? TenantId | |
{ | |
get | |
{ | |
if (OverridedSession.Value != null) | |
{ | |
return OverridedSession.Value.TenantId; | |
} | |
return base.TenantId; | |
} | |
} | |
public override long? ImpersonatorUserId | |
{ | |
get | |
{ | |
if (OverridedSession.Value != null) | |
{ | |
return OverridedSession.Value.ImpersonatorUserId; | |
} | |
return base.ImpersonatorUserId; | |
} | |
} | |
public override int? ImpersonatorTenantId | |
{ | |
get | |
{ | |
if (OverridedSession.Value != null) | |
{ | |
return OverridedSession.Value.ImpersonatorTenantId; | |
} | |
return base.ImpersonatorTenantId; | |
} | |
} | |
public IDisposable Override(int? tenantId, long? userId) | |
{ | |
OverridedSession.Value = new SessionOverride(tenantId, userId); | |
return new DisposeAction(() => | |
{ | |
OverridedSession.Value = null; | |
}); | |
} | |
} | |
} |
This file contains hidden or 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
using Abp.MultiTenancy; | |
using Abp.Runtime.Session; | |
namespace Abp.Temp | |
{ | |
public class SessionOverride : IAbpSession | |
{ | |
public long? UserId { get; set; } | |
public int? TenantId { get; set; } | |
public long? ImpersonatorUserId { get; set; } | |
public int? ImpersonatorTenantId { get; set; } | |
public MultiTenancySides MultiTenancySide | |
{ | |
get | |
{ | |
return !TenantId.HasValue ? MultiTenancySides.Host : MultiTenancySides.Tenant; | |
} | |
} | |
public SessionOverride(int? tenantId, long? userId) | |
{ | |
TenantId = tenantId; | |
UserId = userId; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment