From 083c49889bd08edd506299b2b7ee23fb3b5bd3bc Mon Sep 17 00:00:00 2001 From: Shawn Melton <11204251+wsmelton@users.noreply.github.com> Date: Fri, 28 May 2021 20:27:05 -0500 Subject: [PATCH] Get-UserGroup - closes #96 --- .../_abouttopics/about_tssgroupusersummary.md | 3 +- src/en-us/about_tssgroupusersummary.help.txt | 3 +- src/functions/users/Get-UserGroup.ps1 | 74 +++++++++++++++++++ tests/users/Get-UserGroup.Tests.ps1 | 24 ++++++ 4 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/functions/users/Get-UserGroup.ps1 create mode 100644 tests/users/Get-UserGroup.Tests.ps1 diff --git a/docs/collections/_abouttopics/about_tssgroupusersummary.md b/docs/collections/_abouttopics/about_tssgroupusersummary.md index bb2023a4..3b5b3ee4 100644 --- a/docs/collections/_abouttopics/about_tssgroupusersummary.md +++ b/docs/collections/_abouttopics/about_tssgroupusersummary.md @@ -53,4 +53,5 @@ last_modified_at: 2021-04-25T00:00:00-00:00 # METHODS # RELATED LINKS: - Get-TssGroupMember \ No newline at end of file + Get-TssGroupMember + Get-TssUserGroup \ No newline at end of file diff --git a/src/en-us/about_tssgroupusersummary.help.txt b/src/en-us/about_tssgroupusersummary.help.txt index a498b123..20ea784e 100644 --- a/src/en-us/about_tssgroupusersummary.help.txt +++ b/src/en-us/about_tssgroupusersummary.help.txt @@ -47,4 +47,5 @@ PROPERTIES METHODS RELATED LINKS: - Get-TssGroupMember \ No newline at end of file + Get-TssGroupMember + Get-TssUserGroup \ No newline at end of file diff --git a/src/functions/users/Get-UserGroup.ps1 b/src/functions/users/Get-UserGroup.ps1 new file mode 100644 index 00000000..29de780d --- /dev/null +++ b/src/functions/users/Get-UserGroup.ps1 @@ -0,0 +1,74 @@ +function Get-UserGroup { + <# + .SYNOPSIS + Get the groups of a user by ID + + .DESCRIPTION + Get the groups of a user by ID + + .EXAMPLE + $session = New-TssSession -SecretServer https://alpha -Credential $ssCred + Get-TssUserGroup -TssSession $session -Id 42 + + Get group the User ID 42 is a member of + + .LINK + https://thycotic-ps.github.io/thycotic.secretserver/commands/Get-TssUserGroup + + .LINK + https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions//Get-UserGroup.ps1 + + .NOTES + Requires TssSession object returned by New-TssSession + #> + [CmdletBinding()] + [OutputType('TssUserGroup')] + param ( + # TssSession object created by New-TssSession for auth + [Parameter(Mandatory, ValueFromPipeline, Position = 0)] + [TssSession] + $TssSession, + + # User ID + [Parameter(Mandatory, ValueFromPipelineByPropertyName)] + [Alias('UserId')] + [int[]] + $Id, + + # Sort by specific property, default GroupId + [string] + $SortBy = 'GroupId' + ) + begin { + $tssParams = $PSBoundParameters + $invokeParams = . $GetInvokeTssParams $TssSession + } + process { + Write-Verbose "Provided command parameters: $(. $GetInvocation $PSCmdlet.MyInvocation)" + if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) { + . $CheckVersion $TssSession '10.9.000000' $PSCmdlet.MyInvocation + foreach ($user in $Id) { + $restResponse = $null + $uri = $TssSession.ApiUrl, 'users', $user, 'groups' -join '/' + $uri = $uri, "sortBy[0].direction=asc&sortBy[0].name=$SortBy&take=$($TssSession.Take)" -join '?' + $invokeParams.Uri = $uri + $invokeParams.Method = 'GET' + + Write-Verbose "Performing the operation $($invokeParams.Method) $uri" + try { + $restResponse = . $InvokeApi @invokeParams + } catch { + Write-Warning "Issue getting group(s) on user [$user]" + $err = $_ + . $ErrorHandling $err + } + + if ($restResponse.records) { + [TssGroupUserSummary[]]$restREsponse.records + } + } + } else { + Write-Warning 'No valid session found' + } + } +} \ No newline at end of file diff --git a/tests/users/Get-UserGroup.Tests.ps1 b/tests/users/Get-UserGroup.Tests.ps1 new file mode 100644 index 00000000..e8e1df0c --- /dev/null +++ b/tests/users/Get-UserGroup.Tests.ps1 @@ -0,0 +1,24 @@ +BeforeDiscovery { + $commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf +} +Describe "$commandName verify parameters" { + BeforeDiscovery { + [object[]]$knownParameters = 'TssSession', 'Id', 'SortBy' + [object[]]$currentParams = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function')).Parameters.Keys + [object[]]$commandDetails = [System.Management.Automation.CommandInfo]$ExecutionContext.SessionState.InvokeCommand.GetCommand($commandName,'Function') + $unknownParameters = Compare-Object -ReferenceObject $knownParameters -DifferenceObject $currentParams -PassThru + } + Context "Verify parameters" -Foreach @{currentParams = $currentParams } { + It "$commandName should contain <_> parameter" -TestCases $knownParameters { + $_ -in $currentParams | Should -Be $true + } + It "$commandName should not contain parameter: <_>" -TestCases $unknownParameters { + $_ | Should -BeNullOrEmpty + } + } + Context "Command specific details" { + It "$commandName should set OutputType to TssUserGroup" -TestCases $commandDetails { + $_.OutputType.Name | Should -Be 'TssUserGroup' + } + } +} \ No newline at end of file