diff --git a/README.md b/README.md index 74fc8fa..dda8578 100644 --- a/README.md +++ b/README.md @@ -1 +1,10 @@ -# SpeakingInBitsWeb \ No newline at end of file +# SpeakingInBitsWeb + +## Logging as instructor in development +To log in as an instructor in development, you can use the +following credentials: +- Username: `DefaultInstructor` +- Password: `Programming01#` + +*Note*: This account is only available in the development environment +and seeded within `/Models/SeedData.cs`. \ No newline at end of file diff --git a/SpeakingInBitsWeb.slnx b/SpeakingInBitsWeb.slnx index 0e40db4..bfc03a1 100644 --- a/SpeakingInBitsWeb.slnx +++ b/SpeakingInBitsWeb.slnx @@ -1,3 +1,6 @@ + + + diff --git a/SpeakingInBitsWeb/Models/SeedData.cs b/SpeakingInBitsWeb/Models/SeedData.cs new file mode 100644 index 0000000..61db0f2 --- /dev/null +++ b/SpeakingInBitsWeb/Models/SeedData.cs @@ -0,0 +1,57 @@ +using Microsoft.AspNetCore.Identity; + +namespace SpeakingInBitsWeb.Models +{ + /// + /// Provides methods for seeding initial data into the database. + /// + public static class SeedData + { + /// + /// Creates all necessary roles in the system if they don't already exist. + /// + /// The role manager service. + public static async Task CreateRolesAsync(RoleManager roleManager) + { + string[] roles = ["Instructor", "Student"]; + + foreach (var role in roles) + { + if (!await roleManager.RoleExistsAsync(role)) + { + await roleManager.CreateAsync(new IdentityRole(role)); + } + } + } + + /// + /// Creates a default instructor user if one doesn't already exist. + /// + /// The user manager service. + public static async Task CreateDefaultUserAsync(UserManager userManager) + { + string defaultEmail = "instructor@example.com"; + ApplicationUser? defaultUser = await userManager.FindByEmailAsync(defaultEmail); + + if (defaultUser == null) + { + ApplicationUser newUser = new() + { + UserName = "DefaultInstructor", + Email = defaultEmail, + EmailConfirmed = true, + FirstName = "Default", + LastName = "Instructor" + }; + + // Ensure password meets password strength requirements + IdentityResult result = await userManager.CreateAsync(newUser, "Programming01#"); + + if (result.Succeeded) + { + await userManager.AddToRoleAsync(newUser, "Instructor"); + } + } + } + } +} diff --git a/SpeakingInBitsWeb/Program.cs b/SpeakingInBitsWeb/Program.cs index fad9f1e..76d988c 100644 --- a/SpeakingInBitsWeb/Program.cs +++ b/SpeakingInBitsWeb/Program.cs @@ -12,6 +12,7 @@ builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) + .AddRoles() // Add role support to Identity .AddEntityFrameworkStores(); builder.Services.AddControllersWithViews(); @@ -44,4 +45,15 @@ app.MapRazorPages() .WithStaticAssets(); +#if DEBUG +using (var scope = app.Services.CreateScope()) +{ + var roleManager = scope.ServiceProvider.GetRequiredService>(); + var userManager = scope.ServiceProvider.GetRequiredService>(); + + await SeedData.CreateRolesAsync(roleManager); + await SeedData.CreateDefaultUserAsync(userManager); +} +#endif + app.Run();