using Autofac;
using Insight.Database;
using System.Data;
namespace SetupInfrastructure
{
///
/// 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=;Password=;"
///
internal class Program
{
static IContainer _container;
static async Task Main(string[] args)
{
string userPass;
try
{
do
{
Console.WriteLine("Input :");
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);
if (TestDbRole())
{
// SetupApplicationUser
// SetupIdentitySystem
// SetupConfiguration
//and a lot of other tables that we haven't defined yet
// input configurations!!! TODO:Missing
}
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e);
}
}
static bool TestDbRole()
{
var backgroundColor = Console.BackgroundColor;
var foregroundColor = Console.ForegroundColor;
//test db access
Console.WriteLine("Testing db access...");
string query = @"SELECT usename, usesuper FROM pg_user WHERE usename = CURRENT_USER;";
var conn = _container.Resolve();
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;
Console.BackgroundColor = backgroundColor;
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;
Console.BackgroundColor = backgroundColor;
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;
}
}
}