Public/Remove-JuribaDashboardTeamUserAccess.ps1

#requires -Version 7
function Remove-JuribaDashboardTeamUserAccess {
    <#
        .SYNOPSIS
        Revokes a user or team's access to a dashboard.
 
        .DESCRIPTION
        Uses ApiV1 to delete a previously-granted user or team access record
        from a dashboard. Pair with Set-JuribaDashboardTeamUserAccess (which
        adds grants) and Set-JuribaDashboardAccessType (which controls the
        dashboard's overall access mode).
 
        .PARAMETER Instance
        Optional. Juriba instance to be provided if not authenticating using Connect-Juriba. For example, https://myinstance.dpc.juriba.app
 
        .PARAMETER APIKey
        Optional. API key to be provided if not authenticating using Connect-Juriba.
 
        .PARAMETER DashboardId
        The id of the dashboard whose grant is being removed.
 
        .PARAMETER UserId
        The id of the user whose access should be revoked. Mutually exclusive with -TeamId.
 
        .PARAMETER TeamId
        The id of the team whose access should be revoked. Mutually exclusive with -UserId.
 
        .OUTPUTS
        The API response from the DELETE call.
 
        .EXAMPLE
        PS> Remove-JuribaDashboardTeamUserAccess -DashboardId 654 -UserId "f98fa56f-e271-47ff-a90e-31e2f02748b3"
 
        .EXAMPLE
        PS> Remove-JuribaDashboardTeamUserAccess -DashboardId 654 -TeamId 2836
    #>

    [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'User')]
    param(
        [Parameter(Mandatory = $false)]
        [string]$Instance,
        [Parameter(Mandatory = $false)]
        [string]$APIKey,
        [Parameter(Mandatory = $true)]
        [int]$DashboardId,
        [Parameter(Mandatory = $true, ParameterSetName = 'User')]
        [string]$UserId,
        [Parameter(Mandatory = $true, ParameterSetName = 'Team')]
        [int]$TeamId
    )
 
    if ((Get-Variable 'dwConnection' -Scope 'Global' -ErrorAction 'Ignore') -and !$APIKey -and !$Instance) {
        $APIKey = ConvertFrom-SecureString -SecureString $dwConnection.secureAPIKey -AsPlainText
        $Instance = $dwConnection.instance
    }
 
    if ($APIKey -and $Instance) {
        if ($PSCmdlet.ParameterSetName -eq 'User') {
            $target = "user"
            $targetId = $UserId
        }
        else {
            $target = "team"
            $targetId = $TeamId
        }
 
        $headers = @{ 'x-api-key' = $APIKey }
        $uri = "{0}/apiv1/dashboard/{1}/{2}/{3}" -f $Instance, $DashboardId, $target, $targetId
 
        try {
            if ($PSCmdlet.ShouldProcess("Dashboard $DashboardId", "Remove $target $targetId access")) {
                $result = Invoke-RestMethod -Uri $uri -Method DELETE -Headers $headers
                return $result
            }
        }
        catch {
            Write-Error $_
        }
    }
    else {
        Write-Error "No connection found. Please ensure `$APIKey and `$Instance is provided or connect using Connect-Juriba before proceeding."
    }
}