From ef96e219508620053dbc67a1c6f506210ade50ef Mon Sep 17 00:00:00 2001 From: Chris Peterson Date: Fri, 19 Jan 2024 16:10:14 -0800 Subject: [PATCH] Add some cmdlets --- src/GitlabCli/GitlabCli.psd1 | 14 +++++++-- src/GitlabCli/Members.psm1 | 51 ++++++++++++++++++++++++++++++- src/GitlabCli/Users.psm1 | 58 ++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 4 deletions(-) diff --git a/src/GitlabCli/GitlabCli.psd1 b/src/GitlabCli/GitlabCli.psd1 index c3f33ce..d21f118 100644 --- a/src/GitlabCli/GitlabCli.psd1 +++ b/src/GitlabCli/GitlabCli.psd1 @@ -1,11 +1,16 @@ @{ - ModuleVersion = '1.107.0' + ModuleVersion = '1.108.0' PrivateData = @{ PSData = @{ LicenseUri = 'https://github.com/chris-peterson/pwsh-gitlab/blob/main/LICENSE' ProjectUri = 'https://github.com/chris-peterson/pwsh-gitlab' - ReleaseNotes = 'Get more resources by Url' + ReleaseNotes = +@' +New CmdLets: + * Remove-GitlabUserMembership (https://github.com/chris-peterson/pwsh-gitlab/issues/87) + * Block-GitlabUser, Unblock-GitlabUser +'@ } } @@ -13,7 +18,7 @@ Author = 'Chris Peterson' CompanyName = 'Chris Peterson' - Copyright = '(c) 2021-2023' + Copyright = '(c) 2021-2024' Description = 'Interact with GitLab via PowerShell' PowerShellVersion = '7.1' @@ -215,6 +220,8 @@ # User 'Get-GitlabUser' 'Get-GitlabCurrentUser' + 'Block-GitlabUser' + 'Unblock-GitlabUser' # Events 'Get-GitlabUserEvent' @@ -241,6 +248,7 @@ 'Get-GitlabUserMembership' 'Add-GitlabUserMembership' 'Update-GitlabUserMembership' + 'Remove-GitlabUserMembership' # Utilities 'ConvertTo-PascalCase' diff --git a/src/GitlabCli/Members.psm1 b/src/GitlabCli/Members.psm1 index f7425f2..da3f0ae 100644 --- a/src/GitlabCli/Members.psm1 +++ b/src/GitlabCli/Members.psm1 @@ -127,7 +127,7 @@ function Add-GitlabGroupMember { } function Remove-GitlabGroupMember { - [CmdletBinding(SupportsShouldProcess)] + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Medium')] param ( [Parameter(Mandatory, ValueFromPipelineByPropertyName)] [string] @@ -304,6 +304,55 @@ function Get-GitlabUserMembership { New-WrapperObject 'Gitlab.UserMembership' } +function Remove-GitlabUserMembership { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')] + param( + [Parameter(Mandatory, Position=0, ValueFromPipelineByPropertyName)] + [string] + $Username, + + [Parameter()] + $Group, + + [Parameter()] + $Project, + + [Parameter()] + [switch] + $RemoveAllAccess, + + [Parameter] + [string] + $SiteUrl + ) + + $User = Get-GitlabUser -Username $Username -SiteUrl $SiteUrl + + if ($Group) { + if ($PSCmdlet.ShouldProcess("$($Group -join ',' )", "remove $Username access from groups")) { + $Group | ForEach-Object { + $User | Remove-GitlabGroupMember -GroupId $_ -SiteUrl $SiteUrl + } + } + } + if ($Project) { + if ($PSCmdlet.ShouldProcess("$($Project -join ',' )", "remove $Username access from project ")) { + $Project | ForEach-Object { + $User | Remove-GitlabProjectMember -ProjectId $_ -SiteUrl $SiteUrl + } + } + } + if ($RemoveAllAccess) { + $CurrentAccess = $User | Get-GitlabUserMembership + $Request = @{ + Group = $CurrentAccess | Where-Object Sourcetype -eq 'Namespace' | Select-Object -ExpandProperty SourceId + Project = $CurrentAccess | Where-Object Sourcetype -eq 'Project' | Select-Object -ExpandProperty SourceId + SiteUrl = $SiteUrl + } + $User | Remove-GitlabUserMembership @Request + } +} + # https://docs.gitlab.com/ee/api/members.html#add-a-member-to-a-group-or-project function Add-GitlabUserMembership { [CmdletBinding()] diff --git a/src/GitlabCli/Users.psm1 b/src/GitlabCli/Users.psm1 index e321b9e..eaef6b3 100644 --- a/src/GitlabCli/Users.psm1 +++ b/src/GitlabCli/Users.psm1 @@ -39,6 +39,64 @@ function Get-GitlabUser { Invoke-GitlabApi @Parameters | New-WrapperObject 'Gitlab.User' } +function Block-GitlabUser { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')] + param ( + [Parameter(Position=0, Mandatory, ValueFromPipelineByPropertyName)] + [Alias('Id')] + [Alias('Username')] + [Alias('EmailAddress')] + [string] + $UserId, + + [Parameter()] + [string] + $SiteUrl + ) + $User = Get-GitlabUser $UserId + + if ($PSCmdlet.ShouldProcess("$($User.Username)", "block user")) { + $Parameters = @{ + # https://docs.gitlab.com/ee/api/users.html#block-user + Method = 'POST' + Path = "users/$($User.Id)/block" + SiteUrl = $SiteUrl + } + if (Invoke-GitlabApi @Parameters) { + Write-Host "$($User.Username) blocked" + } + } +} + +function Unblock-GitlabUser { + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')] + param ( + [Parameter(Position=0, Mandatory, ValueFromPipelineByPropertyName)] + [Alias('Id')] + [Alias('Username')] + [Alias('EmailAddress')] + [string] + $UserId, + + [Parameter()] + [string] + $SiteUrl + ) + $User = Get-GitlabUser $UserId + + if ($PSCmdlet.ShouldProcess("$($User.Username)", "unblock user")) { + $Parameters = @{ + # https://docs.gitlab.com/ee/api/users.html#unblock-user + Method = 'POST' + Path = "users/$($User.Id)/unblock" + SiteUrl = $SiteUrl + } + if (Invoke-GitlabApi @Parameters) { + Write-Host "$($User.Username) unblocked" + } + } +} + # https://docs.gitlab.com/ee/api/events.html#get-user-contribution-events function Get-GitlabUserEvent { [CmdletBinding(DefaultParameterSetName='ByUserId')]