Seed applications roles from application settings.

This commit is contained in:
2020-05-23 19:00:57 +01:00
parent dab05368e6
commit 9537d204bb
5 changed files with 70 additions and 6 deletions

View File

@@ -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<ApplicationUser> userManager,
RoleManager<ApplicationRole> 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);

View File

@@ -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<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager,
IConfiguration configuration)
{
var roles = configuration.GetSection("MarechaiRoles").GetChildren().Select(x => new
{
Name = x.GetValue<string>("Name"), Description = x.GetValue<string>("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);
}
}
}
}

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1008</Version>
<Version>3.0.99.1016</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>

View File

@@ -72,7 +72,7 @@ namespace Marechai
GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true).
AddEntityFrameworkStores<MarechaiContext>();
AddRoles<ApplicationRole>().AddEntityFrameworkStores<MarechaiContext>();
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<ApplicationUser> userManager, RoleManager<ApplicationRole> 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 =>
{

View File

@@ -9,5 +9,11 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"MarechaiRoles": [
{
"Name": "UberAdmin",
"Description": "Can administer everything, cannot be deleted, only one must exist."
}
]
}