Fix Access/Refres Token
This commit is contained in:
@@ -17,12 +17,17 @@ public sealed class TenantRepository : ITenantRepository
|
||||
private readonly IHttpContextAccessor _http;
|
||||
|
||||
public TenantRepository(IDbScope scope, ITenantResolver resolver, IHttpContextAccessor http)
|
||||
{ _scope = scope; _resolver = resolver; _http = http; }
|
||||
{
|
||||
_scope = scope;
|
||||
_resolver = resolver;
|
||||
_http = http;
|
||||
}
|
||||
|
||||
private AppDbContext Db()
|
||||
{
|
||||
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
|
||||
var platform = _resolver.Resolve(http, "@platform") ?? throw new InvalidOperationException("No platform tenant");
|
||||
var platform = _resolver.Resolve(http, "@platform") ??
|
||||
throw new InvalidOperationException("No platform tenant");
|
||||
_scope.EnsureForTenant(platform);
|
||||
return _scope.Get<AppDbContext>();
|
||||
}
|
||||
@@ -55,7 +60,8 @@ public sealed class TenantRepository : ITenantRepository
|
||||
return await Db().SaveChangesAsync(ct) > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(TenantConfig row, DateTimeOffset? ifUnmodifiedSince, CancellationToken ct = default)
|
||||
public async Task<bool> UpdateAsync(TenantConfig row, DateTimeOffset? ifUnmodifiedSince,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var key = Norm(row.TenantKey);
|
||||
var db = Db();
|
||||
@@ -65,8 +71,12 @@ public sealed class TenantRepository : ITenantRepository
|
||||
if (ifUnmodifiedSince.HasValue && cur.UpdatedAtUtc > ifUnmodifiedSince.Value)
|
||||
return false;
|
||||
|
||||
cur.Schema = row.Schema is null ? cur.Schema : (string.IsNullOrWhiteSpace(row.Schema) ? null : row.Schema.Trim());
|
||||
cur.ConnectionString = row.ConnectionString is null ? cur.ConnectionString : (string.IsNullOrWhiteSpace(row.ConnectionString) ? null : row.ConnectionString.Trim());
|
||||
cur.Schema = row.Schema is null
|
||||
? cur.Schema
|
||||
: (string.IsNullOrWhiteSpace(row.Schema) ? null : row.Schema.Trim());
|
||||
cur.ConnectionString = row.ConnectionString is null
|
||||
? cur.ConnectionString
|
||||
: (string.IsNullOrWhiteSpace(row.ConnectionString) ? null : row.ConnectionString.Trim());
|
||||
cur.Mode = row.Mode;
|
||||
cur.IsActive = row.IsActive;
|
||||
cur.UpdatedAtUtc = DateTimeOffset.UtcNow;
|
||||
@@ -112,7 +122,13 @@ public sealed class TenantRepository : ITenantRepository
|
||||
var db = Db();
|
||||
var ex = await db.Set<TenantDomain>().FirstOrDefaultAsync(x => x.Domain == d, ct);
|
||||
if (ex is null)
|
||||
await db.Set<TenantDomain>().AddAsync(new TenantDomain { Domain = d, TenantKey = key, IsPlatformBaseDomain = false, IsActive = true, UpdatedAtUtc = DateTimeOffset.UtcNow }, ct);
|
||||
await db.Set<TenantDomain>()
|
||||
.AddAsync(
|
||||
new TenantDomain
|
||||
{
|
||||
Domain = d, TenantKey = key, IsPlatformBaseDomain = false, IsActive = true,
|
||||
UpdatedAtUtc = DateTimeOffset.UtcNow
|
||||
}, ct);
|
||||
else
|
||||
{
|
||||
ex.TenantKey = key;
|
||||
@@ -120,6 +136,7 @@ public sealed class TenantRepository : ITenantRepository
|
||||
ex.IsActive = true;
|
||||
ex.UpdatedAtUtc = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
return await db.SaveChangesAsync(ct) > 0;
|
||||
}
|
||||
|
||||
@@ -147,6 +164,7 @@ public sealed class TenantRepository : ITenantRepository
|
||||
var key = Norm(tenantKey!);
|
||||
q = q.Where(x => x.TenantKey == key || x.IsPlatformBaseDomain);
|
||||
}
|
||||
|
||||
return await q.OrderBy(x => x.Domain).ToListAsync(ct);
|
||||
}
|
||||
|
||||
@@ -156,7 +174,13 @@ public sealed class TenantRepository : ITenantRepository
|
||||
var db = Db();
|
||||
var ex = await db.Set<TenantDomain>().FirstOrDefaultAsync(x => x.Domain == d, ct);
|
||||
if (ex is null)
|
||||
await db.Set<TenantDomain>().AddAsync(new TenantDomain { Domain = d, TenantKey = null, IsPlatformBaseDomain = true, IsActive = true, UpdatedAtUtc = DateTimeOffset.UtcNow }, ct);
|
||||
await db.Set<TenantDomain>()
|
||||
.AddAsync(
|
||||
new TenantDomain
|
||||
{
|
||||
Domain = d, TenantKey = null, IsPlatformBaseDomain = true, IsActive = true,
|
||||
UpdatedAtUtc = DateTimeOffset.UtcNow
|
||||
}, ct);
|
||||
else
|
||||
{
|
||||
ex.TenantKey = null;
|
||||
@@ -164,6 +188,7 @@ public sealed class TenantRepository : ITenantRepository
|
||||
ex.IsActive = true;
|
||||
ex.UpdatedAtUtc = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
return await db.SaveChangesAsync(ct) > 0;
|
||||
}
|
||||
|
||||
@@ -180,4 +205,36 @@ public sealed class TenantRepository : ITenantRepository
|
||||
|
||||
return await db.SaveChangesAsync(ct) > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// คืนค่า TenantKey จาก TenantId (Guid) ถ้าไม่เจอหรือไม่ active คืน null
|
||||
/// </summary>
|
||||
public async Task<string?> GetTenantKeyByTenantIdAsync(Guid tenantId, CancellationToken ct = default)
|
||||
{
|
||||
var key = await Db().Set<TenantConfig>()
|
||||
.AsNoTracking()
|
||||
.Where(x => x.TenantId == tenantId && x.IsActive)
|
||||
.Select(x => x.TenantKey)
|
||||
.FirstOrDefaultAsync(ct);
|
||||
|
||||
return string.IsNullOrWhiteSpace(key) ? null : key;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// คืนค่าแม็พ TenantId -> TenantKey เฉพาะที่เจอและ active
|
||||
/// </summary>
|
||||
public async Task<Dictionary<Guid, string>> MapTenantKeysAsync(IEnumerable<Guid> tenantIds, CancellationToken ct = default)
|
||||
{
|
||||
var ids = (tenantIds ?? Array.Empty<Guid>()).Distinct().ToArray();
|
||||
if (ids.Length == 0) return new Dictionary<Guid, string>();
|
||||
|
||||
var rows = await Db().Set<TenantConfig>()
|
||||
.AsNoTracking()
|
||||
.Where(x => ids.Contains(x.TenantId) && x.IsActive)
|
||||
.Select(x => new { x.TenantId, x.TenantKey })
|
||||
.ToListAsync(ct);
|
||||
|
||||
return rows.ToDictionary(x => x.TenantId, x => x.TenantKey);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user