Skip to content

Commit

Permalink
Merge pull request #15 from Readify/group-memberships
Browse files Browse the repository at this point in the history
Adds role assertions and route tests to group membership functions
  • Loading branch information
RobFaie authored Feb 19, 2020
2 parents 89f0a5f + 7b5cb7e commit 65f2cb0
Show file tree
Hide file tree
Showing 5 changed files with 524 additions and 11 deletions.
10 changes: 9 additions & 1 deletion functions/Get-GroupMembership.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function Get-GroupMembership {
# Unique Id of the group membership to retrieve
[Parameter(Mandatory = $true,
ParameterSetName = 'Id')]
[Parameter(Mandatory = $false,
ParameterSetName = 'UserId')]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$Id,
Expand Down Expand Up @@ -51,6 +53,8 @@ function Get-GroupMembership {
$Context = $null
)

Assert-IsAgent -Context $Context

$key = 'group_memberships'

switch ($PSCMDlet.ParameterSetName) {
Expand All @@ -60,7 +64,11 @@ function Get-GroupMembership {
}

'UserId' {
$path = "/api/v2/users/$UserId/group_memberships.json"
if ($PSBoundParameters.ContainsKey('Id')) {
$path = "/api/v2/users/$UserId/group_memberships/$Id.json"
} else {
$path = "/api/v2/users/$UserId/group_memberships.json"
}
}

'GroupId' {
Expand Down
43 changes: 34 additions & 9 deletions functions/New-GroupMembership.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,60 @@ function New-GroupMembership {
Param (

# The id of an agent
[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $true,
ParameterSetName = 'Properties')]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$UserId,

# The id of a group
[Parameter(Mandatory = $true)]
[Parameter(Mandatory = $true,
ParameterSetName = 'Properties')]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$GroupId,

# If true, tickets assigned directly to the agent will assume this membership's group.
[Parameter(Mandatory = $false)]
[Parameter(Mandatory = $false,
ParameterSetName = 'Properties')]
[Switch]
$Default,

[Parameter(Mandatory = $true,
ParameterSetName = 'Object')]
[ValidateNotNullOrEmpty()]
[PSCustomObject[]]
$Membership,

# Zendesk Connection Context from `Get-ZendeskConnection`
[Parameter(Mandatory = $false)]
[PSTypeName('ZendeskContext')]
[PSCustomObject]
$Context = $null
)

$path = 'api/v2/group_memberships.json'
$body = @{
group_membership = @{
user_id = $UserId
group_id = $GroupId
default = $Default
Assert-IsAdmin -Context $Context

if ($PSCmdlet.ParameterSetName -eq 'Properties') {
$path = "/api/v2/users/$UserId/group_memberships.json"
$body = @{
group_membership = @{
user_id = $UserId
group_id = $GroupId
default = $Default
}
}
} else {
if ($Membership.Count -gt 1) {
$path = '/api/v2/group_memberships/create_many.json'
$body = @{
group_memberships = $Membership
}
} else {
$path = '/api/v2/group_memberships.json'
$body = @{
group_membership = $Membership
}
}
}

Expand Down
14 changes: 13 additions & 1 deletion functions/Remove-GroupMembership.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,30 @@ function Remove-GroupMembership {
[Int64[]]
$Id,

# The id of the user to remove group membership for
[Parameter(Mandatory = $false)]
[ValidateRange(1, [Int64]::MaxValue)]
[Int64]
$UserId,

# Zendesk Connection Context from `Get-ZendeskConnection`
[Parameter(Mandatory = $false)]
[PSTypeName('ZendeskContext')]
[PSCustomObject]
$Context = $null
)

Assert-IsAdmin -Context $Context

if ($Id.count -gt 1) {
$ids = $Id -join ','
$path = "/api/v2/group_memberships/destroy_many.json?ids=$ids"
} else {
$path = "/api/v2/group_memberships/$Id.json"
if ($PSBoundParameters.ContainsKey('UserId')) {
$path = "/api/v2/users/$UserId/group_memberships/$Id.json"
} else {
$path = "/api/v2/group_memberships/$Id.json"
}
}

if ($PSCmdlet.ShouldProcess("$Id", 'Delete Group Memberships')) {
Expand Down
2 changes: 2 additions & 0 deletions functions/Set-GroupMembershipAsDefault.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function Set-GroupMembershipAsDefault {
$Context = $null
)

Assert-IsAgent -Context $Context

$path = "/api/v2/users/$UserId/group_memberships/$MembershipId/make_default.json"

if ($PSCmdlet.ShouldProcess($UserId, "Set default group: $MembershipId")) {
Expand Down
Loading

0 comments on commit 65f2cb0

Please sign in to comment.