internal/functions/validate/Validate-UserSet.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function Validate-UserSet
{
    <#
        .SYNOPSIS
            Validates the properties of a userSet complex type

        .PARAMETER reference
            The id, displayName, userPrincipalName or mailNickname of the referenced resource.
    #>

    [CmdletBinding()]
    Param (
        [Parameter(ParameterSetName = "Default")]
        [string] $reference,
        [ValidateSet("singleUser", "groupMembers", "connectedOrganizationMembers", "requestorManager", "internalSponsors", "externalSponsors")]
        [string] $type = "singleUser",
        [bool] $isBackup = $false,
        [Parameter(Mandatory = $true, ParameterSetName = "RequestorManager")]
        [int] $managerLevel,
        [System.Management.Automation.PSCmdlet]
        $Cmdlet = $PSCmdlet
    )
    
    begin
    {
        if ($managerLevel) {
            $type = "requestorManager"
        }
    }
    process
    {
        if (Test-PSFFunctionInterrupt) { return }                

        $userSetObject = [PSCustomObject]@{
            type = $type
            isBackup = $isBackup            
        }

        Add-Member -InputObject $userSetObject -MemberType NoteProperty -Name "@odata.type" -Value ("#microsoft.graph.{0}" -f $type)
        if ($type -in @("singleUser", "groupMembers", "connectedOrganizationMembers")) {
            Add-Member -InputObject $userSetObject -MemberType NoteProperty -Name reference -Value $reference
            Add-Member -InputObject $userSetObject -MemberType ScriptMethod -Name getId -Value {
                switch ($this.type) {
                    "singleUser" {
                        Resolve-User -InputReference $this.reference -Cmdlet $PSCmdlet
                    }
                    "groupMembers" {
                        Resolve-Group -InputReference $this.reference -Cmdlet $PSCmdlet
                    }
                    "connectedOrganizationMembers" {
                        Resolve-ConnectedOrganization -InputReference $this.reference -Cmdlet $PSCmdlet
                    }
                }
            }
            
        }
        if ($type -eq "requestorManager") {
            Add-Member -InputObject $userSetObject -MemberType NoteProperty -Name managerLevel -Value $managerLevel
        }

        Add-Member -InputObject $userSetObject -MemberType ScriptMethod -Name prepareBody -Value {
            $hashtable = @{                
                isBackup = $this.isBackup
                "@odata.type" = $this."@odata.type"
            }
            if ($this.getId) {$hashtable["id"] = $this.getId()}
            if ($this.managerLevel) {$hashtable["managerLevel"] = $this.managerLevel}
            return $hashtable
        }
    }
    end
    {
        $userSetObject
    }
}