Initial Commit
This commit is contained in:
44
Shared/Linux/GroupManager.cs
Normal file
44
Shared/Linux/GroupManager.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using CliWrap;
|
||||
using CliWrap.Buffered;
|
||||
|
||||
namespace Barcode.Host.Shared.Linux;
|
||||
|
||||
public class LinuxGroupManager : ILinuxGroupManager
|
||||
{
|
||||
public async Task<bool> IsInInputGroup()
|
||||
{
|
||||
BufferedCommandResult result = await Cli.Wrap("id")
|
||||
.ExecuteBufferedAsync();
|
||||
|
||||
string output = result.StandardOutput;
|
||||
|
||||
const StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries;
|
||||
bool inInputGroup = output.Split(new[] { ' ' }, options)
|
||||
.First(p => p.StartsWith("groups"))
|
||||
.Remove(0, "groups".Length)
|
||||
.Split(',', options)
|
||||
.Any(p => p.Contains("input"));
|
||||
|
||||
return inInputGroup;
|
||||
}
|
||||
|
||||
public async Task AddUserToInputGroup(string password)
|
||||
{
|
||||
using CancellationTokenSource cts = new();
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(10));
|
||||
|
||||
_ = await Cli.Wrap("bash")
|
||||
.WithArguments($"-c \"echo '{password}' | sudo -S gpasswd -a $USER input")
|
||||
.ExecuteBufferedAsync(cts.Token);
|
||||
}
|
||||
|
||||
public async Task RebootSystem(string password)
|
||||
{
|
||||
using CancellationTokenSource cts = new();
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(10));
|
||||
|
||||
_ = await Cli.Wrap("bash")
|
||||
.WithArguments($"-c \"echo '{password}' | sudo -S reboot\"")
|
||||
.ExecuteBufferedAsync(cts.Token);
|
||||
}
|
||||
}
|
10
Shared/Linux/IGroupManager.cs
Normal file
10
Shared/Linux/IGroupManager.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace Barcode.Host.Shared.Linux;
|
||||
|
||||
public interface ILinuxGroupManager
|
||||
{
|
||||
Task<bool> IsInInputGroup();
|
||||
|
||||
Task AddUserToInputGroup(string password);
|
||||
|
||||
Task RebootSystem(string password);
|
||||
}
|
Reference in New Issue
Block a user