From 694e4ea311375e8a4042503058bb3ceff3cf061a Mon Sep 17 00:00:00 2001 From: code liturgy Date: Wed, 16 Nov 2022 23:46:58 +0000 Subject: [PATCH] Add system extensions --- .../CodeLiturgy.Data.Auth.csproj | 8 --- CodeLiturgy.Data.Auth/appsettings.json | 22 -------- CodeLiturgy.Views/CodeLiturgy.Views.csproj | 2 + CodeLiturgy.Views/Program.cs | 2 +- CodeLiturgy.Views/System/SystemExtensions.cs | 56 +++++++++++++++++++ 5 files changed, 59 insertions(+), 31 deletions(-) delete mode 100644 CodeLiturgy.Data.Auth/appsettings.json create mode 100644 CodeLiturgy.Views/System/SystemExtensions.cs diff --git a/CodeLiturgy.Data.Auth/CodeLiturgy.Data.Auth.csproj b/CodeLiturgy.Data.Auth/CodeLiturgy.Data.Auth.csproj index 25ee9ef..aee1593 100644 --- a/CodeLiturgy.Data.Auth/CodeLiturgy.Data.Auth.csproj +++ b/CodeLiturgy.Data.Auth/CodeLiturgy.Data.Auth.csproj @@ -42,12 +42,4 @@ - - - true - PreserveNewest - PreserveNewest - - - diff --git a/CodeLiturgy.Data.Auth/appsettings.json b/CodeLiturgy.Data.Auth/appsettings.json deleted file mode 100644 index b0a056a..0000000 --- a/CodeLiturgy.Data.Auth/appsettings.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*", - "ConnectionStringDocker": { - "MySQL": "server=db;user=blueuser;password=dXjw127124dJ;database=bluedb;" - }, - "ConnectionStringNoDocker": { - "MySQL": "server=localhost;user=blueuser;password=dXjw127124dJ;database=bluedb;" - }, - "AuthSettings": { - "SecretKey": "iJWHDmHLpUA283sqsfhqGbMRdRj1PVkH" - }, - "JwtIssuerOptions": { - "Issuer": "SomeIssuer", - "Audience": "http://localhost:5000" - } -} diff --git a/CodeLiturgy.Views/CodeLiturgy.Views.csproj b/CodeLiturgy.Views/CodeLiturgy.Views.csproj index 5cdfc61..76d45a4 100644 --- a/CodeLiturgy.Views/CodeLiturgy.Views.csproj +++ b/CodeLiturgy.Views/CodeLiturgy.Views.csproj @@ -5,6 +5,7 @@ disable enable Linux + true @@ -14,6 +15,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/CodeLiturgy.Views/Program.cs b/CodeLiturgy.Views/Program.cs index ec29ad8..9407925 100644 --- a/CodeLiturgy.Views/Program.cs +++ b/CodeLiturgy.Views/Program.cs @@ -32,7 +32,7 @@ namespace BlueWest.WebApi private static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) - + .UseSystemd() .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); diff --git a/CodeLiturgy.Views/System/SystemExtensions.cs b/CodeLiturgy.Views/System/SystemExtensions.cs new file mode 100644 index 0000000..a3c5b37 --- /dev/null +++ b/CodeLiturgy.Views/System/SystemExtensions.cs @@ -0,0 +1,56 @@ +using System.Diagnostics; + +namespace CodeLiturgy.Views +{ + public static class SystemExtensions + { + public static void ExecuteCommand(string command) + { + Process proc = new(); + proc.StartInfo.FileName = "/bin/bash"; + proc.StartInfo.Arguments = "-c \" " + command + " \""; + proc.StartInfo.UseShellExecute = false; + proc.StartInfo.RedirectStandardOutput = true; + proc.Start (); + while (!proc.StandardOutput.EndOfStream) { + Console.WriteLine (proc.StandardOutput.ReadLine ()); + } + } + + public static void AddUser( + string username, + bool createHome, + bool useSudo) + { + var sudoStr = useSudo ? "sudo" : ""; + var createHomeStr = createHome ? "-m" : ""; + var addUser = $"{sudoStr} useradd {createHomeStr} {username}"; + var deletePass = $"{sudoStr} passwd -d {username}"; + var lockPass = $"{sudoStr} passwd -l {username} "; + + ExecuteCommand(addUser); + ExecuteCommand(deletePass); + ExecuteCommand(lockPass); + } + + + public static void DeleteUser( + string username, + bool deleteHome, + bool useSudo) + { + var sudo = useSudo ? "sudo" : ""; + var rmUser = $"{sudo} userdel {username}"; + ExecuteCommand(rmUser); + + if (deleteHome) + { + var delHome = $"{sudo} rm -rf /home/{username}"; + ExecuteCommand(delHome); + } + } + + + } +} +