-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
73 changed files
with
3,266 additions
and
462 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
52 changes: 52 additions & 0 deletions
52
...sions.AspNetCore/GetDataKeyCode/GetShardingDataAppAndHierarchicalUsersAccessTenantData.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2022 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
using AuthPermissions.AspNetCore.AccessTenantData; | ||
using AuthPermissions.AspNetCore.Services; | ||
using AuthPermissions.CommonCode; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace AuthPermissions.AspNetCore.GetDataKeyCode | ||
{ | ||
|
||
/// <summary> | ||
/// This service is registered if a multi-tenant setup with sharding on | ||
/// NOTE: There are other versions if the "Access the data of other tenant" is turned on | ||
/// </summary> | ||
public class GetShardingDataAppAndHierarchicalUsersAccessTenantData : IGetShardingDataFromUser | ||
{ | ||
/// <summary> | ||
/// This will return the AuthP's DataKey and the connection string via the ConnectionName claim. | ||
/// This version works with tenant users, but is little bit slower than the version that only works with app users | ||
/// If no user, or no claim then both parameters will be null | ||
/// </summary> | ||
/// <param name="accessor"></param> | ||
/// <param name="connectionService">Service to get the current connection string for the </param> | ||
/// <param name="linkService"></param> | ||
public GetShardingDataAppAndHierarchicalUsersAccessTenantData(IHttpContextAccessor accessor, | ||
IShardingConnections connectionService, | ||
ILinkToTenantDataService linkService) | ||
{ | ||
var overrideLink = linkService.GetShardingDataOfLinkedTenant(); | ||
|
||
DataKey = overrideLink.dataKey ?? accessor.HttpContext?.User.GetAuthDataKeyFromUser(); | ||
|
||
var connectionStringName = overrideLink.connectionName | ||
?? accessor.HttpContext?.User.GetConnectionNameFromUser(); | ||
|
||
if (connectionStringName != null) | ||
ConnectionString = connectionService.GetNamedConnectionString(connectionStringName); | ||
} | ||
|
||
/// <summary> | ||
/// The AuthP' DataKey, can be null. | ||
/// </summary> | ||
public string DataKey { get; } | ||
|
||
/// <summary> | ||
/// This contains the connection string to the database to use | ||
/// If null, then use the default connection string as defined at the time when your application's DbContext was registered | ||
/// </summary> | ||
public string ConnectionString { get; } | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
AuthPermissions.AspNetCore/GetDataKeyCode/GetShardingDataUserAccessTenantData.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2022 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
using AuthPermissions.AspNetCore.AccessTenantData; | ||
using AuthPermissions.AspNetCore.Services; | ||
using AuthPermissions.CommonCode; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace AuthPermissions.AspNetCore.GetDataKeyCode | ||
{ | ||
|
||
/// <summary> | ||
/// This service is registered if a multi-tenant setup with sharding on | ||
/// NOTE: There are other versions if the "Access the data of other tenant" is turned on | ||
/// </summary> | ||
public class GetShardingDataUserAccessTenantData : IGetShardingDataFromUser | ||
{ | ||
/// <summary> | ||
/// This will return the AuthP's DataKey and the connection string via the ConnectionName claim, | ||
/// but only if the user doesn't have a tenant, i.e. an app admin user | ||
/// If no user, or no claim then both parameters will be null | ||
/// </summary> | ||
/// <param name="accessor"></param> | ||
/// <param name="connectionService">Service to get the current connection string for the </param> | ||
/// <param name="linkService"></param> | ||
public GetShardingDataUserAccessTenantData(IHttpContextAccessor accessor, | ||
IShardingConnections connectionService, | ||
ILinkToTenantDataService linkService) | ||
{ | ||
var overrideLink = linkService.GetShardingDataOfLinkedTenant(); | ||
|
||
DataKey = accessor.HttpContext?.User.GetAuthDataKeyFromUser() | ||
?? overrideLink.dataKey; | ||
var connectionStringName = accessor.HttpContext?.User.GetConnectionNameFromUser() | ||
?? overrideLink.connectionName; | ||
|
||
if (connectionStringName != null) | ||
ConnectionString = connectionService.GetNamedConnectionString(connectionStringName); | ||
} | ||
|
||
/// <summary> | ||
/// The AuthP' DataKey, can be null. | ||
/// </summary> | ||
public string DataKey { get; } | ||
|
||
/// <summary> | ||
/// This contains the connection string to the database to use | ||
/// If null, then use the default connection string as defined at the time when your application's DbContext was registered | ||
/// </summary> | ||
public string ConnectionString { get; } | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
AuthPermissions.AspNetCore/GetDataKeyCode/GetShardingDataUserNormal.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2022 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
using AuthPermissions.AspNetCore.Services; | ||
using AuthPermissions.CommonCode; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace AuthPermissions.AspNetCore.GetDataKeyCode | ||
{ | ||
|
||
/// <summary> | ||
/// This service is registered if a multi-tenant setup with sharding on | ||
/// NOTE: There are other versions if the "Access the data of other tenant" is turned on | ||
/// </summary> | ||
public class GetShardingDataUserNormal : IGetShardingDataFromUser | ||
{ | ||
/// <summary> | ||
/// This will return the AuthP's DataKey and the connection string via the ConnectionName claim. | ||
/// If no user, or no claim then both parameters will be null | ||
/// </summary> | ||
/// <param name="accessor"></param> | ||
/// <param name="connectionService">Service to get the current connection string for the </param> | ||
public GetShardingDataUserNormal(IHttpContextAccessor accessor, IShardingConnections connectionService) | ||
{ | ||
DataKey = accessor.HttpContext?.User.GetAuthDataKeyFromUser(); | ||
var connectionStringName = accessor.HttpContext?.User.GetConnectionNameFromUser(); | ||
if (connectionStringName != null) | ||
ConnectionString = connectionService.GetNamedConnectionString(connectionStringName); | ||
} | ||
|
||
/// <summary> | ||
/// The AuthP' DataKey, can be null. | ||
/// </summary> | ||
public string DataKey { get; } | ||
|
||
/// <summary> | ||
/// This contains the connection string to the database to use | ||
/// If null, then use the default connection string as defined at the time when your application's DbContext was registered | ||
/// </summary> | ||
public string ConnectionString { get; } | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
AuthPermissions.AspNetCore/GetDataKeyCode/IGetShardingDataFromUser.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) 2022 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
namespace AuthPermissions.AspNetCore.GetDataKeyCode; | ||
|
||
/// <summary> | ||
/// This is the interface provides both the DataKey and the connection string | ||
/// </summary> | ||
public interface IGetShardingDataFromUser | ||
{ | ||
/// <summary> | ||
/// The DataKey to be used for multi-tenant applications | ||
/// </summary> | ||
string DataKey { get; } | ||
|
||
/// <summary> | ||
/// This contains the connection string to the database to use | ||
/// If null, then use the default connection string as defined at the time when your application's DbContext was registered | ||
/// </summary> | ||
string ConnectionString { get; } | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
AuthPermissions.AspNetCore/Services/IShardingConnections.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) 2022 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/ | ||
// Licensed under MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace AuthPermissions.AspNetCore.Services; | ||
|
||
/// <summary> | ||
/// The interface for the service to manage the connection strings in the appsetting file. | ||
/// </summary> | ||
public interface IShardingConnections | ||
{ | ||
/// <summary> | ||
/// This returns all the connection strings name in the application's appsettings | ||
/// </summary> | ||
/// <returns>The name of each connection string</returns> | ||
IEnumerable<string> GetAllConnectionStringNames(); | ||
|
||
/// <summary> | ||
/// This will provide the connection string for the entry with the given connection string name | ||
/// </summary> | ||
/// <param name="connectionName">The name of the connection string you want to access</param> | ||
/// <returns>The connection string, or null if not found</returns> | ||
string GetNamedConnectionString(string connectionName); | ||
} |
Oops, something went wrong.