116 lines
3.1 KiB
C#
116 lines
3.1 KiB
C#
using API.Hashing;
|
|
using API.Hashing.Interfaces;
|
|
using DAL.Contexts;
|
|
using DAL.Models;
|
|
using DAL.Values;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Options;
|
|
using MySql.Data.MySqlClient;
|
|
using Mysqlx.Session;
|
|
using System.Configuration;
|
|
using System.Data.Common;
|
|
|
|
namespace Setup
|
|
{
|
|
internal class Program
|
|
{
|
|
public static bool lineIsYes(string? input)
|
|
{
|
|
if (input == null)
|
|
return false;
|
|
|
|
input = input.Trim().ToLower();
|
|
switch (input)
|
|
{
|
|
case "y":
|
|
case "yes":
|
|
return true;
|
|
case "n":
|
|
case "no":
|
|
return false;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static string getStringWithConfirmation(string message)
|
|
{
|
|
while (true)
|
|
{
|
|
Console.Write(message);
|
|
string? input = Console.ReadLine();
|
|
Console.WriteLine();
|
|
if (input == null)
|
|
continue;
|
|
|
|
Console.Write($"Is '{input}' correct? [Y/N]: ");
|
|
if (lineIsYes(Console.ReadLine()))
|
|
{
|
|
Console.WriteLine();
|
|
return input;
|
|
}
|
|
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
|
|
public static void Main(string[] args)
|
|
{
|
|
Console.Write("Entering setup. Only run this once. Will reinstate database. Continue? [Y/N]: ");
|
|
if (!lineIsYes(Console.ReadLine()))
|
|
System.Environment.Exit(0);
|
|
Console.WriteLine();
|
|
|
|
string connectionString = getStringWithConfirmation("Enter connection string: ");
|
|
|
|
DbContextOptionsBuilder<SASGContext> optionsBuilder = new DbContextOptionsBuilder<SASGContext>();
|
|
optionsBuilder.UseMySQL(connectionString);
|
|
|
|
SASGContext context = new SASGContext(optionsBuilder.Options);
|
|
|
|
string firstName = getStringWithConfirmation("Enter admin first name: ");
|
|
string lastname = getStringWithConfirmation("Enter admin last name: ");
|
|
PhoneNumber phoneNumber = getStringWithConfirmation("Enter admin phone number: ");
|
|
string unHashedPassword = getStringWithConfirmation("Enter admin password: ");
|
|
|
|
HashingType defaultHashingType = Enum.Parse<HashingType>(
|
|
getStringWithConfirmation($"Enter default hashing type [{String.Join(", ", Enum.GetNames(typeof(HashingType)))}]: "));
|
|
|
|
IHashingFactory hashingFactory = new HashingFactory();
|
|
IHashingAlgorithm algorithm = hashingFactory.getAlgorithm(defaultHashingType) ?? throw new InvalidOperationException();
|
|
|
|
byte[] salt;
|
|
string password = algorithm.hash(unHashedPassword, out salt);
|
|
|
|
Console.Write("About to touch db. Continue? [Y/N]: ");
|
|
if (!lineIsYes(Console.ReadLine()))
|
|
System.Environment.Exit(0);
|
|
|
|
MySqlConnection conn = (MySqlConnection) context.Database.GetDbConnection();
|
|
|
|
conn.Open();
|
|
using (MySqlCommand reader = new MySqlCommand(File.ReadAllText("Filler/Permissions.sql"), conn))
|
|
{
|
|
reader.ExecuteNonQuery();
|
|
}
|
|
|
|
using (MySqlCommand reader = new MySqlCommand(File.ReadAllText("Filler/Grants.sql"), conn))
|
|
{
|
|
reader.ExecuteNonQuery();
|
|
}
|
|
conn.Close();
|
|
|
|
context.users.Add(new User
|
|
{
|
|
firstName = firstName,
|
|
lastName = lastname,
|
|
phoneNumber = phoneNumber,
|
|
password = password,
|
|
salt = salt,
|
|
hashingType = defaultHashingType,
|
|
permissionId = 1,
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
}
|
|
} |