Init Git
This commit is contained in:
27
AMREZ.EOP.Infrastructures/Tenancy/TenantRlsInterceptor.cs
Normal file
27
AMREZ.EOP.Infrastructures/Tenancy/TenantRlsInterceptor.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Data.Common;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
|
||||
namespace AMREZ.EOP.Infrastructures.Tenancy;
|
||||
|
||||
public sealed class TenantRlsInterceptor : DbConnectionInterceptor
|
||||
{
|
||||
private string _tenant = "";
|
||||
public void Configure(string tenantId) => _tenant = tenantId?.Trim() ?? "";
|
||||
|
||||
public override async Task ConnectionOpenedAsync(
|
||||
DbConnection connection,
|
||||
ConnectionEndEventData eventData,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
// ตั้งแค่ RLS session param เท่านั้น
|
||||
if (!string.IsNullOrWhiteSpace(_tenant))
|
||||
{
|
||||
await using var cmd = connection.CreateCommand();
|
||||
var v = _tenant.Replace("'", "''");
|
||||
cmd.CommandText = $"SET LOCAL app.tenant_id = '{v}';";
|
||||
await cmd.ExecuteNonQueryAsync(cancellationToken);
|
||||
}
|
||||
|
||||
await base.ConnectionOpenedAsync(connection, eventData, cancellationToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user