Public/Remove-JiraFilterPermission.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
76
77
78
79
80
81
82
83
84
85
86
87
function Remove-JiraFilterPermission {
    # .ExternalHelp ..\JiraPS-help.xml
    [CmdletBinding( SupportsShouldProcess, DefaultParameterSetName = 'ByFilterId' )]
    param(
        [Parameter( Position = 0, Mandatory, ValueFromPipeline, ParameterSetName = 'ByFilterObject' )]
        [ValidateNotNullOrEmpty()]
        [ValidateScript(
            {
                if (@($Filter).Count -gt 1) {
                    $exception = ([System.ArgumentException]"Invalid Parameter")
                    $errorId = 'ParameterType.TooManyFilters'
                    $errorCategory = 'InvalidArgument'
                    $errorTarget = $_
                    $errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget
                    $errorItem.ErrorDetails = "Only one Filter can be passed at a time."
                    $PSCmdlet.ThrowTerminatingError($errorItem)
                }
                elseif (@($_.FilterPermissions).Count -lt 1) {
                    $exception = ([System.ArgumentException]"Invalid Type for Parameter")
                    $errorId = 'ParameterType.FilterWithoutPermission'
                    $errorCategory = 'InvalidArgument'
                    $errorTarget = $_
                    $errorItem = New-Object -TypeName System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $errorTarget
                    $errorItem.ErrorDetails = "The Filter provided does not contain any Permission to delete."
                    $PSCmdlet.ThrowTerminatingError($errorItem)
                }
                else {
                    return $true
                }
            }
        )]
        [PSTypeName('JiraPS.Filter')]
        $Filter,

        [Parameter( Position = 0, Mandatory, ParameterSetName = 'ByFilterId' )]
        [ValidateNotNullOrEmpty()]
        [Alias('Id')]
        [UInt32]
        $FilterId,

        # TODO: [Parameter( Position = 1, ParameterSetName = 'ByFilterObject')]
        [Parameter( Position = 1, Mandatory, ParameterSetName = 'ByFilterId')]
        [ValidateNotNullOrEmpty()]
        [UInt32[]]
        $PermissionId,

        [Parameter()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty
    )

    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    process {
        Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)"
        Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"

        switch ($PSCmdlet.ParameterSetName) {
            "ByFilterObject" {
                $PermissionId = $Filter.FilterPermissions.Id
            }
            "ByFilterId" {
                $Filter = Get-JiraFilter -Id $FilterId
            }
        }

        foreach ($_permissionId in $PermissionId) {
            $parameter = @{
                URI        = "{0}/permission/{1}" -f $Filter.RestURL, $_permissionId
                Method     = "DELETE"
                Credential = $Credential
            }
            Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter"
            if ($PSCmdlet.ShouldProcess($InputObject.Type, "Remove Permission")) {
                Invoke-JiraMethod @parameter
            }
        }
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }
}