Skip to content

Commit

Permalink
Enable-TssUser - closes #156
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Apr 16, 2021
1 parent 51fedf8 commit b9fa4bd
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 0 deletions.
112 changes: 112 additions & 0 deletions docs/collections/_commands/Enable-TssUser.md
@@ -0,0 +1,112 @@
---
category: general
external help file: Thycotic.SecretServer-help.xml
Module Name: Thycotic.SecretServer
online version: https://thycotic-ps.github.io/thycotic.secretserver/commands/Enable-TssUser
schema: 2.0.0
title: Enable-TssUser
---

# Enable-TssUser

## SYNOPSIS
Enable a Secret Server User Account

## SYNTAX

```
Enable-TssUser [-TssSession] <TssSession> -Id <Int32[]> [-WhatIf] [-Confirm] [<CommonParameters>]
```

## DESCRIPTION
Enable a Secret Server User Account

## EXAMPLES

### EXAMPLE 1
```
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Enable-TssUser -TssSession $session -Id 28
```

Enable User 28

## PARAMETERS

### -TssSession
TssSession object created by New-TssSession for auth

```yaml
Type: TssSession
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```

### -Id
User Id

```yaml
Type: Int32[]
Parameter Sets: (All)
Aliases: UserId

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -WhatIf
Shows what would happen if the cmdlet runs.
The cmdlet is not run.

```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases: wi

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Confirm
Prompts you for confirmation before running the cmdlet.

```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases: cf

Required: False
Position: Named
Default value: None
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

## NOTES
Requires TssSession object returned by New-TssSession

## RELATED LINKS

[https://thycotic-ps.github.io/thycotic.secretserver/commands/Enable-TssUser](https://thycotic-ps.github.io/thycotic.secretserver/commands/Enable-TssUser)

[https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/users/Enable-User.ps1](https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/users/Enable-User.ps1)

78 changes: 78 additions & 0 deletions src/functions/users/Enable-User.ps1
@@ -0,0 +1,78 @@
function Enable-User {
<#
.SYNOPSIS
Enable a Secret Server User Account
.DESCRIPTION
Enable a Secret Server User Account
.EXAMPLE
$session = New-TssSession -SecretServer https://alpha -Credential $ssCred
Enable-TssUser -TssSession $session -Id 28
Enable User 28
.LINK
https://thycotic-ps.github.io/thycotic.secretserver/commands/Enable-TssUser
.LINK
https://github.com/thycotic-ps/thycotic.secretserver/blob/main/src/functions/users/Enable-User.ps1
.NOTES
Requires TssSession object returned by New-TssSession
#>
[CmdletBinding(SupportsShouldProcess)]
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
)
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) {
$uri = $TssSession.ApiUrl, 'users', $user -join '/'
$invokeParams.Uri = $uri
$invokeParams.Method = 'PATCH'

$userBody = @{
enabled = @{
dirty = $true
value = $true
}
}
$invokeParams.Body = $userBody | ConvertTo-Json
if ($PSCmdlet.ShouldProcess("SecretId: $user", "$($invokeParams.Method) $uri with:`n$($invokeParams.Body)`n")) {
Write-Verbose "$($invokeParams.Method) $uri with:`n$($invokeParams.Body)`n"
try {
$restResponse = . $InvokeApi @invokeParams
} catch {
Write-Warning "Issue enabling User [$user]"
$err = $_
. $ErrorHandling $err
}

if ($restResponse.enabled) {
Write-Verbose "User [$user] enabled"
} else {
Write-Warning "User [$user] has not been enabled"
}
}
}
} else {
Write-Warning "No valid session found"
}
}
}
58 changes: 58 additions & 0 deletions tests/users/Enable-User.Tests.ps1
@@ -0,0 +1,58 @@
BeforeDiscovery {
$commandName = Split-Path ($PSCommandPath.Replace('.Tests.ps1','')) -Leaf
. ([IO.Path]::Combine([string]$PSScriptRoot, '..', 'constants.ps1'))
}
Describe "$commandName verify parameters" {
BeforeDiscovery {
[object[]]$knownParameters = 'TssSession', 'Id'
[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
}
}
}
Describe "$commandName functions" {
Context "Checking" {
BeforeAll {
$session = [pscustomobject]@{
ApiVersion = 'api/v1'
Take = 2147483647
SecretServer = 'http://alpha/'
ApiUrl = 'http://alpha/api/v1'
AccessToken = 'AgJf5YLChrisPine312UcBrM1s1KB2BGZ5Ufc4qLZ'
RefreshToken = '9oacYeah0YqgBNg0L7VinDiesel6-Z9ITE51Humus'
TokenType = 'bearer'
ExpiresIn = 1199
}
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match '/version' } -MockWith {
return @{
model = [pscustomobject]@{
Version = '10.9.000033'
}
}
}

$userId = 42
Mock -Verifiable -CommandName Invoke-RestMethod -ParameterFilter { $Uri -match "/users/$userId" } -MockWith {
return [pscustomobject]@{
enabled = $true
}
}
$object = Enable-User -TssSession $session -Id $userId
Assert-VerifiableMock
}
It "Should be empty" {
$object | Should -BeNullOrEmpty
}
It "Should have called Invoke-RestMethod 2 times" {
Assert-MockCalled -CommandName Invoke-RestMethod -Times 2 -Scope Describe
}
}
}

0 comments on commit b9fa4bd

Please sign in to comment.