27 lines
972 B
C#
27 lines
972 B
C#
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);
|
|
}
|
|
} |