Renamed ApplicationDbContext to IdentityDbContext

This commit is contained in:
2025-01-08 23:08:23 -05:00
parent ede5483bbf
commit 75c18d9613
22 changed files with 83 additions and 89 deletions

View File

@@ -19,7 +19,7 @@ public static class DependencyInjection
services.AddHttpContextAccessor();
services.AddHealthChecks()
.AddDbContextCheck<ApplicationDbContext>();
.AddDbContextCheck<IdentityDbContext>();
services.AddRazorPages();

View File

@@ -1,9 +0,0 @@
using Microsoft.AspNetCore.Identity;
namespace Hutopy.Web.Features.Users;
public class ApplicationRole : IdentityRole<Guid>
{
public ApplicationRole() { }
public ApplicationRole(string roleName) : base(roleName) { }
}

View File

@@ -2,9 +2,9 @@
namespace Hutopy.Web.Features.Users.Data
{
public class ApplicationDbContext(
DbContextOptions<ApplicationDbContext> options)
: IdentityDbContext<ApplicationUser, ApplicationRole, Guid>(options)
public class IdentityDbContext(
DbContextOptions<IdentityDbContext> options)
: IdentityDbContext<IdentityUser, IdentityRole, Guid>(options)
{
public const string SchemaName = "Identity";
protected override void OnModelCreating(ModelBuilder

View File

@@ -4,11 +4,11 @@ namespace Hutopy.Web.Features.Users.Data;
public static class InitializerExtensions
{
public static async Task InitialiseApplicationDatabaseAsync(this WebApplication app)
public static async Task InitialiseIdentityDatabaseAsync(this WebApplication app)
{
using var scope = app.Services.CreateScope();
var initializer = scope.ServiceProvider.GetRequiredService<ApplicationDbContextInitializer>();
var initializer = scope.ServiceProvider.GetRequiredService<IdentityDbContextInitializer>();
await initializer.InitialiseAsync();
@@ -16,10 +16,10 @@ public static class InitializerExtensions
}
}
public class ApplicationDbContextInitializer(
ILogger<ApplicationDbContextInitializer> logger,
ApplicationDbContext context,
RoleManager<ApplicationRole> roleManager)
public class IdentityDbContextInitializer(
ILogger<IdentityDbContextInitializer> logger,
IdentityDbContext context,
RoleManager<IdentityRole> roleManager)
{
public async Task InitialiseAsync()
{
@@ -49,13 +49,13 @@ public class ApplicationDbContextInitializer(
private async Task TrySeedAsync()
{
var administratorRole = new ApplicationRole(KnownRoles.Administrator);
var administratorRole = new IdentityRole(KnownRoles.Administrator);
if (roleManager.Roles.All(r => r.Name != administratorRole.Name))
{
await roleManager.CreateAsync(administratorRole);
}
var roleCreator = new ApplicationRole(KnownRoles.Creator);
var roleCreator = new IdentityRole(KnownRoles.Creator);
if (roleManager.Roles.All(r => r.Name != roleCreator.Name))
{
await roleManager.CreateAsync(roleCreator);

View File

@@ -11,7 +11,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Hutopy.Web.Features.Users.Data.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[DbContext(typeof(IdentityDbContext))]
[Migration("20241020183421_Initial")]
partial class Initial
{
@@ -26,7 +26,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Hutopy.Web.Features.Users.ApplicationRole", b =>
modelBuilder.Entity("Hutopy.Web.Features.Users.IdentityRole", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
@@ -53,7 +53,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
b.ToTable("AspNetRoles", "Identity");
});
modelBuilder.Entity("Hutopy.Web.Features.Users.ApplicationUser", b =>
modelBuilder.Entity("Hutopy.Web.Features.Users.IdentityUser", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
@@ -250,7 +250,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
{
b.HasOne("Hutopy.Web.Features.Users.ApplicationRole", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityRole", null)
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
@@ -259,7 +259,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<System.Guid>", b =>
{
b.HasOne("Hutopy.Web.Features.Users.ApplicationUser", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -268,7 +268,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<System.Guid>", b =>
{
b.HasOne("Hutopy.Web.Features.Users.ApplicationUser", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -277,13 +277,13 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<System.Guid>", b =>
{
b.HasOne("Hutopy.Web.Features.Users.ApplicationRole", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityRole", null)
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Hutopy.Web.Features.Users.ApplicationUser", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -292,7 +292,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<System.Guid>", b =>
{
b.HasOne("Hutopy.Web.Features.Users.ApplicationUser", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)

View File

@@ -10,7 +10,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Hutopy.Web.Features.Users.Data.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[DbContext(typeof(IdentityDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
@@ -23,7 +23,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Hutopy.Web.Features.Users.ApplicationRole", b =>
modelBuilder.Entity("Hutopy.Web.Features.Users.IdentityRole", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
@@ -50,7 +50,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
b.ToTable("AspNetRoles", "Identity");
});
modelBuilder.Entity("Hutopy.Web.Features.Users.ApplicationUser", b =>
modelBuilder.Entity("Hutopy.Web.Features.Users.IdentityUser", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
@@ -247,7 +247,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
{
b.HasOne("Hutopy.Web.Features.Users.ApplicationRole", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityRole", null)
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
@@ -256,7 +256,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<System.Guid>", b =>
{
b.HasOne("Hutopy.Web.Features.Users.ApplicationUser", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -265,7 +265,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<System.Guid>", b =>
{
b.HasOne("Hutopy.Web.Features.Users.ApplicationUser", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -274,13 +274,13 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<System.Guid>", b =>
{
b.HasOne("Hutopy.Web.Features.Users.ApplicationRole", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityRole", null)
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Hutopy.Web.Features.Users.ApplicationUser", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
@@ -289,7 +289,7 @@ namespace Hutopy.Web.Features.Users.Data.Migrations
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<System.Guid>", b =>
{
b.HasOne("Hutopy.Web.Features.Users.ApplicationUser", null)
b.HasOne("Hutopy.Web.Features.Users.IdentityUser", null)
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)

View File

@@ -14,8 +14,8 @@ public static class DependencyInjection
services.AddDbContext<MessagingDbContext>(configureAction);
services.AddScoped<MessagingDbContextInitializer>();
services.AddDbContext<ApplicationDbContext>(configureAction);
services.AddScoped<ApplicationDbContextInitializer>();
services.AddDbContext<IdentityDbContext>(configureAction);
services.AddScoped<IdentityDbContextInitializer>();
services.AddAuthentication()
.AddBearerToken(IdentityConstants.BearerScheme);
@@ -23,12 +23,12 @@ public static class DependencyInjection
services.AddAuthorizationBuilder();
services
.AddIdentityCore<ApplicationUser>()
.AddUserManager<ApplicationUserManager>()
.AddRoles<ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddIdentityCore<IdentityUser>()
.AddUserManager<IdentityUserManager>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<IdentityDbContext>()
.AddApiEndpoints()
.AddSignInManager<SignInManager<ApplicationUser>>()
.AddSignInManager<SignInManager<IdentityUser>>()
.AddDefaultTokenProviders();
// Singleton services

View File

@@ -1,5 +1,4 @@
using Hutopy.Web.Common;
using Hutopy.Web.Common.Security;
using Hutopy.Web.Common.Security;
namespace Hutopy.Web.Features.Users.Handlers;
@@ -9,7 +8,7 @@ public record ChangeAddressRequest(
[PublicAPI]
public class ChangeAddressHandler(
ApplicationUserManager userManager)
IdentityUserManager userManager)
: Endpoint<ChangeAddressRequest>
{
public override void Configure()

View File

@@ -1,5 +1,4 @@
using Hutopy.Web.Common;
using Hutopy.Web.Common.Security;
using Hutopy.Web.Common.Security;
namespace Hutopy.Web.Features.Users.Handlers;
@@ -9,7 +8,7 @@ public record ChangeAliasRequest(
[PublicAPI]
public class ChangeAliasHandler(
ApplicationUserManager userManager)
IdentityUserManager userManager)
: Endpoint<ChangeAliasRequest>
{
public override void Configure()

View File

@@ -1,5 +1,4 @@
using Hutopy.Web.Common;
using Hutopy.Web.Common.Security;
using Hutopy.Web.Common.Security;
namespace Hutopy.Web.Features.Users.Handlers;
@@ -9,7 +8,7 @@ public record ChangeBirthDateRequest(
[PublicAPI]
public class ChangeBirthDateHandler(
ApplicationUserManager userManager)
IdentityUserManager userManager)
: Endpoint<ChangeBirthDateRequest>
{
public override void Configure()

View File

@@ -1,5 +1,4 @@
using Hutopy.Web.Common;
using Hutopy.Web.Common.Security;
using Hutopy.Web.Common.Security;
namespace Hutopy.Web.Features.Users.Handlers;
@@ -9,7 +8,7 @@ public record ChangeEmailRequest(
[PublicAPI]
public class ChangeEmailHandler(
ApplicationUserManager userManager)
IdentityUserManager userManager)
: Endpoint<ChangeEmailRequest>
{
public override void Configure()

View File

@@ -1,5 +1,4 @@
using Hutopy.Web.Common;
using Hutopy.Web.Common.Security;
using Hutopy.Web.Common.Security;
namespace Hutopy.Web.Features.Users.Handlers;
@@ -10,7 +9,7 @@ public record ChangeFullnameRequest(
[PublicAPI]
public class ChangeFullnameHandler(
ApplicationUserManager userManager)
IdentityUserManager userManager)
: Endpoint<ChangeFullnameRequest>
{
public override void Configure()

View File

@@ -1,5 +1,4 @@
using Hutopy.Web.Common;
using Hutopy.Web.Common.Security;
using Hutopy.Web.Common.Security;
namespace Hutopy.Web.Features.Users.Handlers;
@@ -9,7 +8,7 @@ public record ChangePhoneRequest(
[PublicAPI]
public class ChangePhoneHandler(
ApplicationUserManager userManager)
IdentityUserManager userManager)
: Endpoint<ChangePhoneRequest>
{
public override void Configure()

View File

@@ -1,5 +1,4 @@
using Hutopy.Web.Common;
using Hutopy.Web.Common.BlobStorage;
using Hutopy.Web.Common.BlobStorage;
using Hutopy.Web.Common.Security;
namespace Hutopy.Web.Features.Users.Handlers;
@@ -25,7 +24,7 @@ public sealed class ChangePortraitRequestValidator : Validator<ChangePortraitReq
[PublicAPI]
public class ChangePortraitHandler(
ApplicationUserManager userManager,
IdentityUserManager userManager,
AzureBlobStorage blobStorage)
: Endpoint<ChangePortraitRequest, ChangePortraitResponse>
{

View File

@@ -39,8 +39,8 @@ public record LoginWithGoogleResponse(
[PublicAPI]
public class LoginWithGoogleHandler(
IHttpClientFactory httpClientFactory,
ApplicationUserManager userManager,
SignInManager<ApplicationUser> signInManager,
IdentityUserManager userManager,
SignInManager<IdentityUser> signInManager,
IOptionsSnapshot<JwtOptions> jwtOptions)
: Endpoint<LoginWithGoogleRequest, LoginWithGoogleResponse>
{
@@ -91,7 +91,7 @@ public class LoginWithGoogleHandler(
if (user is null)
{
var generatedPassword = PasswordGenerator.GeneratePassword(10, 12);
var generatedUser = new ApplicationUser
var generatedUser = new IdentityUser
{
UserName = userInfo.Email,
Email = userInfo.Email,

View File

@@ -0,0 +1,9 @@
using Microsoft.AspNetCore.Identity;
namespace Hutopy.Web.Features.Users;
public class IdentityRole : IdentityRole<Guid>
{
public IdentityRole() { }
public IdentityRole(string roleName) : base(roleName) { }
}

View File

@@ -4,14 +4,14 @@ using Hutopy.Web.Features.Users.Models;
namespace Hutopy.Web.Features.Users;
public class IdentityService(
ApplicationUserManager userManager,
IdentityUserManager userManager,
IHttpContextAccessor contextAccessor
)
{
public async Task<Result<Guid>> CreateUserAsync(string email, string userName, string firstName, string lastName,
string password)
{
var applicationUser = new ApplicationUser
var applicationUser = new IdentityUser
{
UserName = userName, Email = email, Firstname = firstName, Lastname = lastName
};
@@ -28,7 +28,7 @@ public class IdentityService(
return result;
}
private static UserModel BuildModelFrom(ApplicationUser response)
private static UserModel BuildModelFrom(IdentityUser response)
{
var userModel = new UserModel
{

View File

@@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Identity;
namespace Hutopy.Web.Features.Users;
public class ApplicationUser : IdentityUser<Guid>
public class IdentityUser : IdentityUser<Guid>
{
[MaxLength(255)] public string? Alias { get; set; }
[MaxLength(255)] public string? Firstname { get; set; }

View File

@@ -3,17 +3,17 @@ using Microsoft.Extensions.Options;
namespace Hutopy.Web.Features.Users;
public sealed class ApplicationUserManager(
IUserStore<ApplicationUser> store,
public sealed class IdentityUserManager(
IUserStore<IdentityUser> store,
IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<ApplicationUser> passwordHasher,
IEnumerable<IUserValidator<ApplicationUser>> userValidators,
IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators,
IPasswordHasher<IdentityUser> passwordHasher,
IEnumerable<IUserValidator<IdentityUser>> userValidators,
IEnumerable<IPasswordValidator<IdentityUser>> passwordValidators,
ILookupNormalizer keyNormalizer,
IdentityErrorDescriber errors,
IServiceProvider services,
ILogger<UserManager<ApplicationUser>> logger)
: UserManager<ApplicationUser>(
ILogger<UserManager<IdentityUser>> logger)
: UserManager<IdentityUser>(
store,
optionsAccessor,
passwordHasher,

View File

@@ -1,7 +1,8 @@
@using Hutopy.Web.Features.Users
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Mvc.TagHelpers
@inject SignInManager<ApplicationUser> SignInManager
@using IdentityUser = Hutopy.Web.Features.Users.IdentityUser
@inject SignInManager<IdentityUser> SignInManager
@{
string? returnUrl = null;

View File

@@ -84,7 +84,7 @@ builder.Services.AddFastEndpoints();
builder.Services.AddUsersModule(options =>
options.UseNpgsql(
postgresConnectionString,
o => o.MigrationsHistoryTable("__EFMigrationsHistory", ApplicationDbContext.SchemaName)));
o => o.MigrationsHistoryTable("__EFMigrationsHistory", IdentityDbContext.SchemaName)));
builder.Services.AddContentModule(options =>
options.UseNpgsql(
postgresConnectionString,
@@ -114,7 +114,7 @@ app.UseAuthentication();
app.UseAuthorization();
// Initialize and seed the db.
await app.InitialiseApplicationDatabaseAsync();
await app.InitialiseIdentityDatabaseAsync();
await app.InitialiseContentDbContextAsync();
await app.InitialiseMessagingDbContextAsync();
await app.InitialiseMembershipDbContextAsync();

View File

@@ -17,7 +17,7 @@ public static class WebApplicationExtensions
using var scope = app.Services.CreateScope();
var seeder = new TestDataSeeder(
scope.ServiceProvider.GetRequiredService<ApplicationUserManager>(),
scope.ServiceProvider.GetRequiredService<IdentityUserManager>(),
scope.ServiceProvider.GetRequiredService<ContentDbContext>(),
scope.ServiceProvider.GetRequiredService<MessagingDbContext>(),
scope.ServiceProvider.GetRequiredService<MembershipDbContext>());
@@ -27,7 +27,7 @@ public static class WebApplicationExtensions
}
internal class TestDataSeeder(
ApplicationUserManager userManager,
IdentityUserManager userManager,
ContentDbContext contentContext,
MessagingDbContext messagingContext,
MembershipDbContext membershipContext)
@@ -181,12 +181,12 @@ internal class TestDataSeeder(
return replies;
}
private async Task<ApplicationUser> CreateUserAsync(
private async Task<IdentityUser> CreateUserAsync(
string name,
string? portraitUrl,
params string[] roles)
{
var user = new ApplicationUser
var user = new IdentityUser
{
UserName = $"{name}@test",
Email = $"{name}@test",
@@ -204,7 +204,7 @@ internal class TestDataSeeder(
return user;
}
private readonly List<ApplicationUser> _users =
private readonly List<IdentityUser> _users =
[
];