Skip to content

Commit 3982cad

Browse files
Add new cmdlet Get-TeamViewerRoleByUser
1 parent 672570a commit 3982cad

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function Get-TeamViewerRoleByUser {
2+
param(
3+
[Parameter(Mandatory = $true)]
4+
[securestring]
5+
$ApiToken,
6+
7+
[Parameter(Mandatory = $true)]
8+
[ValidateScript({ $_ | Resolve-TeamViewerUserId })]
9+
[Alias('UsersId')]
10+
[Alias('Id')]
11+
[string]
12+
$UserId
13+
)
14+
15+
begin {
16+
$copyUri = "$(Get-TeamViewerApiUri)/users/$userId/userroles"
17+
$parameters = $null
18+
$list = @()
19+
}
20+
process {
21+
$resourceUri = $copyUri
22+
do {
23+
$response = Invoke-TeamViewerRestMethod `
24+
-ApiToken $ApiToken `
25+
-Uri $resourceUri `
26+
-Method Get `
27+
-Body $parameters `
28+
-WriteErrorTo $PSCmdlet `
29+
-ErrorAction Stop
30+
31+
if ($response.assignedRoleIds -and $response.assignedRoleIds.Count -gt 0) {
32+
$list += $response.assignedRoleIds
33+
}
34+
if ($response.nextPaginationToken) {
35+
$resourceUri = $copyUri + '?paginationToken=' + $response.nextPaginationToken
36+
}
37+
} while ($response.nextPaginationToken)
38+
return $list
39+
}
40+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
external help file: TeamViewerPS-help.xml
3+
Module Name: TeamViewerPS
4+
online version: https://github.com/teamviewer/TeamViewerPS/blob/main/Docs/Help/Get-TeamViewerUserByRole.md
5+
schema: 2.0.0
6+
---
7+
8+
# Get-TeamViewerRoleByUser
9+
10+
## SYNOPSIS
11+
12+
Returns the assigned role ids of the user or null.
13+
14+
## SYNTAX
15+
16+
```powershell
17+
Get-TeamViewerRoleByUser [-ApiToken] <SecureString> [-UserId] <Object> [<CommonParameters>]
18+
```
19+
20+
## DESCRIPTION
21+
22+
Lists the assigned roles of a user in the TeamViewer company associated with the API access token. If no role is assigned or user id is unknown, null is returned.
23+
24+
## EXAMPLES
25+
26+
### Example 1
27+
28+
```powershell
29+
PS /> Get-TeamViewerRoleByUser -UserId "u123456777"
30+
```
31+
32+
Lists the assigned roles of the user with the ID u123456777.
33+
34+
## PARAMETERS
35+
36+
### -ApiToken
37+
38+
The TeamViewer API access token.
39+
40+
```yaml
41+
Type: SecureString
42+
Parameter Sets: (All)
43+
Aliases:
44+
45+
Required: True
46+
Position: 0
47+
Default value: None
48+
Accept pipeline input: False
49+
Accept wildcard characters: False
50+
```
51+
52+
### -UserId
53+
54+
User to list its assigned roles.
55+
56+
```yaml
57+
Type: Object
58+
Parameter Sets: (All)
59+
Aliases: UsersId
60+
61+
Required: True
62+
Position: 1
63+
Default value: None
64+
Accept pipeline input: False
65+
Accept wildcard characters: False
66+
```
67+
68+
### CommonParameters
69+
70+
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).
71+
72+
## INPUTS
73+
74+
### None
75+
76+
## OUTPUTS
77+
78+
### UUID[]
79+
80+
An array of `UUID` objects.
81+
82+
## NOTES
83+
84+
## RELATED LINKS

Docs/TeamViewerPS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ The following functions are available in this category:
7777

7878
[`Remove-TeamViewerUserTFA`](Help/Remove-TeamViewerUserTFA.md)
7979

80+
['Get-TeamViewerRoleByUser'](Help/Get-TeamViewerRoleByUser.md)
81+
8082
['Get-TeamViewerEffectivePermission'](Help/Get-TeamViewerEffectivePermission.md)
8183

8284
## User Groups
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
Describe 'Get-TeamViewerUserByRole' {
3+
Context 'When retrieving role assignments' {
4+
BeforeEach {
5+
. "$PSScriptRoot\..\..\Cmdlets\Public\Get-TeamViewerRoleByUser.ps1"
6+
7+
@(Get-ChildItem -Path "$PSScriptRoot\..\..\Cmdlets\Private\*.ps1") | `
8+
ForEach-Object { . $_.FullName }
9+
10+
Mock Get-TeamViewerApiUri { '//unit.test' }
11+
$responses = [System.Collections.Queue]::new()
12+
$responses.Enqueue([PSCustomObject]@{
13+
currentPaginationToken = $null
14+
nextPaginationToken = 'Test1'
15+
assignedRoleIds = @('f37001f9-bc3e-452e-9533-d81b0916be09')
16+
})
17+
$responses.Enqueue([PSCustomObject]@{
18+
currentPaginationToken = $null
19+
nextPaginationToken = $null
20+
assignedRoleIds = @('f47001f9-bc3e-452e-9533-d81b0916be09')
21+
})
22+
Mock Invoke-TeamViewerRestMethod -MockWith {
23+
if ($responses.Count -gt 0) {
24+
return $responses.Dequeue()
25+
}
26+
}
27+
$testApiToken = [securestring]@{}
28+
$null = $testApiToken
29+
$testUserId = 'u123456777'
30+
$null = $testUserId
31+
}
32+
It 'Should call the correct API endpoint' {
33+
Get-TeamViewerRoleByUser -ApiToken $testApiToken -UserId $testUserId
34+
35+
Assert-MockCalled Invoke-TeamViewerRestMethod -Times 1 -Scope It -ParameterFilter {
36+
$ApiToken -eq $testApiToken -And `
37+
$Uri -eq "//unit.test/users/$testUserId/userroles?paginationToken=Test1" -And `
38+
$Method -eq 'Get'
39+
}
40+
}
41+
It 'Should return assigned users' {
42+
$result = Get-TeamViewerRoleByUser -ApiToken $testApiToken -UserId $testUserId
43+
$result | Should -HaveCount 2
44+
}
45+
It 'Should return an empty list if no roles are assigned' {
46+
Mock Invoke-TeamViewerRestMethod -MockWith {
47+
[PSCustomObject]@{
48+
currentPaginationToken = $null
49+
nextPaginationToken = $null
50+
assignedRoleIds = @()
51+
}
52+
}
53+
$result = Get-TeamViewerRoleByUser -ApiToken $testApiToken -UserId $testUserId
54+
$result | Should -HaveCount 0
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)