CodeLiturgy.Dashboard/CodeLiturgy.Views/System/SystemExtensions.cs

57 lines
1.6 KiB
C#

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);
}
}
}
}