diff --git a/MyApp/Configure.Auth.cs b/MyApp/Configure.Auth.cs index 40c29d6..ae482d7 100644 --- a/MyApp/Configure.Auth.cs +++ b/MyApp/Configure.Auth.cs @@ -1,8 +1,5 @@ -using Microsoft.AspNetCore.Identity; -using ServiceStack; using ServiceStack.Auth; using MyApp.Data; -using MyApp.ServiceModel; [assembly: HostingStartup(typeof(MyApp.ConfigureAuth))] @@ -11,98 +8,10 @@ namespace MyApp; public class ConfigureAuth : IHostingStartup { public void Configure(IWebHostBuilder builder) => builder - .ConfigureAppHost(appHost => + .ConfigureAppHost(appHost => { appHost.Plugins.Add(new AuthFeature(IdentityAuth.For(options => { options.EnableCredentialsAuth = true; }))); - - AddSeedUsers(appHost.GetApp()).Wait(); }); - - private async Task AddSeedUsers(IApplicationBuilder app) - { - var scopeFactory = app.ApplicationServices.GetRequiredService(); - - using var scope = scopeFactory.CreateScope(); - // Ensure EF Migrations has created Identity tables before using them. - var dbContext = scope.ServiceProvider.GetService(); - var dbCreated = await dbContext?.Database.EnsureCreatedAsync()!; - if (!dbCreated) - return; - //initializing custom roles - var roleManager = scope.ServiceProvider.GetRequiredService>(); - var userManager = scope.ServiceProvider.GetRequiredService>(); - string[] allRoles = [Roles.Admin, Roles.Manager, Roles.Employee]; - - void assertResult(IdentityResult result) - { - if (!result.Succeeded) - throw new Exception(result.Errors.First().Description); - } - - async Task EnsureUserAsync(ApplicationUser user, string password, string[]? roles = null) - { - var existingUser = await userManager.FindByEmailAsync(user.Email!); - if (existingUser != null) return; - - await userManager!.CreateAsync(user, password); - if (roles?.Length > 0) - { - var newUser = await userManager.FindByEmailAsync(user.Email!); - assertResult(await userManager.AddToRolesAsync(user, roles)); - } - } - - foreach (var roleName in allRoles) - { - var roleExist = await roleManager.RoleExistsAsync(roleName); - if (!roleExist) - { - //create the roles and seed them to the database: Question 1 - assertResult(await roleManager.CreateAsync(new IdentityRole(roleName))); - } - } - - await EnsureUserAsync(new ApplicationUser - { - DisplayName = "Test User", - Email = "test@email.com", - UserName = "test@email.com", - FirstName = "Test", - LastName = "User", - EmailConfirmed = true, - }, "p@55wOrd"); - - await EnsureUserAsync(new ApplicationUser - { - DisplayName = "Test Employee", - Email = "employee@email.com", - UserName = "employee@email.com", - FirstName = "Test", - LastName = "Employee", - EmailConfirmed = true, - }, "p@55wOrd", [Roles.Employee]); - - await EnsureUserAsync(new ApplicationUser - { - DisplayName = "Test Manager", - Email = "manager@email.com", - UserName = "manager@email.com", - FirstName = "Test", - LastName = "Manager", - EmailConfirmed = true, - }, "p@55wOrd", [Roles.Manager]); - - await EnsureUserAsync(new ApplicationUser - { - DisplayName = "Admin User", - Email = "admin@email.com", - UserName = "admin@email.com", - FirstName = "Admin", - LastName = "User", - EmailConfirmed = true, - }, "p@55wOrd", allRoles); - } - } diff --git a/MyApp/Configure.Db.Migrations.cs b/MyApp/Configure.Db.Migrations.cs index eda49fc..50db137 100644 --- a/MyApp/Configure.Db.Migrations.cs +++ b/MyApp/Configure.Db.Migrations.cs @@ -1,5 +1,8 @@ +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; using MyApp.Data; using MyApp.Migrations; +using MyApp.ServiceModel; using ServiceStack; using ServiceStack.Data; using ServiceStack.OrmLite; @@ -16,17 +19,107 @@ public void Configure(IWebHostBuilder builder) => builder var migrator = new Migrator(appHost.Resolve(), typeof(Migration1000).Assembly); AppTasks.Register("migrate", _ => { - // Run EF Migrations + var log = appHost.GetApplicationServices().GetRequiredService>(); + + log.LogInformation("Running EF Migrations..."); var scopeFactory = appHost.GetApplicationServices().GetRequiredService(); using (var scope = scopeFactory.CreateScope()) { using var db = scope.ServiceProvider.GetRequiredService(); - db.Database.EnsureCreated(); + var dbJustCreated = db.Database.EnsureCreated(); + db.Database.Migrate(); + + // Only seed users if DB was just created + if (dbJustCreated) + { + log.LogInformation("Adding Seed Users..."); + AddSeedUsers(scope.ServiceProvider).Wait(); + } } - // Run OrmLite Migrations + + log.LogInformation("Running OrmLite Migrations..."); migrator.Run(); }); AppTasks.Register("migrate.revert", args => migrator.Revert(args[0])); AppTasks.Run(); }); + + private async Task AddSeedUsers(IServiceProvider services) + { + var scopeFactory = services.GetRequiredService(); + + //initializing custom roles + var roleManager = services.GetRequiredService>(); + var userManager = services.GetRequiredService>(); + string[] allRoles = [Roles.Admin, Roles.Manager, Roles.Employee]; + + void assertResult(IdentityResult result) + { + if (!result.Succeeded) + throw new Exception(result.Errors.First().Description); + } + + async Task EnsureUserAsync(ApplicationUser user, string password, string[]? roles = null) + { + var existingUser = await userManager.FindByEmailAsync(user.Email!); + if (existingUser != null) return; + + await userManager!.CreateAsync(user, password); + if (roles?.Length > 0) + { + var newUser = await userManager.FindByEmailAsync(user.Email!); + assertResult(await userManager.AddToRolesAsync(user, roles)); + } + } + + foreach (var roleName in allRoles) + { + var roleExist = await roleManager.RoleExistsAsync(roleName); + if (!roleExist) + { + //create the roles and seed them to the database: Question 1 + assertResult(await roleManager.CreateAsync(new IdentityRole(roleName))); + } + } + + await EnsureUserAsync(new ApplicationUser + { + DisplayName = "Test User", + Email = "test@email.com", + UserName = "test@email.com", + FirstName = "Test", + LastName = "User", + EmailConfirmed = true, + }, "p@55wOrd"); + + await EnsureUserAsync(new ApplicationUser + { + DisplayName = "Test Employee", + Email = "employee@email.com", + UserName = "employee@email.com", + FirstName = "Test", + LastName = "Employee", + EmailConfirmed = true, + }, "p@55wOrd", [Roles.Employee]); + + await EnsureUserAsync(new ApplicationUser + { + DisplayName = "Test Manager", + Email = "manager@email.com", + UserName = "manager@email.com", + FirstName = "Test", + LastName = "Manager", + EmailConfirmed = true, + }, "p@55wOrd", [Roles.Manager]); + + await EnsureUserAsync(new ApplicationUser + { + DisplayName = "Admin User", + Email = "admin@email.com", + UserName = "admin@email.com", + FirstName = "Admin", + LastName = "User", + EmailConfirmed = true, + }, "p@55wOrd", allRoles); + } }