diff --git a/docs/commands/ipaddress-restrictions/Search-TssIpRestrictionUser.md b/docs/commands/ipaddress-restrictions/Search-TssIpRestrictionUser.md new file mode 100644 index 00000000..f9b85fa2 --- /dev/null +++ b/docs/commands/ipaddress-restrictions/Search-TssIpRestrictionUser.md @@ -0,0 +1,113 @@ +# Search-TssIpRestrictionUser + +## SYNOPSIS +Search IP Address restrictions assigned to users. + +## SYNTAX + +``` +Search-TssIpRestrictionUser [-TssSession] [-IpAddressRestrictionId ] [-UserId ] + [-SortBy ] [] +``` + +## DESCRIPTION +Search IP Address restrictions assigned to groups. +Search based on User ID or IP Address Restriction Id. + +## EXAMPLES + +### EXAMPLE 1 +``` +$session = New-TssSession -SecretServer https://alpha -Credential $ssCred +Search-TssIpRestrictionUser -TssSession $session -Id 5 +``` + +Return users assigned to IP Address restriction 5 + +### EXAMPLE 2 +``` +$session = New-TssSession -SecretServer https://alpha -Credential $ssCred +Search-TssIpRestrictionUser -TssSession $session -UserId 46 +``` + +Return IP Address restrictions assigned to User ID 46 + +## PARAMETERS + +### -TssSession +TssSession object created by New-TssSession for authentication + +```yaml +Type: Session +Parameter Sets: (All) +Aliases: + +Required: True +Position: 1 +Default value: None +Accept pipeline input: True (ByValue) +Accept wildcard characters: False +``` + +### -IpAddressRestrictionId +IP Address Restriction + +```yaml +Type: Int32 +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: 0 +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -UserId +User ID + +```yaml +Type: Int32 +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: 0 +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -SortBy +Sort by specific property, default IpAddressRestrictionId + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: IpAddressRestrictionId +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +## OUTPUTS + +### Thycotic.PowerShell.IpRestrictions.User +## NOTES +Requires TssSession object returned by New-TssSession + +## RELATED LINKS + +[https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/Search-TssIpRestrictionUser](https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/Search-TssIpRestrictionUser) + +[https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/Search-TssIpRestrictionUser.ps1](https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/Search-TssIpRestrictionUser.ps1) + diff --git a/src/Thycotic.SecretServer.Format.ps1xml b/src/Thycotic.SecretServer.Format.ps1xml index bf3255ba..ca8b0a25 100644 --- a/src/Thycotic.SecretServer.Format.ps1xml +++ b/src/Thycotic.SecretServer.Format.ps1xml @@ -3351,5 +3351,43 @@ + + + IpRestrictions.User + + Thycotic.PowerShell.IpRestrictions.User + + + + + + + + + + + + + + IpAddressRestrictionId + + + IpAddressRestrictionName + + + UserId + + + Username + + + UserDisplayName + + + + + + + diff --git a/src/Thycotic.SecretServer/classes/ipaddress-restrictions/User.cs b/src/Thycotic.SecretServer/classes/ipaddress-restrictions/User.cs new file mode 100644 index 00000000..0973205e --- /dev/null +++ b/src/Thycotic.SecretServer/classes/ipaddress-restrictions/User.cs @@ -0,0 +1,17 @@ +using System; +using System.Threading.Tasks; +using System.Management.Automation; +using System.Management.Automation.Runspaces; + +namespace Thycotic.PowerShell.IpRestrictions +{ + public class User + { + public int Id { get; set; } + public int IpAddressRestrictionId { get; set; } + public string IpAddressRestrictionName { get; set; } + public string UserDisplayName { get; set; } + public int UserId { get; set; } + public string Username { get; set; } + } +} \ No newline at end of file diff --git a/src/functions/ipaddress-restrictions/Search-TssIpRestrictionUser.ps1 b/src/functions/ipaddress-restrictions/Search-TssIpRestrictionUser.ps1 new file mode 100644 index 00000000..8e44cde7 --- /dev/null +++ b/src/functions/ipaddress-restrictions/Search-TssIpRestrictionUser.ps1 @@ -0,0 +1,88 @@ +function Search-TssIpRestrictionUser { + <# + .SYNOPSIS + Search IP Address restrictions assigned to users. + + .DESCRIPTION + Search IP Address restrictions assigned to groups. Search based on User ID or IP Address Restriction Id. + + .LINK + https://thycotic-ps.github.io/thycotic.secretserver/commands/ipaddress-restrictions/Search-TssIpRestrictionUser + + .LINK + https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/ipaddress-restrictions/Search-TssIpRestrictionUser.ps1 + + .EXAMPLE + $session = New-TssSession -SecretServer https://alpha -Credential $ssCred + Search-TssIpRestrictionUser -TssSession $session -Id 5 + + Return users assigned to IP Address restriction 5 + + .EXAMPLE + $session = New-TssSession -SecretServer https://alpha -Credential $ssCred + Search-TssIpRestrictionUser -TssSession $session -UserId 46 + + Return IP Address restrictions assigned to User ID 46 + + .NOTES + Requires TssSession object returned by New-TssSession + #> + [CmdletBinding()] + [OutputType('Thycotic.PowerShell.IpRestrictions.User')] + param ( + # TssSession object created by New-TssSession for authentication + [Parameter(Mandatory,ValueFromPipeline,Position = 0)] + [Thycotic.PowerShell.Authentication.Session] + $TssSession, + + # IP Address Restriction + [int] + $IpAddressRestrictionId, + + # User ID + [int] + $UserId, + + # Sort by specific property, default IpAddressRestrictionId + [string] + $SortBy = 'IpAddressRestrictionId' + ) + begin { + $tssParams = $PSBoundParameters + $invokeParams = . $GetInvokeApiParams $TssSession + } + process { + Get-TssInvocation $PSCmdlet.MyInvocation + if ($tssParams.ContainsKey('TssSession') -and $TssSession.IsValidSession()) { + Compare-TssVersion $TssSession '10.9.000064' $PSCmdlet.MyInvocation + if ($tssParams.ContainsKey('IpAddressRestrictionId')) { + $uri = $TssSession.ApiUrl, 'ipaddress-restrictions', $IpAddressRestrictionId, 'users' -join '/' + } + if ($tssParams.ContainsKey('UserId')) { + $uri = $TssSession.ApiUrl, 'users', $UserId, 'ipaddress-restrictions' -join '/' + } + $uri = $uri, "sortBy[0].direction=asc&sortBy[0].name=$SortBy&take=$($TssSession.Take)" -join '?' + $invokeParams.Method = 'GET' + $invokeParams.Uri = $uri + + Write-Verbose "Performing the operation $($invokeParams.Method) $($invokeParams.Uri)" + try { + $apiResponse = Invoke-TssApi @invokeParams + $restResponse = . $ProcessResponse $apiResponse + } catch { + Write-Warning "Issue on search request" + $err = $_ + . $ErrorHandling $err + } + + if ($restResponse.records.Count -le 0 -and $restResponse.records.Length -eq 0) { + Write-Warning "No records found" + } + if ($restResponse.records) { + [Thycotic.PowerShell.IpRestrictions.User[]]$restResponse.records + } + } else { + Write-Warning "No valid session found" + } + } +} \ No newline at end of file diff --git a/tests/ipaddress-restrictions/Search-TssIpRestrictionUser.Tests.ps1 b/tests/ipaddress-restrictions/Search-TssIpRestrictionUser.Tests.ps1 new file mode 100644 index 00000000..075f3e8e --- /dev/null +++ b/tests/ipaddress-restrictions/Search-TssIpRestrictionUser.Tests.ps1 @@ -0,0 +1,24 @@ +BeforeDiscovery { + $commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf +} +Describe "$commandName verify parameters" { + BeforeDiscovery { + [object[]]$knownParameters = 'TssSession', 'IpAddressRestrictionId', 'UserId', '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 Thycotic.PowerShell.IpRestrictions.User" -TestCases $commandDetails { + $_.OutputType.Name | Should -Be 'Thycotic.PowerShell.IpRestrictions.User' + } + } +} \ No newline at end of file