2025-02-06 17:48:24 +01:00
|
|
|
using Autofac;
|
2025-02-10 18:41:51 +01:00
|
|
|
using Insight.Database;
|
|
|
|
|
using System.Data;
|
2025-02-05 18:38:29 +01:00
|
|
|
|
|
|
|
|
namespace SetupInfrastructure
|
2025-01-24 18:04:35 +01:00
|
|
|
{
|
2025-02-10 18:41:51 +01:00
|
|
|
/// <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 IContainer _container;
|
2025-02-12 17:38:01 +01:00
|
|
|
static ConsoleColor _backgroundColor = Console.BackgroundColor;
|
|
|
|
|
static ConsoleColor _foregroundColor = Console.ForegroundColor;
|
2025-02-06 23:46:55 +01:00
|
|
|
|
2025-02-10 18:41:51 +01:00
|
|
|
static async Task Main(string[] args)
|
|
|
|
|
{
|
2025-02-12 17:38:01 +01:00
|
|
|
Welcome();
|
2025-02-10 18:41:51 +01:00
|
|
|
string userPass;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
2025-02-11 17:07:01 +01:00
|
|
|
Console.WriteLine("Input username:password for a superadmin role");
|
2025-02-10 18:41:51 +01:00
|
|
|
userPass = Console.ReadLine() ?? string.Empty;
|
|
|
|
|
} while (!userPass.Contains(":") || userPass.Split(":").Length != 2 ||
|
|
|
|
|
string.IsNullOrEmpty(userPass.Split(":")[0]) ||
|
|
|
|
|
string.IsNullOrEmpty(userPass.Split(":")[1]));
|
|
|
|
|
|
|
|
|
|
var ctp = new Startup.ConnectionStringTemplateParameters(
|
|
|
|
|
user: userPass.Split(":")[0],
|
|
|
|
|
pwd: userPass.Split(":")[1]
|
|
|
|
|
);
|
|
|
|
|
_container = new Startup().ConfigureContainer(ctp);
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
if (IsSuperAdmin())
|
2025-02-10 18:41:51 +01:00
|
|
|
{
|
2025-02-12 17:38:01 +01:00
|
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
|
|
|
var setupApplicationUser = _container.Resolve<Database.Core.DCL.SetupApplicationUser>();
|
|
|
|
|
setupApplicationUser.With(new Database.Core.DCL.SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" });
|
|
|
|
|
Console.WriteLine("Database.Core.DCL.SetupApplicationUser done!");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
|
|
|
|
//create new container with application user, we use that role from now.
|
|
|
|
|
_container = new Startup().ConfigureContainer(new Startup.ConnectionStringTemplateParameters("sathumper", "3911"));
|
2025-02-10 18:41:51 +01:00
|
|
|
|
2025-02-12 17:38:01 +01:00
|
|
|
var setupIdentitySystem = _container.Resolve<Database.Core.DDL.SetupIdentitySystem>();
|
|
|
|
|
setupIdentitySystem.With(new Database.Core.DDL.SetupIdentitySystem.Command());
|
2025-02-10 18:41:51 +01:00
|
|
|
// SetupIdentitySystem
|
|
|
|
|
// SetupConfiguration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//and a lot of other tables that we haven't defined yet
|
|
|
|
|
|
|
|
|
|
// input configurations!!! TODO:Missing
|
|
|
|
|
|
2025-02-12 17:38:01 +01:00
|
|
|
Console.ForegroundColor = _foregroundColor;
|
2025-02-10 18:41:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
static bool IsSuperAdmin()
|
2025-02-10 18:41:51 +01:00
|
|
|
{
|
2025-02-12 17:38:01 +01:00
|
|
|
|
2025-02-10 18:41:51 +01:00
|
|
|
|
|
|
|
|
//test db access
|
|
|
|
|
Console.WriteLine("Testing db access...");
|
|
|
|
|
|
|
|
|
|
string query = @"SELECT usename, usesuper FROM pg_user WHERE usename = CURRENT_USER;";
|
|
|
|
|
|
|
|
|
|
var conn = _container.Resolve<IDbConnection>();
|
|
|
|
|
var result = (dynamic)conn.QuerySql(query).Single();
|
|
|
|
|
|
|
|
|
|
string username = result.usename;
|
|
|
|
|
bool isSuperuser = (bool)result.usesuper;
|
|
|
|
|
|
|
|
|
|
if ((bool)result.usesuper)
|
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
|
|
|
Console.BackgroundColor = ConsoleColor.Yellow;
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("TEST SUCCESSFULLY");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.White;
|
2025-02-12 17:38:01 +01:00
|
|
|
Console.BackgroundColor = _backgroundColor;
|
2025-02-10 18:41:51 +01:00
|
|
|
Console.WriteLine("-------------------------------");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine($"Username: {username}");
|
|
|
|
|
Console.WriteLine($"Super admin: true");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("-------------------------------");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Press any key to start database setup");
|
|
|
|
|
Console.Read();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
|
|
|
Console.BackgroundColor = ConsoleColor.Red;
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("TEST WAS NOT SUCCESSFULLY");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.White;
|
2025-02-12 17:38:01 +01:00
|
|
|
Console.BackgroundColor = _backgroundColor;
|
2025-02-10 18:41:51 +01:00
|
|
|
Console.WriteLine("-------------------------------");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine($"Username: {username}");
|
|
|
|
|
Console.WriteLine($"Super admin: false");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("-------------------------------");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("User is required to be super admin");
|
|
|
|
|
Console.Read();
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
2025-02-12 17:38:01 +01:00
|
|
|
}
|
|
|
|
|
static void Welcome()
|
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Blue;
|
|
|
|
|
Console.WriteLine(@"__________.__ ___________
|
|
|
|
|
\______ \ | _____ ___\__ ___/___ _____ ______ __ __ ______
|
|
|
|
|
| ___/ | \__ \ / \| |_/ __ \ / \\____ \| | \/ ___/
|
|
|
|
|
| | | |__/ __ \| | \ |\ ___/| Y Y \ |_> > | /\___ \
|
|
|
|
|
|____| |____(____ /___| /____| \___ >__|_| / __/|____//____ >
|
|
|
|
|
\/ \/ \/ \/|__| \/
|
|
|
|
|
_________ __
|
|
|
|
|
/ _____/ _____/ |_ __ ________
|
|
|
|
|
\_____ \_/ __ \ __\ | \____ \
|
|
|
|
|
/ \ ___/| | | | / |_> >
|
|
|
|
|
/_______ /\___ >__| |____/| __/
|
|
|
|
|
\/ \/ |__|");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.White;
|
|
|
|
|
|
2025-02-10 18:41:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-24 18:04:35 +01:00
|
|
|
}
|