diff --git a/Marechai.Database/Seeders/All.cs b/Marechai.Database/Seeders/All.cs index addd30b7..435c4767 100644 --- a/Marechai.Database/Seeders/All.cs +++ b/Marechai.Database/Seeders/All.cs @@ -1,11 +1,14 @@ using System; using Marechai.Database.Models; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Configuration; namespace Marechai.Database.Seeders { public static class All { - public static void Seed(MarechaiContext context) + public static void Seed(MarechaiContext context, UserManager userManager, + RoleManager roleManager, IConfiguration configuration) { DateTime start, end; @@ -41,6 +44,14 @@ namespace Marechai.Database.Seeders License.Seed(context); end = DateTime.Now; + Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m", + (end - start).TotalSeconds); + + start = DateTime.Now; + Console.WriteLine("\u001b[31;1mSeeding application roles...\u001b[0m"); + Roles.Seed(userManager, roleManager, configuration); + end = DateTime.Now; + Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m", (end - start).TotalSeconds); diff --git a/Marechai.Database/Seeders/Roles.cs b/Marechai.Database/Seeders/Roles.cs new file mode 100644 index 00000000..b4aadc26 --- /dev/null +++ b/Marechai.Database/Seeders/Roles.cs @@ -0,0 +1,46 @@ +using System; +using System.Linq; +using Marechai.Database.Models; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Configuration; + +namespace Marechai.Database.Seeders +{ + public static class Roles + { + public static void Seed(UserManager userManager, RoleManager roleManager, + IConfiguration configuration) + { + var roles = configuration.GetSection("MarechaiRoles").GetChildren().Select(x => new + { + Name = x.GetValue("Name"), Description = x.GetValue("Description") + }).ToList(); + + if(roles.Count == 0) + return; + + foreach(var role in roles) + { + ApplicationRole existingRole = roleManager.FindByNameAsync(role.Name).Result; + + if(existingRole != null) + { + if(existingRole.Description != role.Description) + { + Console.WriteLine("Updating description for role {0}", role.Name); + existingRole.Description = role.Description; + } + + continue; + } + + var newRole = new ApplicationRole(role.Name, role.Description); + + IdentityResult result = roleManager.CreateAsync(newRole).Result; + + Console.WriteLine(result.Succeeded ? "New role {0} added successfully" : "Failed to add new role {0}", + role.Description); + } + } + } +} \ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 493aae7f..638f5e98 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.1008 + 3.0.99.1016 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website diff --git a/Marechai/Startup.cs b/Marechai/Startup.cs index 2d0aadba..e9e50242 100644 --- a/Marechai/Startup.cs +++ b/Marechai/Startup.cs @@ -72,7 +72,7 @@ namespace Marechai GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true). - AddEntityFrameworkStores(); + AddRoles().AddEntityFrameworkStores(); services.AddRazorPages(); services.AddServerSideBlazor(); @@ -86,7 +86,8 @@ namespace Marechai } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MarechaiContext context) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MarechaiContext context, + UserManager userManager, RoleManager roleManager) { if(env.IsDevelopment()) { @@ -122,7 +123,7 @@ namespace Marechai app.UseAuthentication(); app.UseAuthorization(); - All.Seed(context); + All.Seed(context, userManager, roleManager, Configuration); app.UseEndpoints(endpoints => { diff --git a/Marechai/appsettings.json b/Marechai/appsettings.json index 9a209025..f192d1bb 100644 --- a/Marechai/appsettings.json +++ b/Marechai/appsettings.json @@ -9,5 +9,11 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "MarechaiRoles": [ + { + "Name": "UberAdmin", + "Description": "Can administer everything, cannot be deleted, only one must exist." + } + ] } \ No newline at end of file