Skip to content

Commit 9abe4a1

Browse files
authored
Merge pull request #5741 from sbwalker/dev
refactor TenantDBContext to accomodate AspNetUserPasskeys
2 parents 57aeac2 + 273097d commit 9abe4a1

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ public static IServiceCollection AddOqtane(this IServiceCollection services, ICo
8484
options.Cookie.HttpOnly = true;
8585
});
8686

87-
services.AddIdentityCore<IdentityUser>(options => { })
87+
services.AddIdentityCore<IdentityUser>(options =>
88+
{
89+
// must be set prior to AddEntityFrameworkStores
90+
options.Stores.SchemaVersion = IdentitySchemaVersions.Version3;
91+
})
8892
.AddEntityFrameworkStores<TenantDBContext>()
8993
.AddSignInManager()
9094
.AddDefaultTokenProviders()
@@ -167,7 +171,7 @@ public static IServiceCollection AddOqtaneAssemblies(this IServiceCollection ser
167171
public static IServiceCollection AddOqtaneDbContext(this IServiceCollection services)
168172
{
169173
services.AddDbContext<MasterDBContext>(options => { }, ServiceLifetime.Transient);
170-
services.AddDbContext<TenantDBContext>(options => { }, ServiceLifetime.Transient);
174+
services.AddDbContext<TenantDBContext>(options => { }, ServiceLifetime.Scoped);
171175
services.AddDbContextFactory<TenantDBContext>(opt => { }, ServiceLifetime.Transient);
172176
return services;
173177
}
@@ -366,9 +370,6 @@ public static IServiceCollection ConfigureOqtaneIdentityOptions(this IServiceCol
366370
// User settings
367371
options.User.RequireUniqueEmail = false;
368372
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
369-
370-
// Stores settings
371-
options.Stores.SchemaVersion = IdentitySchemaVersions.Version3;
372373
});
373374

374375
// overrides defined in appsettings

Oqtane.Server/Infrastructure/DatabaseManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ private Installation MigrateTenants(InstallConfig install)
395395
var connectionString = _configManager.GetSetting($"{SettingKeys.ConnectionStringsSection}:{tenant.DBConnectionString}", "");
396396
if (!string.IsNullOrEmpty(connectionString))
397397
{
398-
using (var tenantDbContext = new TenantDBContext(DBContextDependencies))
398+
using (var tenantDbContext = new TenantDBContext(new DbContextOptions<TenantDBContext>(), DBContextDependencies))
399399
{
400400
AddEFMigrationsHistory(sql, connectionString, tenant.DBType, tenant.Version, false);
401401
// push latest model into database

Oqtane.Server/Managers/UserManager.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,15 @@ public async Task<User> LoginUser(User user, bool setCookie, bool isPersistent)
369369
IdentityUser identityuser = await _identityUserManager.FindByNameAsync(user.Username);
370370
if (identityuser != null)
371371
{
372+
try
373+
{
374+
var passKeysFunctional = await _identityUserManager.GetPasskeysAsync(identityuser);
375+
}
376+
catch (Exception ex)
377+
{
378+
var error = ex.ToString();
379+
}
380+
372381
var result = await _identitySignInManager.CheckPasswordSignInAsync(identityuser, user.Password, true);
373382
if (result.Succeeded)
374383
{

Oqtane.Server/Repository/Context/DBContextBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Threading.Tasks;
55
using Microsoft.AspNetCore.Http;
66
using Microsoft.AspNetCore.Identity;
7-
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
87
using Microsoft.EntityFrameworkCore;
98
using Microsoft.EntityFrameworkCore.Migrations;
109
using Microsoft.Extensions.Configuration;
@@ -19,7 +18,7 @@
1918

2019
namespace Oqtane.Repository
2120
{
22-
public class DBContextBase : IdentityUserContext<IdentityUser>
21+
public class DBContextBase : DbContext
2322
{
2423
private readonly ITenantManager _tenantManager;
2524
private readonly IHttpContextAccessor _accessor;
@@ -75,8 +74,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
7574
protected override void OnModelCreating(ModelBuilder builder)
7675
{
7776
base.OnModelCreating(builder);
78-
79-
ActiveDatabase.UpdateIdentityStoreTableNames(builder);
8077
}
8178

8279
public override int SaveChanges()

Oqtane.Server/Repository/Context/TenantDBContext.cs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,99 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Identity;
6+
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
17
using Microsoft.EntityFrameworkCore;
8+
using Microsoft.EntityFrameworkCore.Migrations;
9+
using Microsoft.Extensions.Configuration;
10+
using Oqtane.Databases.Interfaces;
11+
using Oqtane.Extensions;
12+
using Oqtane.Infrastructure;
13+
using Oqtane.Migrations.Framework;
214
using Oqtane.Models;
315
using Oqtane.Repository.Databases.Interfaces;
16+
using Oqtane.Shared;
417

518
// ReSharper disable CheckNamespace
619
// ReSharper disable MemberCanBePrivate.Global
720
// ReSharper disable UnusedAutoPropertyAccessor.Global
821

922
namespace Oqtane.Repository
1023
{
11-
public class TenantDBContext : DBContextBase, IMultiDatabase
24+
public class TenantDBContext : IdentityUserContext<IdentityUser>, IMultiDatabase
1225
{
13-
public TenantDBContext(IDBContextDependencies DBContextDependencies) : base(DBContextDependencies) { }
26+
private readonly ITenantManager _tenantManager;
27+
private readonly IHttpContextAccessor _accessor;
28+
private readonly IConfigurationRoot _config;
29+
private string _connectionString = "";
30+
private string _databaseType = "";
31+
32+
public TenantDBContext(DbContextOptions<TenantDBContext> options, IDBContextDependencies DBContextDependencies) : base(options)
33+
{
34+
_tenantManager = DBContextDependencies.TenantManager;
35+
_accessor = DBContextDependencies.Accessor;
36+
_config = DBContextDependencies.Config;
37+
}
38+
39+
public IDatabase ActiveDatabase { get; set; }
40+
41+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
42+
{
43+
optionsBuilder.ReplaceService<IMigrationsAssembly, MultiDatabaseMigrationsAssembly>();
44+
45+
if (string.IsNullOrEmpty(_connectionString))
46+
{
47+
Tenant tenant = _tenantManager.GetTenant();
48+
if (tenant != null)
49+
{
50+
_connectionString = _config.GetConnectionString(tenant.DBConnectionString);
51+
if (_connectionString != null)
52+
{
53+
_connectionString = _connectionString.Replace($"|{Constants.DataDirectory}|", AppDomain.CurrentDomain.GetData(Constants.DataDirectory)?.ToString());
54+
_databaseType = tenant.DBType;
55+
}
56+
else
57+
{
58+
// tenant connection string does not exist in appsettings.json
59+
}
60+
}
61+
}
62+
63+
if (!string.IsNullOrEmpty(_databaseType))
64+
{
65+
var type = Type.GetType(_databaseType);
66+
ActiveDatabase = Activator.CreateInstance(type) as IDatabase;
67+
}
68+
69+
if (!string.IsNullOrEmpty(_connectionString) && ActiveDatabase != null)
70+
{
71+
optionsBuilder.UseOqtaneDatabase(ActiveDatabase, _connectionString);
72+
}
73+
74+
base.OnConfiguring(optionsBuilder);
75+
}
76+
77+
protected override void OnModelCreating(ModelBuilder builder)
78+
{
79+
base.OnModelCreating(builder);
80+
81+
ActiveDatabase.UpdateIdentityStoreTableNames(builder);
82+
}
83+
84+
public override int SaveChanges()
85+
{
86+
DbContextUtils.SaveChanges(this, _accessor);
87+
88+
return base.SaveChanges();
89+
}
90+
91+
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
92+
{
93+
DbContextUtils.SaveChanges(this, _accessor);
94+
95+
return base.SaveChangesAsync(cancellationToken);
96+
}
1497

1598
public virtual DbSet<Site> Site { get; set; }
1699
public virtual DbSet<Page> Page { get; set; }

0 commit comments

Comments
 (0)