-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITenantDBProvider.cs
45 lines (38 loc) · 1.65 KB
/
ITenantDBProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace RepositoryPattern.Abstractions
{
public abstract class TenantDBProvider
{
private readonly ClaimsPrincipal _principal;
public TenantDBProvider(ClaimsPrincipal principal)
{
_principal = principal;
}
protected abstract Task<string> RetrieveConnectionStringFor(string tenantIdentifier);
public async Task<string> RetrieveConnectionStringAsync()
{
var tenantIdentifier = GetTenantIdentifierFromPrincipal();
if (tenantIdentifier is null)
throw new InvalidOperationException("Unable to find Tenant Identifier");
var connectionString = await RetrieveConnectionStringFor(tenantIdentifier);
if (connectionString is null)
throw new DataRepositoryException(DataRepositoryException.INVALID_CONNECTION_STRING);
return connectionString;
}
public string RetrieveConnectionString()
{
return RetrieveConnectionStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
}
/// <summary>
/// Used by <see cref="RetrieveConnectionString"/> to extract tenant identifier from ClaimsPrincipal. Override this method if your identifier is not in <see cref="ClaimTypes.NameIdentifier"/> claim type
/// </summary>
/// <returns>Tenant Identifier</returns>
protected virtual string GetTenantIdentifierFromPrincipal()
{
return _principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
}
}
}