Skip to content

Commit 75dc125

Browse files
committed
5.0.0-preview015 - fix for signup etc
1 parent 75a2d99 commit 75dc125

7 files changed

Lines changed: 40 additions & 29 deletions

File tree

AuthPermissions.AspNetCore/CreateNuGetDebug.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
33
<metadata>
44
<id>AuthPermissions.AspNetCore</id>
5-
<version>5.0.0-preview014debug</version>
5+
<version>5.0.0-preview015debug</version>
66
<authors>Jon P Smith</authors>
77
<product>AuthPermissions.AspNetCore</product>
88
<copyright>Copyright (c) 2021 Jon P Smith</copyright>

AuthPermissions.AspNetCore/MultiProjPack.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<!-- See documentation for all the possible values -->
55
<metadata>
66
<id>AuthPermissions.AspNetCore</id>
7-
<version>5.0.0-preview014debug</version>
7+
<version>5.0.0-preview015debug</version>
88
<authors>Jon P Smith</authors>
99
<product>AuthPermissions.AspNetCore</product>
1010
<copyright>Copyright (c) 2021 Jon P Smith</copyright>

AuthPermissions.AspNetCore/ShardingServices/IGetDatabaseForNewTenant.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ public interface IGetDatabaseForNewTenant
1414
{
1515
/// <summary>
1616
/// This will look for a database for a new tenant when <see cref="TenantTypes.AddSharding"/> is on
17-
/// The job of this method that will return a DatabaseInfoName for the database to use, or an error if can't be found
17+
/// The job of this method is to find or create a database. If it successful, then it
18+
/// a) Gets the DatabaseInfoName of the entry for the database we are using
19+
/// b) It updates the Tenant's sharding information
1820
/// </summary>
1921
/// <param name="tenant">This is the tenant that you want to find/create a new database.</param>
2022
/// <param name="hasOwnDb">If true the tenant needs its own database. False means it shares a database.</param>
2123
/// <param name="region">If not null this provides geographic information to pick the nearest database server.</param>
2224
/// <param name="version">Optional: provides the version name in case that effects the database selection</param>
23-
/// <returns>Status with the DatabaseInfoName, or error if it can't find a database to work with</returns>
24-
Task<IStatusGeneric<string>> FindOrCreateDatabaseAsync(Tenant tenant, bool hasOwnDb, string region, string version = null);
25+
/// <returns>Status with the updated tenant, or error if it can't find a database to work with</returns>
26+
Task<IStatusGeneric<Tenant>> FindOrCreateDatabaseAsync(Tenant tenant, bool hasOwnDb, string region, string version = null);
2527

2628
/// <summary>
2729
/// If called it will undo what the <see cref="FindOrCreateDatabaseAsync"/> did.

AuthPermissions.SupportCode/AddUsersServices/SignInAndCreateTenant.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ public async Task<IStatusGeneric<AddNewUserDto>> SignUpNewTenantWithVersionAsync
127127
// a. Call the FindOrCreateDatabaseAsync to get the database and return the DatabaseInfoName
128128
// b. If OK, then update the tenant with the sharding data
129129
Tenant newTenant = null;
130-
string databaseInfoName = null;
131130
try
132131
{
133132
//NOTE: you mustn't exit this try / catch if you have errors, as we need to "clean up" if there errors
@@ -144,14 +143,8 @@ public async Task<IStatusGeneric<AddNewUserDto>> SignUpNewTenantWithVersionAsync
144143
//This method will find a database for the new tenant when using sharding
145144
var dbStatus = await _getShardingDb.FindOrCreateDatabaseAsync(newTenant,
146145
hasOwnDb, tenantData.Region, tenantData.Version);
147-
databaseInfoName = dbStatus.Result;
148-
149-
if (status.CombineStatuses(dbStatus).IsValid)
150-
{
151-
//Now set up the sharding parts of the tenant
152-
newTenant.UpdateShardingState(databaseInfoName, hasOwnDb);
153-
status.CombineStatuses(await _context.SaveChangesWithChecksAsync(_localizeDefault));
154-
}
146+
newTenant = dbStatus.Result ?? newTenant;
147+
status.CombineStatuses(dbStatus);
155148
}
156149
}
157150
}
@@ -171,7 +164,7 @@ public async Task<IStatusGeneric<AddNewUserDto>> SignUpNewTenantWithVersionAsync
171164
if (newTenant != null)
172165
{
173166
await _tenantAdmin.DeleteTenantAsync(newTenant.TenantId);
174-
if (databaseInfoName != null)
167+
if (newTenant.DatabaseInfoName != null)
175168
await _getShardingDb.RemoveLastDatabaseSetupAsync();
176169
}
177170

@@ -220,7 +213,7 @@ public async Task<IStatusGeneric<AddNewUserDto>> SignUpNewTenantWithVersionAsync
220213
await _addNewUserManager.RemoveAuthUserAsync(newAuthUser.UserId);
221214
if (newTenant != null)
222215
await _tenantAdmin.DeleteTenantAsync(newTenant.TenantId);
223-
if (databaseInfoName != null)
216+
if (newTenant?.DatabaseInfoName != null)
224217
await _getShardingDb.RemoveLastDatabaseSetupAsync();
225218

226219
return status;

AuthPermissions.SupportCode/DemoGetDatabaseForNewTenant.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using AuthPermissions.AspNetCore.ShardingServices;
55
using AuthPermissions.BaseCode.DataLayer.Classes;
6+
using AuthPermissions.BaseCode.DataLayer.EfCode;
67
using AuthPermissions.BaseCode.SetupCode;
78
using LocalizeMessagesAndErrors;
89
using StatusGeneric;
@@ -15,17 +16,21 @@ namespace AuthPermissions.SupportCode;
1516
public class DemoGetDatabaseForNewTenant : IGetDatabaseForNewTenant
1617
{
1718
private readonly IShardingConnections _shardingService;
19+
private readonly AuthPermissionsDbContext _context;
1820
private readonly IDefaultLocalizer _localizeDefault;
1921

2022
/// <summary>
2123
/// ctor
2224
/// </summary>
2325
/// <param name="shardingService"></param>
26+
/// <param name="context"></param>
2427
/// <param name="localizeProvider"></param>
25-
public DemoGetDatabaseForNewTenant(IShardingConnections shardingService,
28+
public DemoGetDatabaseForNewTenant(IShardingConnections shardingService,
29+
AuthPermissionsDbContext context,
2630
IAuthPDefaultLocalizer localizeProvider)
2731
{
2832
_shardingService = shardingService;
33+
_context = context;
2934
_localizeDefault = localizeProvider.DefaultLocalizer;
3035
}
3136

@@ -40,10 +45,10 @@ public DemoGetDatabaseForNewTenant(IShardingConnections shardingService,
4045
/// <param name="region">If not null this provides geographic information to pick the nearest database server.</param>
4146
/// <param name="version">Optional: provides the version name in case that effects the database selection</param>
4247
/// <returns>Status with the DatabaseInfoName, or error if it can't find a database to work with</returns>
43-
public async Task<IStatusGeneric<string>> FindOrCreateDatabaseAsync(Tenant tenant, bool hasOwnDb, string region,
48+
public async Task<IStatusGeneric<Tenant>> FindOrCreateDatabaseAsync(Tenant tenant, bool hasOwnDb, string region,
4449
string version = null)
4550
{
46-
var status = new StatusGenericLocalizer<string>(_localizeDefault);
51+
var status = new StatusGenericLocalizer<Tenant>(_localizeDefault);
4752

4853
//This gets the databases with the info on whether the database is available
4954
var dbsWithUsers = await _shardingService.GetDatabaseInfoNamesWithTenantNamesAsync();
@@ -68,7 +73,11 @@ public async Task<IStatusGeneric<string>> FindOrCreateDatabaseAsync(Tenant tenan
6873
status.AddErrorString("NoDbForTenant".ClassLocalizeKey(this, true),
6974
"We cannot create the tenant at this time. Please contact the support team with the code: no db available.");
7075

71-
return status;
76+
//Now set up the sharding parts of the tenant
77+
tenant.UpdateShardingState(foundDatabaseInfoName, hasOwnDb);
78+
status.CombineStatuses(await _context.SaveChangesWithChecksAsync(_localizeDefault));
79+
80+
return status.SetResult(tenant);
7281
}
7382

7483
/// <summary>

Test/StubClasses/StubIGetDatabaseForNewTenant.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,47 @@
33

44
using AuthPermissions.AspNetCore.ShardingServices;
55
using AuthPermissions.BaseCode.DataLayer.Classes;
6+
using AuthPermissions.BaseCode.DataLayer.EfCode;
67
using AuthPermissions.BaseCode.SetupCode;
78
using StatusGeneric;
89

910
namespace Test.StubClasses;
1011

1112
public class StubIGetDatabaseForNewTenant : IGetDatabaseForNewTenant
1213
{
14+
private readonly AuthPermissionsDbContext _context;
1315
private readonly bool _returnError;
1416

15-
public StubIGetDatabaseForNewTenant(bool returnError)
17+
public StubIGetDatabaseForNewTenant(AuthPermissionsDbContext context, bool returnError)
1618
{
19+
_context = context;
1720
_returnError = returnError;
1821
}
1922

2023
public bool RemoveLastDatabaseCalled { get; private set; }
2124

2225
/// <summary>
2326
/// This will look for a database for a new tenant when <see cref="TenantTypes.AddSharding"/> is on
24-
/// The job of this method that will return a DatabaseInfoName for the database to use, or an error if can't be found
27+
/// The job of this method is to find or create a database. If it successful, then it
28+
/// a) Gets the DatabaseInfoName of the entry for the database we are using
29+
/// b) It updates the Tenant's sharding information
2530
/// </summary>
2631
/// <param name="tenant"></param>
2732
/// <param name="hasOwnDb">If true the tenant needs its own database. False means it shares a database.</param>
2833
/// <param name="region">If not null this provides geographic information to pick the nearest database server.</param>
2934
/// <param name="version">Optional: provides the version name in case that effects the database selection</param>
3035
/// <returns>Status with the DatabaseInfoName, or error if it can't find a database to work with</returns>
31-
public Task<IStatusGeneric<string>> FindOrCreateDatabaseAsync(Tenant tenant, bool hasOwnDb, string region,
36+
public Task<IStatusGeneric<Tenant>> FindOrCreateDatabaseAsync(Tenant tenant, bool hasOwnDb, string region,
3237
string version)
3338
{
34-
var status = new StatusGenericHandler<string>();
39+
var status = new StatusGenericHandler<Tenant>();
3540
if (_returnError)
3641
return Task.FromResult(status.AddError("An Error"));
3742

38-
status.SetResult(hasOwnDb ? "OwnDb" : "SharedDb");
39-
return Task.FromResult<IStatusGeneric<string>>(status);
43+
tenant.UpdateShardingState(hasOwnDb ? "OwnDb" : "SharedDb", hasOwnDb);
44+
_context.SaveChanges();
45+
46+
return Task.FromResult<IStatusGeneric<Tenant>>(status.SetResult(tenant));
4047
}
4148

4249
/// <summary>

Test/UnitTests/TestSupportCode/TestSignInAndCreateTenant.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static (SignInAndCreateTenant service, AuthUsersAdminService userAdmin)
4747
var service = new SignInAndCreateTenant(authOptions, tenantAdmin,
4848
new StubAddNewUserManager(userAdmin, tenantAdmin, loginReturnsError), context,
4949
"en".SetupAuthPLoggingLocalizer(),
50-
overrideNormal ?? new StubIGetDatabaseForNewTenant(false));
50+
overrideNormal ?? new StubIGetDatabaseForNewTenant(context, false));
5151

5252
return (service, userAdmin);
5353
}
@@ -200,7 +200,7 @@ public async Task TestAddUserAndNewTenantAsync_Sharding_UndoTenant()
200200
using var context = new AuthPermissionsDbContext(options);
201201
context.Database.EnsureCreated();
202202

203-
var getDbCauseError = new StubIGetDatabaseForNewTenant(true);
203+
var getDbCauseError = new StubIGetDatabaseForNewTenant(context,true);
204204
var tuple = CreateISignInAndCreateTenant(context, TenantTypes.SingleLevel | TenantTypes.AddSharding, getDbCauseError);
205205
var authSettings = new AuthPermissionsOptions { InternalData = { EnumPermissionsType = typeof(Example3Permissions) } };
206206
var rolesSetup = new BulkLoadRolesService(context, authSettings);
@@ -237,7 +237,7 @@ public async Task TestAddUserAndNewTenantAsync_Sharding_UndoOnBadUser()
237237
using var context = new AuthPermissionsDbContext(options);
238238
context.Database.EnsureCreated();
239239

240-
var getDbCauseError = new StubIGetDatabaseForNewTenant(false);
240+
var getDbCauseError = new StubIGetDatabaseForNewTenant(context,false);
241241
var tuple = CreateISignInAndCreateTenant(context, TenantTypes.SingleLevel | TenantTypes.AddSharding,
242242
getDbCauseError, true);
243243
var authSettings = new AuthPermissionsOptions { InternalData = { EnumPermissionsType = typeof(Example3Permissions) } };

0 commit comments

Comments
 (0)