Files
marechai/Marechai.Database/Seeders/Users.cs

94 lines
3.5 KiB
C#
Raw Normal View History

2020-06-01 02:12:50 +01:00
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2025-11-14 05:08:14 +00:00
// Copyright © 2003-2026 Natalia Portillo
2020-06-01 02:12:50 +01:00
*******************************************************************************/
2020-05-23 19:53:54 +01:00
using System;
using System.Collections.Generic;
using Marechai.Database.Models;
using Microsoft.AspNetCore.Identity;
2025-11-13 04:05:35 +00:00
namespace Marechai.Database.Seeders;
public static class Users
2020-05-23 19:53:54 +01:00
{
2025-11-13 04:05:35 +00:00
public static void Seed(UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager)
2020-05-23 19:53:54 +01:00
{
2025-11-13 04:05:35 +00:00
ApplicationRole uberAdminRole = roleManager.FindByNameAsync("UberAdmin").Result;
if(uberAdminRole is null)
2020-05-23 19:53:54 +01:00
{
2025-11-13 04:05:35 +00:00
Console.WriteLine("Cannot find UberAdmin role, is database properly seeded?");
2020-05-23 19:53:54 +01:00
2025-11-13 04:05:35 +00:00
return;
}
2020-05-23 19:53:54 +01:00
2025-11-13 04:05:35 +00:00
IList<ApplicationUser> uberAdmins = userManager.GetUsersInRoleAsync("UberAdmin").Result;
2020-05-23 19:53:54 +01:00
2025-11-13 04:05:35 +00:00
if(uberAdmins.Count > 1)
{
Console.WriteLine("Too many uberadmins, only one can exist");
2020-05-23 19:53:54 +01:00
2025-11-13 04:05:35 +00:00
foreach(ApplicationUser user in uberAdmins)
2020-05-23 19:53:54 +01:00
{
2025-11-13 04:05:35 +00:00
Console.WriteLine("Removing uberadmin permissions from user {0}", user.UserName);
2020-05-23 19:53:54 +01:00
2025-11-13 04:05:35 +00:00
userManager.RemoveFromRoleAsync(user, "UberAdmin");
2020-05-23 19:53:54 +01:00
}
2025-11-13 04:05:35 +00:00
}
2020-05-23 19:53:54 +01:00
2025-11-13 04:05:35 +00:00
if(uberAdmins.Count == 1) return;
2020-05-23 19:53:54 +01:00
2025-11-13 04:05:35 +00:00
ApplicationUser uberAdmin = userManager.FindByEmailAsync("claunia@claunia.com").Result ??
userManager.FindByNameAsync("claunia").Result;
2020-05-23 19:53:54 +01:00
2025-11-13 04:05:35 +00:00
if(uberAdmin is null)
{
uberAdmin = new ApplicationUser
2020-05-23 19:53:54 +01:00
{
2025-11-13 04:05:35 +00:00
UserName = "claunia",
Email = "claunia@claunia.com",
EmailConfirmed = true
};
var 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);
2020-05-23 19:53:54 +01:00
}
2025-11-13 04:05:35 +00:00
else
Console.WriteLine("Could not create new uberadmin.");
2020-05-23 19:53:54 +01:00
2025-11-13 04:05:35 +00:00
return;
2020-05-23 19:53:54 +01:00
}
2025-11-13 04:05:35 +00:00
Console.WriteLine("Giving uberadmin permissions to user claunia");
userManager.AddToRoleAsync(uberAdmin, "UberAdmin").Wait();
2020-05-23 19:53:54 +01:00
}
}