From 8ef09f9fed656bacc3a25e790796e9f144a5fac8 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Fri, 8 May 2026 01:28:31 +1000 Subject: v0.14a --- GetUser.cs | 26 ++++++++++++++++++++++++++ NewUser.cs | 26 ++++++++++++++++++++++++++ PowerShell.csproj | 2 +- RemoveUser.cs | 20 ++++++++++++++++++++ SetUser.cs | 38 ++++++++++++++++++++++++++++++++++++++ Util.cs | 17 +++++++++++++++++ 6 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 GetUser.cs create mode 100644 NewUser.cs create mode 100644 RemoveUser.cs create mode 100644 SetUser.cs create mode 100644 Util.cs 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 @@ $(AssemblyVersion) HyperBooru.PowerShell Jake Mannens - 0.13-alpha + 0.14-alpha 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); + } +} diff --git a/Util.cs b/Util.cs new file mode 100644 index 0000000..9f261ee --- /dev/null +++ b/Util.cs @@ -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); + } + } +} -- cgit v1.3