Create admin user on first startup

This commit is contained in:
jvyden 2022-06-01 17:06:27 -04:00
commit 27d92001c3
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278

View file

@ -9,6 +9,7 @@ using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Logging.Loggers;
using LBPUnion.ProjectLighthouse.Match.Rooms;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using LBPUnion.ProjectLighthouse.Startup;
using LBPUnion.ProjectLighthouse.StorableLists;
using LBPUnion.ProjectLighthouse.Types;
@ -91,6 +92,22 @@ public static class StartupTasks
RoomHelper.StartCleanupThread();
}
// Create admin user if no users exist
if (serverType == ServerType.Website && database.Users.CountAsync().Result == 0)
{
const string passwordClear = "lighthouse";
string password = CryptoHelper.BCryptHash(CryptoHelper.Sha256Hash(passwordClear));
User admin = database.CreateUser("admin", password).Result;
admin.IsAdmin = true;
admin.PasswordResetRequired = true;
database.SaveChanges();
Logger.Success("No users were found, so an admin user was created. " +
$"The username is 'admin' and the password is '{passwordClear}'.", LogArea.Startup);
}
stopwatch.Stop();
Logger.Success($"Ready! Startup took {stopwatch.ElapsedMilliseconds}ms. Passing off control to ASP.NET...", LogArea.Startup);
}