Skip to content

Commit fbed7bd

Browse files
committed
5.0.1-preview003
1 parent 65df7a7 commit fbed7bd

File tree

10 files changed

+68
-31
lines changed

10 files changed

+68
-31
lines changed

AuthPermissions.AspNetCore/CreateNuGetDebug.nuspec

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
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-preview022debug</version>
5+
<version>5.0.1-preview003</version>
66
<authors>Jon P Smith</authors>
77
<product>AuthPermissions.AspNetCore</product>
88
<copyright>Copyright (c) 2021 Jon P Smith</copyright>
99
<description>Provides extra authorization and multi-tenant features to a ASP.NET Core application.</description>
1010
<releaseNotes>
11-
- BREAKING CHANGE(small): If you are using sharding with your multi-tenant app, then read the UpdateToVersion5.md file.
12-
- New feature: You can use any database provider supported by EF Core, not just SQLServer and Postgres - see documentation for more details
11+
- Minor improvements AccessDatabaseInformationJsonFile
12+
- Minor improvements to SignInAndCreateTenant (better handling of errors)
13+
- Minor improvements to IndividualUserAddUserManager (check password)
1314
</releaseNotes>
1415
<license type="expression">MIT</license>
1516
<projectUrl>https://github.com/JonPSmith/AuthPermissions.AspNetCore</projectUrl>

AuthPermissions.AspNetCore/MultiProjPack.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
<!-- See documentation for all the possible values -->
55
<metadata>
66
<id>AuthPermissions.AspNetCore</id>
7-
<version>5.0.0</version>
7+
<version>5.0.1-preview003</version>
88
<authors>Jon P Smith</authors>
99
<product>AuthPermissions.AspNetCore</product>
1010
<copyright>Copyright (c) 2021 Jon P Smith</copyright>
1111
<description>Provides extra authorization and multi-tenant features to a ASP.NET Core application.</description>
1212
<releaseNotes>
13-
- BREAKING CHANGE(medium): If you are using sharding with your multi-tenant app, then read the UpdateToVersion5.md file.
14-
- New feature: You can now use a custom database provider with the AuthP library - see documentation for more details.
15-
- New feature: It easier to setup sharding / hybrid multi-tenant application via extension method called SetupMultiTenantSharding
16-
- New feature: Ability to replace parts of the sharding services, e.g. store sharding data in a database instead of a json file.
17-
</releaseNotes>
13+
- Minor improvements AccessDatabaseInformationJsonFile
14+
- Minor improvements to SignInAndCreateTenant (better handling of errors)
15+
- Minor improvements to IndividualUserAddUserManager (check password)
16+
</releaseNotes>
1817
<license type="expression">MIT</license>
1918
<projectUrl>https://github.com/JonPSmith/AuthPermissions.AspNetCore</projectUrl>
2019
<icon>images\AuthPermissionsAspNetCoreNuGetIcon.png</icon>

AuthPermissions.AspNetCore/ShardingServices/IGetDatabaseForNewTenant.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ public interface IGetDatabaseForNewTenant
2626
Task<IStatusGeneric<Tenant>> FindOrCreateDatabaseAsync(Tenant tenant, bool hasOwnDb, string region, string version = null);
2727

2828
/// <summary>
29-
/// If called it will undo what the <see cref="FindOrCreateDatabaseAsync"/> did.
29+
/// If called it will undo what the <see cref="FindOrCreateDatabaseAsync"/> did,
30+
/// i.e. deleting the database (or at least use tenantChangeService to delete the data) and remove the sharding data.
3031
/// This is called if there was a problem with the new user such that the new tenant would be deleted.
32+
/// NOTE: This method may be called even if the <see cref="FindOrCreateDatabaseAsync"/> hasn't been called.
3133
/// </summary>
3234
/// <returns>Status</returns>
3335
Task<IStatusGeneric> RemoveLastDatabaseSetupAsync();

AuthPermissions.AspNetCore/ShardingServices/ShardingConnectionsJsonFile.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@ public class ConnectionStringsOption : Dictionary<string, string> {}
2323
public class ShardingConnectionsJsonFile : IShardingConnections
2424
{
2525
private readonly ConnectionStringsOption _connectionDict;
26+
private readonly IOptionsMonitor<ShardingSettingsOption> _shardingSettingsAccessor;
2627
private readonly AuthPermissionsDbContext _context;
2728
private readonly IDefaultLocalizer _localizeDefault;
2829
private readonly AuthPermissionsOptions _options;
2930

30-
/// <summary>
31-
/// This contains the most up to date data in the shardingsettings json file
32-
/// </summary>
33-
private List<DatabaseInformation> _databaseInformation;
34-
3531
/// <summary>
3632
/// This contains the methods with are specific to a database provider
3733
/// </summary>
@@ -59,10 +55,9 @@ public ShardingConnectionsJsonFile(IOptionsSnapshot<ConnectionStringsOption> con
5955
{
6056
//thanks to https://stackoverflow.com/questions/37287427/get-multiple-connection-strings-in-appsettings-json-without-ef
6157
_connectionDict = connectionsAccessor.Value;
58+
_shardingSettingsAccessor = shardingSettingsAccessor;
6259
_context = context;
6360
_options = options;
64-
SetDatabaseInformation(shardingSettingsAccessor.CurrentValue);
65-
shardingSettingsAccessor.OnChange(SetDatabaseInformation);
6661

6762
DatabaseProviderMethods = databaseProviderMethods.ToDictionary(x => x.AuthPDatabaseType);
6863
ShardingDatabaseProviders = DatabaseProviderMethods.Values.ToDictionary(x => x.DatabaseProviderShortName);
@@ -78,7 +73,7 @@ public ShardingConnectionsJsonFile(IOptionsSnapshot<ConnectionStringsOption> con
7873
/// <returns>A list of <see cref="DatabaseInformation"/> from the sharding settings file</returns>
7974
public List<DatabaseInformation> GetAllPossibleShardingData()
8075
{
81-
return _databaseInformation;
76+
return GetDatabaseInformation();
8277
}
8378

8479
/// <summary>
@@ -109,7 +104,7 @@ public IEnumerable<string> GetConnectionStringNames()
109104

110105
var result = new List<(string databaseInfoName, bool? hasOwnDb, List<string>)>();
111106
//Add sharding database names that have no tenants in them so that you can see all the connection string names
112-
foreach (var databaseInfoName in _databaseInformation.Select(x => x.Name))
107+
foreach (var databaseInfoName in GetDatabaseInformation().Select(x => x.Name))
113108
{
114109
result.Add(grouped.ContainsKey(databaseInfoName)
115110
? (databaseInfoName,
@@ -135,14 +130,14 @@ public string FormConnectionString(string databaseInfoName)
135130
if (databaseInfoName == null)
136131
throw new AuthPermissionsException("The name of the database date can't be null");
137132

138-
var databaseData = _databaseInformation.SingleOrDefault(x => x.Name == databaseInfoName);
133+
var databaseData = GetDatabaseInformation().SingleOrDefault(x => x.Name == databaseInfoName);
139134
if (databaseData == null)
140135
throw new AuthPermissionsException(
141136
$"The database information with the name of '{databaseInfoName}' wasn't founds.");
142137

143138
if (!_connectionDict.TryGetValue(databaseData.ConnectionName, out var connectionString))
144139
throw new AuthPermissionsException(
145-
$"Could not find the connection name '{connectionString}' that the sharding database data '{databaseInfoName}' requires.");
140+
$"Could not find the connection name '{databaseData.ConnectionName}' that the sharding database data '{databaseInfoName}' requires.");
146141

147142
if (!ShardingDatabaseProviders.TryGetValue(databaseData.DatabaseType,
148143
out IDatabaseSpecificMethods databaseSpecificMethods))
@@ -191,13 +186,12 @@ public IStatusGeneric TestFormingConnectionString(DatabaseInformation databaseIn
191186
//private methods
192187

193188
/// <summary>
194-
/// This takes to information from the OptionsMonitor and sets the _databaseInformation parameter
189+
/// This gets the most up to date data in the shardingsettings json file
195190
/// </summary>
196-
/// <returns></returns>
197-
private void SetDatabaseInformation(ShardingSettingsOption fromMonitor )
191+
private List<DatabaseInformation> GetDatabaseInformation()
198192
{
199-
_databaseInformation = (fromMonitor.ShardingDatabases == null || !fromMonitor.ShardingDatabases.Any())
193+
return _shardingSettingsAccessor.CurrentValue == null || !_shardingSettingsAccessor.CurrentValue.ShardingDatabases.Any()
200194
? new List<DatabaseInformation> { DatabaseInformation.FormDefaultDatabaseInfo(_options, _context) }
201-
: fromMonitor.ShardingDatabases;
195+
: _shardingSettingsAccessor.CurrentValue.ShardingDatabases;
202196
}
203197
}

AuthPermissions.BaseCode/AuthPermissionsOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class AuthPermissionsOptions
2222
public TenantTypes TenantType { get; set; }
2323

2424
/// <summary>
25-
/// If sharding is turned on, then you can define the sharding database data to use if not
25+
/// If sharding is turned on, then this parameter defines the name of the connection string for the main database.
2626
/// This defaults to "Default Database", which should be set up to link to the database that also contains the AuthP data
2727
/// </summary>
2828
public string ShardingDefaultDatabaseInfoName { get; set; } = "Default Database";

AuthPermissions.SupportCode/AddUsersServices/Authentication/IndividualUserAddUserManager.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public IndividualUserAddUserManager(IAuthUsersAdminService authUsersAdmin, IAuth
5757
public AddNewUserDto UserLoginData { get; private set; }
5858

5959
/// <summary>
60-
/// This makes a quick check that the user isn't already has an AuthUser
60+
/// This makes a quick check that the user isn't already has an AuthUser and the password is valid
6161
/// </summary>
6262
/// <param name="newUser"></param>
6363
/// <returns>status, with error if there an user already</returns>
@@ -68,6 +68,20 @@ public async Task<IStatusGeneric> CheckNoExistingAuthUserAsync(AddNewUserDto new
6868
return status.AddErrorString("ExistingUser".ClassLocalizeKey(this, true), //common message
6969
"There is already an AuthUser with your email, so you can't add another.",
7070
nameof(AddNewUserDto.Email));
71+
72+
//Check the password matches the
73+
var passwordValidator = new PasswordValidator<TIdentity>();
74+
var checkPassword = await passwordValidator.ValidateAsync(_userManager, null, newUser.Password);
75+
if (!checkPassword.Succeeded)
76+
{
77+
foreach (var passwordError in checkPassword.Errors)
78+
{
79+
status.AddErrorString("BadPasswordFormat".ClassLocalizeKey(this, true),
80+
passwordError.Description, nameof(AddNewUserDto.Password));
81+
}
82+
}
83+
84+
7185
return status;
7286
}
7387

AuthPermissions.SupportCode/AddUsersServices/SignInAndCreateTenant.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,10 @@ public async Task<IStatusGeneric<AddNewUserDto>> SignUpNewTenantWithVersionAsync
163163

164164
if (newTenant != null)
165165
{
166-
await _tenantAdmin.DeleteTenantAsync(newTenant.TenantId);
167166
if (newTenant.DatabaseInfoName != null)
168167
await _getShardingDb.RemoveLastDatabaseSetupAsync();
168+
169+
await _tenantAdmin.DeleteTenantAsync(newTenant.TenantId);
169170
}
170171

171172
return status;

Test/StubClasses/StubIGetDatabaseForNewTenant.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public StubIGetDatabaseForNewTenant(AuthPermissionsDbContext context, bool retur
1919
_context = context;
2020
_returnError = returnError;
2121
}
22+
private Tenant _tenant;
2223

2324
public bool RemoveLastDatabaseCalled { get; private set; }
2425

@@ -36,6 +37,7 @@ public StubIGetDatabaseForNewTenant(AuthPermissionsDbContext context, bool retur
3637
public Task<IStatusGeneric<Tenant>> FindOrCreateDatabaseAsync(Tenant tenant, bool hasOwnDb, string region,
3738
string version)
3839
{
40+
_tenant = tenant;
3941
var status = new StatusGenericHandler<Tenant>();
4042
if (_returnError)
4143
return Task.FromResult(status.AddError("An Error"));
@@ -54,6 +56,7 @@ public Task<IStatusGeneric<Tenant>> FindOrCreateDatabaseAsync(Tenant tenant, boo
5456
public Task<IStatusGeneric> RemoveLastDatabaseSetupAsync()
5557
{
5658
RemoveLastDatabaseCalled = true;
59+
_context.Tenants.Remove(_tenant);
5760
return Task.FromResult<IStatusGeneric>(new StatusGenericHandler());
5861
}
5962
}

Test/UnitTests/TestSupportCode/TestIndividualUserAddUserManager.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public async Task TestCheckNoExistingAuthUserAsync_Email(string email, bool isVa
9999
context.AddMultipleUsersWithRolesInDb();
100100

101101
var service = _serviceProvider.GetRequiredService<IAddNewUserManager>();
102-
var userData = new AddNewUserDto { Email = email };
102+
var userData = new AddNewUserDto { Email = email, Password = "[email protected]" };
103103

104104
context.ChangeTracker.Clear();
105105

@@ -110,6 +110,29 @@ public async Task TestCheckNoExistingAuthUserAsync_Email(string email, bool isVa
110110
status.IsValid.ShouldEqual(isValid);
111111
}
112112

113+
[Theory]
114+
[InlineData("123", false)]
115+
[InlineData("[email protected]", true)]
116+
public async Task TestCheckNoExistingAuthUserAsync_Password(string password, bool isValid)
117+
{
118+
//SETUP
119+
var context = _serviceProvider.GetRequiredService<AuthPermissionsDbContext>();
120+
context.Database.EnsureClean();
121+
context.AddMultipleUsersWithRolesInDb();
122+
123+
var service = _serviceProvider.GetRequiredService<IAddNewUserManager>();
124+
var userData = new AddNewUserDto { Email = "AnotherEmail", Password = password };
125+
126+
context.ChangeTracker.Clear();
127+
128+
//ATTEMPT
129+
var status = await service.CheckNoExistingAuthUserAsync(userData);
130+
131+
//VERIFY
132+
status.IsValid.ShouldEqual(isValid);
133+
_output.WriteLine(status.GetAllErrors());
134+
}
135+
113136
[Fact]
114137
public async Task TestSetUserInfoAsyncOk()
115138
{

Test/UnitTests/TestSupportCode/TestSignInAndCreateTenant.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public async Task TestAddUserAndNewTenantAsync_Sharding_UndoTenant()
206206
var rolesSetup = new BulkLoadRolesService(context, authSettings);
207207
await rolesSetup.AddRolesToDatabaseAsync(Example3AppAuthSetupData.RolesDefinition);
208208

209-
var userData = new AddNewUserDto { Email = "me[email protected]" };
209+
var userData = new AddNewUserDto { Email = "Me[email protected]"};
210210
var tenantData = new AddNewTenantDto
211211
{
212212
TenantName = "New Tenant",

0 commit comments

Comments
 (0)