Public/Get-DunePermission.ps1

<#
.SYNOPSIS
Retrieve Dune permissions.
 
.DESCRIPTION
Gets one or more permissions from the authorization service. Supports filtering by config item, config item type, user or id. Returns `DunePermission` objects by default; use `-Raw` for raw API responses.
 
.PARAMETER ConfigItem
A `DuneConfigItem` derived object (e.g. `DuneDeployment`, `DuneResourceGroup`, `DuneResource`); returns the permissions granted on the supplied config item (pipeline input supported).
 
.PARAMETER Id
The GUID of a permission. Use the `Id` parameter set for a single permission.
 
.PARAMETER ConfigItemType
Filter permissions by the type of config item they are granted on (`Tenant`, `Collection`, `Deployment`, `ResourceGroup`, `Resource`).
 
.PARAMETER User
A `DuneUser` object; returns the permissions granted to the supplied user (pipeline input supported).
 
.PARAMETER Raw
If set, returns raw API objects instead of `DunePermission` objects.
 
.EXAMPLE
PS> Get-DunePermission
Returns all permissions of the current tenant.
 
.EXAMPLE
PS> Get-DuneDeployment -Name "webapp" | Get-DunePermission
Returns the permissions granted on the deployment `webapp`.
 
.EXAMPLE
PS> Get-DuneUser -Email "john.doe@example.com" | Get-DunePermission
Returns all permissions granted to the supplied user.
 
.EXAMPLE
PS> Get-DunePermission -ConfigItemType Deployment
Returns all permissions granted on deployments.
#>

function Get-DunePermission {
    [CmdletBinding(DefaultParameterSetName = 'Default')]
    param(
        [Parameter(ParameterSetName='ConfigItem',ValueFromPipeline)]
        [DuneConfigItem]$ConfigItem,

        [Parameter(ParameterSetName='Id')]
        [guid]$Id,

        [Parameter(ParameterSetName='ByConfigItemType')]
        [ValidateSet('Tenant','Collection','Deployment','ResourceGroup','Resource')]
        [string]$ConfigItemType,

        [Parameter(ParameterSetName='ByUser',ValueFromPipeline)]
        [DuneUser]$User,

        [Parameter()]
        [switch]$Raw
    )

    begin {
        Write-Debug "$($MyInvocation.MyCommand)|begin"
        $ReturnObjects = @()
        $ProcessedUrls = @()
        $BaseUri = 'authorization/permissions'
        $Method = 'GET'
    }

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process|$($PSCmdlet.ParameterSetName)"

        $Uri = switch ($PSCmdlet.ParameterSetName) {
            'Id' { "{0}/{1}" -f $BaseUri, $Id }
            'ConfigItem' { $BaseUri | Add-UriQueryParam "ConfigItemId=$($ConfigItem.Id)" }
            'ByConfigItemType' { $BaseUri | Add-UriQueryParam "ConfigItemType=$($ConfigItemType)" }
            'ByUser' { $BaseUri | Add-UriQueryParam "UserAuthId=$($User.Id)" }
            Default { $BaseUri }
        }

        if ($ProcessedUrls -notcontains $Uri) {
            $ResultItems = Invoke-DuneApiRequest -Uri $Uri -Method $Method -ExtractItems
            $ProcessedUrls += $Uri
            $ReturnObjects += $ResultItems | ForEach-Object {
                if ($Raw) { $_ } else { ConvertTo-DuneClassObject -Class DunePermission -InputObject $_ }
            }
        }
        else { Write-Debug "$($MyInvocation.MyCommand)|process|ApiCall Cache hit: $Uri" }
    }

    end { return $ReturnObjects }
}