Modules/AzureDevOpsDsc.Common/Resources/Functions/Public/AzDoGitPermission/Remove-AzDoGitPermission.ps1

Function Remove-AzDoGitPermission
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$ProjectName,

        [Parameter(Mandatory = $false)]
        [string]$RepositoryName,

        [Parameter(Mandatory = $true)]
        [bool]$isInherited,

        [Parameter()]
        [HashTable[]]$Permissions,

        [Parameter()]
        [HashTable]$LookupResult,

        [Parameter()]
        [Ensure]$Ensure,

        [Parameter()]
        [System.Management.Automation.SwitchParameter]
        $Force
    )

    Write-Verbose "[Remove-AzDoGitPermission] Started."

    #
    # Test if the Repository is specified
    if ([String]::IsNullOrEmpty($RepositoryName))
    {
        Write-Warning "[Remove-AzDoGitPermission] Repository Name not specified. Defaulting to top-level Project permissions."
        Write-Warning "[Remove-AzDoGitPermission] STOPPING. It is not possible to remove permissions from a top-level Project."
        return
    }

    #
    # Security Namespace ID

    # Get the Security Namespace
    $SecurityNamespace  = Get-CacheItem -Key 'Git Repositories' -Type 'SecurityNamespaces'

    # If the Security Namespace is null, return
    if (-not $SecurityNamespace)
    {
        Write-Error "[Remove-AzDoGitPermission] Security Namespace not found."
        return
    }

    # Get the Project
    $Project = Get-CacheItem -Key $ProjectName -Type 'LiveProjects'

    # If the Project is null, return
    if (-not $Project)
    {
        Write-Error "[Remove-AzDoGitPermission] Project not found."
        return
    }

    # Get the Repository
    $Repository = Get-CacheItem -Key "$ProjectName\$RepositoryName" -Type 'LiveRepositories'

    # If the Repository is null, return
    if (-not $Repository)
    {
        Write-Error "[Remove-AzDoGitPermission] Repository not found."
        return
    }

    # Get the ACLs
    $DescriptorACLList  = Get-CacheItem -Key $SecurityNamespace.namespaceId -Type 'LiveACLList'

    # If the ACLs are null, return
    if (-not $DescriptorACLList)
    {
        Write-Error "[Remove-AzDoGitPermission] ACLs not found."
        return
    }

    #
    # Filter the ACLs that pertain to the Git Repository

    $searchString = 'repoV2/{0}/{1}' -f $Project.id, $Repository.id

    # Test if the Token exists
    $Filtered = $DescriptorACLList | Where-Object { $_.token -eq $searchString }

    # If the ACLs are not null, remove them
    if ($Filtered)
    {
        $params = @{
            OrganizationName = (Get-AzDoOrganizationName)
            SecurityNamespaceID = $SecurityNamespace.namespaceId
            TokenName = $searchString
        }

        # Remove the ACLs
        Remove-AzDoPermission @params

    }

}