This commit is contained in:
Janus Knudsen 2025-03-07 16:17:30 +01:00
parent ddb6abc14e
commit f3ab94eff1
6 changed files with 59 additions and 92 deletions

View file

@ -12,34 +12,33 @@ using System.Diagnostics;
namespace PlanTempus.SetupInfrastructure
{
/// <summary>
/// SETUP APPLICATION USER NAMED sathumper
///
/// This should be handled on the Postgresql db server with a superadmin or similar.
///
/// Execute SQL CreateRole.txt
///
/// After that is executed it is time for running this main program
/// Remember to use the newly created sathumper
/// "ConnectionStrings": {
/// "DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=<uid>;Password=<secret>;"
/// </summary>
internal class Program
/// <summary>
/// SETUP APPLICATION USER NAMED sathumper
///
/// This should be handled on the Postgresql db server with a superadmin or similar.
///
/// Execute SQL CreateRole.txt
///
/// After that is executed it is time for running this main program
/// Remember to use the newly created sathumper
/// "ConnectionStrings": {
/// "DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=[uid];Password=[secret];"
/// </summary>
internal class Program
{
static async Task Main(string[] args)
{
try
{
var host = Host.CreateDefaultBuilder(args)
.UseEnvironment("Dev")
.UseServiceProviderFactory(new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
.UseServiceProviderFactory(
new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
{
var startup = new Startup();
startup.ConfigureContainer(builder);
})
.Build();
await host.StartAsync();
@ -57,32 +56,24 @@ namespace PlanTempus.SetupInfrastructure
}
}
}
public class ConsoleService
{
public class ConsoleService(
IDbConnectionFactory connectionFactory,
SetupDbAdmin setupDbAdmin,
SetupIdentitySystem setupIdentitySystem,
SetupConfiguration setupConfiguration,
SetupApplicationUser setupApplicationUser)
{
static ConsoleColor _backgroundColor = Console.BackgroundColor;
static ConsoleColor _foregroundColor = Console.ForegroundColor;
private readonly IDbConnectionFactory _connectionFactory;
private readonly SetupDbAdmin _setupDbAdmin;
private readonly SetupIdentitySystem _setupIdentitySystem;
private readonly SetupConfiguration _setupConfiguration;
private readonly SetupApplicationUser _setupApplicationUser;
public ConsoleService(IDbConnectionFactory connectionFactory, SetupDbAdmin setupDbAdmin, SetupIdentitySystem setupIdentitySystem, SetupConfiguration setupConfiguration, SetupApplicationUser setupApplicationUser)
{
_connectionFactory = connectionFactory;
_setupDbAdmin = setupDbAdmin;
_setupIdentitySystem = setupIdentitySystem;
_setupConfiguration = setupConfiguration;
_setupApplicationUser = setupApplicationUser;
}
bool IsSuperAdmin(ConnectionStringParameters parameters)
{
Console.WriteLine("Testing db access...");
string query = @"SELECT usename, usesuper FROM pg_user WHERE usename = CURRENT_USER;";
using var conn = _connectionFactory.Create(parameters);
using var conn = connectionFactory.Create(parameters);
var result = (dynamic)conn.QuerySql(query).Single();
string username = result.usename;
@ -131,9 +122,8 @@ namespace PlanTempus.SetupInfrastructure
Console.Read();
return false;
}
static void Welcome()
{
Console.ForegroundColor = ConsoleColor.Blue;
@ -151,7 +141,6 @@ namespace PlanTempus.SetupInfrastructure
\/ \/ |__|");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.White;
}
public void Run()
@ -165,9 +154,9 @@ namespace PlanTempus.SetupInfrastructure
{
Console.WriteLine("Input username:password for a superadmin role");
userPass = Console.ReadLine() ?? string.Empty;
} while (!userPass.Contains(":") || userPass.Split(":").Length != 2 ||
string.IsNullOrEmpty(userPass.Split(":")[0]) ||
string.IsNullOrEmpty(userPass.Split(":")[1]));
} while (!userPass.Contains(':') || userPass.Split(":").Length != 2 ||
string.IsNullOrEmpty(userPass.Split(":")[0]) ||
string.IsNullOrEmpty(userPass.Split(":")[1]));
var superUser = new ConnectionStringParameters(
User: userPass.Split(":")[0],
@ -182,7 +171,9 @@ namespace PlanTempus.SetupInfrastructure
Console.Write("Database.Core.DCL.SetupDbAdmin...");
sw.Start();
_setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" }, superUser);
setupDbAdmin.With(
new SetupDbAdmin.Command
{ Password = "3911", Schema = "system", User = "heimdall" }, superUser);
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("::");
@ -193,14 +184,14 @@ namespace PlanTempus.SetupInfrastructure
//use application user, we use that role from now.
var connParams = new ConnectionStringParameters(User: "heimdall", Pwd: "3911");
_setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" }, connParams);
setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" }, connParams);
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("::");
Console.WriteLine("::");
Console.Write("Database.ConfigurationManagementSystem.SetupConfiguration...");
sw.Restart();
_setupConfiguration.With(new SetupConfiguration.Command(), connParams);
setupConfiguration.With(new SetupConfiguration.Command(), connParams);
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
@ -209,7 +200,9 @@ namespace PlanTempus.SetupInfrastructure
Console.Write("Database.Core.DCL.SetupApplicationUser...");
sw.Start();
_setupApplicationUser.With(new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" }, superUser);
setupApplicationUser.With(
new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" },
superUser);
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
@ -219,7 +212,6 @@ namespace PlanTempus.SetupInfrastructure
Console.ForegroundColor = _foregroundColor;
}
}
catch (Exception e)
@ -227,12 +219,7 @@ namespace PlanTempus.SetupInfrastructure
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine();
Console.WriteLine(e);
}
}
}
}
}