mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Seed application users.
This commit is contained in:
@@ -49,7 +49,15 @@ namespace Marechai.Database.Seeders
|
|||||||
|
|
||||||
start = DateTime.Now;
|
start = DateTime.Now;
|
||||||
Console.WriteLine("\u001b[31;1mSeeding application roles...\u001b[0m");
|
Console.WriteLine("\u001b[31;1mSeeding application roles...\u001b[0m");
|
||||||
Roles.Seed(userManager, roleManager, configuration);
|
Roles.Seed(roleManager, configuration);
|
||||||
|
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 users...\u001b[0m");
|
||||||
|
Users.Seed(userManager, roleManager);
|
||||||
end = DateTime.Now;
|
end = DateTime.Now;
|
||||||
|
|
||||||
Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m",
|
Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m",
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ namespace Marechai.Database.Seeders
|
|||||||
{
|
{
|
||||||
public static class Roles
|
public static class Roles
|
||||||
{
|
{
|
||||||
public static void Seed(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager,
|
public static void Seed(RoleManager<ApplicationRole> roleManager, IConfiguration configuration)
|
||||||
IConfiguration configuration)
|
|
||||||
{
|
{
|
||||||
var roles = configuration.GetSection("MarechaiRoles").GetChildren().Select(x => new
|
var roles = configuration.GetSection("MarechaiRoles").GetChildren().Select(x => new
|
||||||
{
|
{
|
||||||
|
|||||||
71
Marechai.Database/Seeders/Users.cs
Normal file
71
Marechai.Database/Seeders/Users.cs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Marechai.Database.Models;
|
||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
|
||||||
|
namespace Marechai.Database.Seeders
|
||||||
|
{
|
||||||
|
public static class Users
|
||||||
|
{
|
||||||
|
public static void Seed(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager)
|
||||||
|
{
|
||||||
|
ApplicationRole uberAdminRole = roleManager.FindByNameAsync("UberAdmin").Result;
|
||||||
|
|
||||||
|
if(uberAdminRole is null)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Cannot find UberAdmin role, is database properly seeded?");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
IList<ApplicationUser> uberAdmins = userManager.GetUsersInRoleAsync("UberAdmin").Result;
|
||||||
|
|
||||||
|
if(uberAdmins.Count > 1)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Too many uberadmins, only one can exist");
|
||||||
|
|
||||||
|
foreach(ApplicationUser user in uberAdmins)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Removing uberadmin permissions from user {0}", user.UserName);
|
||||||
|
|
||||||
|
userManager.RemoveFromRoleAsync(user, "UberAdmin");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(uberAdmins.Count == 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ApplicationUser uberAdmin = userManager.FindByEmailAsync("claunia@claunia.com").Result ??
|
||||||
|
userManager.FindByNameAsync("claunia").Result;
|
||||||
|
|
||||||
|
if(uberAdmin is null)
|
||||||
|
{
|
||||||
|
uberAdmin = new ApplicationUser
|
||||||
|
{
|
||||||
|
UserName = "claunia", Email = "claunia@claunia.com", EmailConfirmed = true
|
||||||
|
};
|
||||||
|
|
||||||
|
byte[] newPass = new byte[8];
|
||||||
|
new Random().NextBytes(newPass);
|
||||||
|
string newPassString = Convert.ToBase64String(newPass);
|
||||||
|
|
||||||
|
IdentityResult result = userManager.CreateAsync(uberAdmin, newPassString).Result;
|
||||||
|
|
||||||
|
if(result.Succeeded)
|
||||||
|
{
|
||||||
|
userManager.AddToRoleAsync(uberAdmin, "UberAdmin").Wait();
|
||||||
|
Console.WriteLine("Created new claunia uberadmin with password {0}.", newPassString);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Could not create new uberadmin.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Giving uberadmin permissions to user claunia");
|
||||||
|
userManager.AddToRoleAsync(uberAdmin, "UberAdmin").Wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=claunia/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Marechai/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Marechai/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Portillo/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Portillo/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=SIMD/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=SIMD/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=uberadmin/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=uberadmins/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=videogame/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=videogame/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<Version>3.0.99.1017</Version>
|
<Version>3.0.99.1018</Version>
|
||||||
<Company>Canary Islands Computer Museum</Company>
|
<Company>Canary Islands Computer Museum</Company>
|
||||||
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
|
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
|
||||||
<Product>Canary Islands Computer Museum Website</Product>
|
<Product>Canary Islands Computer Museum Website</Product>
|
||||||
|
|||||||
Reference in New Issue
Block a user