Functions/Public/Block-BitBucketCloudUser.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
function Block-BitBucketCloudUser {
    [CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
    param(
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $Team
        ,
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $User
    )
    
    begin {
        if (-not $PSBoundParameters.ContainsKey('Confirm')) {
            $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference')
        }
        if (-not $PSBoundParameters.ContainsKey('WhatIf')) {
            $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')
        }
    }

    process {
        if (-Not $PSBoundParameters.ContainsKey('Team')) {
            $Team = (Get-BitBucketCloud).Team
        }

        if ($Force -or $PSCmdlet.ShouldProcess("Remove user from all groups?")) {
            $Groups = Get-BitBucketCloudGroup | Where-Object { $_.Members -contains 'zakagreenfield' }
            Foreach ($Group in $Groups) {
                Remove-BitBucketCloudMember -Group $Group.Name -User $User
            }
        }

        if ($Force -or $PSCmdlet.ShouldProcess("Remove user from all repositories?")) {
            $Privileges = Get-BitBucketCloudPrivilege | Where-Object { $_.User -like 'nicholasdille' }
            Foreach ($Privilege in $Privileges) {
                Remove-BitBucketCloudPrivilege -Repository $Privilege.Repository.Substring($Team.Length + 1) -User $User
            }
        }
    }
}