diff options
| -rw-r--r-- | GetUser.cs | 26 | ||||
| -rw-r--r-- | NewUser.cs | 26 | ||||
| -rw-r--r-- | PowerShell.csproj | 2 | ||||
| -rw-r--r-- | RemoveUser.cs | 20 | ||||
| -rw-r--r-- | SetUser.cs | 38 | ||||
| -rw-r--r-- | Util.cs | 17 |
6 files changed, 128 insertions, 1 deletions
diff --git a/GetUser.cs b/GetUser.cs new file mode 100644 index 0000000..63c5af7 --- /dev/null +++ b/GetUser.cs @@ -0,0 +1,26 @@ +using HyperBooru.ApiClient; +using System.Management.Automation; + +namespace HyperBooru.PowerShell; + +[Alias("ghbu")] +[Cmdlet(VerbsCommon.Get, "HyperBooruUser")] +public class GetUserCmdlet : PSCmdlet { + [Parameter(Position = 0, Mandatory = true)] + public HBSession Session { get; set; } + + [Parameter(Position = 1)] + public Guid? UserId { get; set; } + + protected override void ProcessRecord() { + ApiModels.User[] users; + + if(UserId is not null) + users = [ Session.User.GetUserAsync((Guid) UserId).GetAwaiter().GetResult() ]; + else + users = Session.User.GetAllUsersAsync().GetAwaiter().GetResult(); + + foreach(var u in users) + WriteObject(u); + } +} diff --git a/NewUser.cs b/NewUser.cs new file mode 100644 index 0000000..7b9ab07 --- /dev/null +++ b/NewUser.cs @@ -0,0 +1,26 @@ +using HyperBooru.ApiClient; +using System.Management.Automation; +using System.Security; + +namespace HyperBooru.PowerShell; + +[Alias("nhbu")] +[Cmdlet(VerbsCommon.New, "HyperBooruUser")] +public class NewUserCmdlet : PSCmdlet { + [Parameter(Position = 0, Mandatory = true)] + public HBSession Session { get; set; } + + [Parameter(Position = 1, Mandatory = true)] + public string Username { get; set; } + + [Parameter(Position = 2, Mandatory = true)] + public SecureString Password { get; set; } + + protected override void ProcessRecord() { + var user = Session.User.CreateUserAsync(Username, Password.ToInsecureString()) + .GetAwaiter() + .GetResult(); + + WriteObject(user); + } +} diff --git a/PowerShell.csproj b/PowerShell.csproj index 188674f..ba0a927 100644 --- a/PowerShell.csproj +++ b/PowerShell.csproj @@ -9,7 +9,7 @@ <FileVersion>$(AssemblyVersion)</FileVersion>
<AssemblyTitle>HyperBooru.PowerShell</AssemblyTitle>
<Authors>Jake Mannens</Authors>
- <Version>0.13-alpha</Version>
+ <Version>0.14-alpha</Version>
</PropertyGroup>
<ItemGroup>
diff --git a/RemoveUser.cs b/RemoveUser.cs new file mode 100644 index 0000000..0e28675 --- /dev/null +++ b/RemoveUser.cs @@ -0,0 +1,20 @@ +using HyperBooru.ApiClient; +using System.Management.Automation; + +namespace HyperBooru.PowerShell; + +[Alias("rhbu")] +[Cmdlet(VerbsCommon.Remove, "HyperBooruUser")] +public class RemoveUserCmdlet : PSCmdlet { + [Parameter(Position = 0, Mandatory = true)] + public HBSession Session { get; set; } + + [Parameter(Position = 1, Mandatory = true)] + public Guid UserId { get; set; } + + protected override void ProcessRecord() { + Session.User.DeleteUserAsync(UserId) + .GetAwaiter() + .GetResult(); + } +} diff --git a/SetUser.cs b/SetUser.cs new file mode 100644 index 0000000..93af581 --- /dev/null +++ b/SetUser.cs @@ -0,0 +1,38 @@ +using HyperBooru.ApiClient; +using System.Management.Automation; +using System.Security; + +namespace HyperBooru.PowerShell; + +[Alias("shbu")] +[Cmdlet(VerbsCommon.Set, "HyperBooruUser")] +public class SetUserCmdlet : PSCmdlet { + [Parameter(Position = 0, Mandatory = true)] + public HBSession Session { get; set; } + + [Parameter(Position = 1, Mandatory = true)] + public Guid UserId { get; set; } + + [Parameter(Position = 2)] + public string? Username { get; set; } + + [Parameter(Position = 3)] + public SecureString? Password { get; set; } + + protected override void ProcessRecord() { + if(Username is null && Password is null) { + ThrowTerminatingError(new( + new ArgumentException("Both Username and Password cannot be null"), + "ArgumentException", + ErrorCategory.InvalidArgument, + null)); + return; + } + + var user = Session.User.UpdateUserAsync(UserId, Username, Password?.ToInsecureString()) + .GetAwaiter() + .GetResult(); + + WriteObject(user); + } +} @@ -0,0 +1,17 @@ +using System.Runtime.InteropServices; +using System.Security; + +namespace HyperBooru.PowerShell; + +internal static class Util { + internal static string ToInsecureString(this SecureString x) { + IntPtr rawString = IntPtr.Zero; + try { + rawString = Marshal.SecureStringToBSTR(x); + return Marshal.PtrToStringBSTR(rawString); + } finally { + if(rawString != IntPtr.Zero) + Marshal.FreeBSTR(rawString); + } + } +} |
