Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Filter Permissions #289

Merged
merged 12 commits into from
Jun 21, 2018
32 changes: 20 additions & 12 deletions JiraPS/Private/ConvertTo-JiraFilter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,33 @@ function ConvertTo-JiraFilter {
param(
[Parameter( ValueFromPipeline )]
[PSObject[]]
$InputObject
$InputObject,

[PSObject[]]
$FilterPermissions
)

process {
foreach ($i in $InputObject) {
Write-Debug "[$($MyInvocation.MyCommand.Name)] Converting `$InputObject to custom object"

$props = @{
'ID' = $i.id
'Name' = $i.name
'JQL' = $i.jql
'RestUrl' = $i.self
'ViewUrl' = $i.viewUrl
'SearchUrl' = $i.searchUrl
'Favourite' = $i.favourite

'SharePermission' = $i.sharePermissions
'SharedUser' = $i.sharedUsers
'Subscription' = $i.subscriptions
'ID' = $i.id
'Name' = $i.name
'JQL' = $i.jql
'RestUrl' = $i.self
'ViewUrl' = $i.viewUrl
'SearchUrl' = $i.searchUrl
'Favourite' = $i.favourite
'FilterPermissions' = @()

'SharePermission' = $i.sharePermissions
'SharedUser' = $i.sharedUsers
'Subscription' = $i.subscriptions
}

if ($FilterPermissions) {
$props.FilterPermissions = @(ConvertTo-JiraFilterPermission ($FilterPermissions))
}

if ($i.description) {
Expand Down
39 changes: 39 additions & 0 deletions JiraPS/Private/ConvertTo-JiraFilterPermission.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function ConvertTo-JiraFilterPermission {
[CmdletBinding()]
param(
[Parameter( ValueFromPipeline )]
[PSObject[]]
$InputObject
)

process {
foreach ($i in $InputObject) {
Write-Debug "[$($MyInvocation.MyCommand.Name)] Converting `$InputObject to custom object"

$props = @{
'ID' = $i.id
'Type' = $i.type
'Group' = $null
'Project' = $null
'Role' = $null
}
if ($i.group) {
$props["Group"] = ConvertTo-JiraGroup $i.group
}
if ($i.project) {
$props["Project"] = ConvertTo-JiraProject $i.project
}
if ($i.role) {
$props["Role"] = ConvertTo-JiraProjectRole $i.role
}

$result = New-Object -TypeName PSObject -Property $props
$result.PSObject.TypeNames.Insert(0, 'JiraPS.FilterPermission')
$result | Add-Member -MemberType ScriptMethod -Name 'ToString' -Force -Value {
Write-Output "$($this.Type)"
}

Write-Output $result
}
}
}
30 changes: 30 additions & 0 deletions JiraPS/Private/ConvertTo-JiraProjectRole.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function ConvertTo-JiraProjectRole {
[CmdletBinding()]
param(
[Parameter( ValueFromPipeline )]
[PSObject[]]
$InputObject
)

process {
foreach ($i in $InputObject) {
Write-Debug "[$($MyInvocation.MyCommand.Name)] Converting `$InputObject to custom object"

$props = @{
'ID' = $i.id
'Name' = $i.name
'Description' = $i.description
'Actors' = $i.actors
'RestUrl' = $i.self
}

$result = New-Object -TypeName PSObject -Property $props
$result.PSObject.TypeNames.Insert(0, 'JiraPS.ProjectRole')
$result | Add-Member -MemberType ScriptMethod -Name "ToString" -Force -Value {
Write-Output "$($this.Name)"
}

Write-Output $result
}
}
}
35 changes: 35 additions & 0 deletions JiraPS/Private/Resolve-FullPath.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function Resolve-FullPath {
[CmdletBinding()]
param (
# Path to be resolved.
# Can be realtive or absolute.
# Resolves PSDrives
[Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )]
[ValidateNotNullOrEmpty()]
[ValidateScript(
{
if (-not (Test-Path $_ -PathType Leaf)) {
$errorItem = [System.Management.Automation.ErrorRecord]::new(
([System.ArgumentException]"File not found"),
'ParameterValue.FileNotFound',
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
$_
)
$errorItem.ErrorDetails = "No file could be found with the provided path '$_'."
$PSCmdlet.ThrowTerminatingError($errorItem)
}
else {
return $true
}
}
)]
[Alias( 'FullName', 'PSPath' )]
[String]
$Path
)

process {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Resolving path $Path"
$PSCmdlet.GetUnresolvedProviderPathFromPSPath($Path)
}
}
79 changes: 79 additions & 0 deletions JiraPS/Public/Add-JiraFilterPermission.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
function Add-JiraFilterPermission {
[CmdletBinding( SupportsShouldProcess, DefaultParameterSetName = 'ByInputObject' )]
# [OutputType( [JiraPS.FilterPermission] )]
param(
[Parameter( Position = 0, Mandatory, ValueFromPipeline, ParameterSetName = 'ByInputObject' )]
[ValidateNotNullOrEmpty()]
[PSTypeName('JiraPS.Filter')]
$Filter,

[Parameter( Position = 0, Mandatory, ValueFromPipeline, ParameterSetName = 'ById')]
[ValidateNotNullOrEmpty()]
[UInt32[]]
$Id,

[Parameter( Mandatory )]
[ValidateNotNullOrEmpty()]
[ValidateSet('Group', 'Project', 'ProjectRole', 'Authenticated', 'Global')]
[String]$Type,

[Parameter()]
[String]$Value,

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

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

$resourceURi = "{0}/permission"
}

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

if ($PSCmdlet.ParameterSetName -eq 'ById') {
$Filter = Get-JiraFilter -Id $Id
}

$body = @{
type = $Type.ToLower()
}
switch ($Type) {
"Group" {
$body["groupname"] = $Value
}
"Project" {
$body["projectId"] = $Value
}
"ProjectRole" {
$body["projectRoleId"] = $Value
}
"Authenticated" { }
"Global" { }
}

foreach ($_filter in $Filter) {
$parameter = @{
URI = $resourceURi -f $_filter.RestURL
Method = "POST"
Body = ConvertTo-Json $body
Credential = $Credential
}
Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter"
if ($PSCmdlet.ShouldProcess($_filter.Name, "Add Permission [$Type - $Value]")) {
$result = Invoke-JiraMethod @parameter

Write-Output (ConvertTo-JiraFilter -InputObject $_filter -FilterPermissions $result)
}
}
}

end {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
}
}
51 changes: 51 additions & 0 deletions JiraPS/Public/Get-JiraFilterPermission.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function Get-JiraFilterPermission {
[CmdletBinding( DefaultParameterSetName = 'ById' )]
# [OutputType( [JiraPS.FilterPermission] )]
param(
[Parameter( Position = 0, Mandatory, ValueFromPipeline, ParameterSetName = 'ByInputObject' )]
[ValidateNotNullOrEmpty()]
[PSTypeName('JiraPS.Filter')]
$Filter,

[Parameter( Position = 0, Mandatory, ValueFromPipeline, ParameterSetName = 'ById')]
[ValidateNotNullOrEmpty()]
[UInt32[]]
$Id,

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

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

$resourceURi = "{0}/permission"
}

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

if ($PSCmdlet.ParameterSetName -eq 'ById') {
$Filter = Get-JiraFilter -Id $Id
}

foreach ($_filter in $Filter) {
$parameter = @{
URI = $resourceURi -f $_filter.RestURL
Method = "GET"
Credential = $Credential
}
Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter"
$result = Invoke-JiraMethod @parameter

Write-Output (ConvertTo-JiraFilter -InputObject $_filter -FilterPermissions $result)
}
}

end {
Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
}
}
1 change: 1 addition & 0 deletions JiraPS/Public/Remove-JiraFilter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function Remove-JiraFilter {
$InputObject,

[Parameter( Position = 0, Mandatory, ValueFromPipeline, ParameterSetName = 'ById')]
[ValidateNotNullOrEmpty()]
[UInt32[]]
$Id,

Expand Down
85 changes: 85 additions & 0 deletions JiraPS/Public/Remove-JiraFilterPermission.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
function Remove-JiraFilterPermission {
[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"
}
}
Loading