Skip to content

Commit 5e75036

Browse files
authored
Merge pull request #167 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 4348e9b + e7bc5be commit 5e75036

File tree

4 files changed

+59
-52
lines changed

4 files changed

+59
-52
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Administration/Invoke-ExecEditCalendarPermissions.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-ExecEditCalendarPermissions {
3+
function Invoke-ExecEditCalendarPermissions {
44
<#
55
.FUNCTIONALITY
66
Entrypoint
@@ -16,9 +16,9 @@ Function Invoke-ExecEditCalendarPermissions {
1616

1717
# Extract parameters from query or body
1818
$TenantFilter = $Request.Query.tenantFilter ?? $Request.Body.tenantFilter
19-
$UserID = $Request.Query.UserID ?? $Request.Body.UserID
19+
$UserID = $Request.Query.userid ?? $Request.Body.userid
2020
$UserToGetPermissions = $Request.Query.UserToGetPermissions ?? $Request.Body.UserToGetPermissions.value
21-
$Permissions = @($Request.Query.Permissions) ?? @($Request.Body.Permissions.value)
21+
$Permissions = $Request.Query.Permissions ?? $Request.Body.Permissions.value
2222
$FolderName = $Request.Query.FolderName ?? $Request.Body.FolderName
2323
$RemoveAccess = $Request.Query.RemoveAccess ?? $Request.Body.RemoveAccess.value
2424

@@ -32,6 +32,8 @@ Function Invoke-ExecEditCalendarPermissions {
3232
} catch {
3333
$Result = $_.Exception.Message
3434
$StatusCode = [HttpStatusCode]::Forbidden
35+
Write-Warning "Error in ExecEditCalendarPermissions: $($_.Exception.Message)"
36+
Write-Information $_.InvocationInfo.PositionMessage
3537
}
3638

3739
# Associate values to output bindings by calling 'Push-OutputBinding'.

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Administration/Groups/Invoke-EditGroup.ps1

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -167,29 +167,7 @@ function Invoke-EditGroup {
167167
$AddOwners = $userobj.AddOwner
168168
try {
169169
if ($AddOwners) {
170-
if ($GroupType -eq 'Distribution List' -or $GroupType -eq 'Mail-Enabled Security') {
171-
$CurrentOwners = New-ExoRequest -tenantid $TenantId -cmdlet 'Get-DistributionGroup' -cmdParams @{ Identity = $userobj.groupid } -UseSystemMailbox $true | Select-Object -ExpandProperty ManagedBy
172-
$NewManagedBy = [System.Collections.Generic.List[string]]::new()
173-
foreach ($CurrentOwner in $CurrentOwners) {
174-
$NewManagedBy.Add($CurrentOwner)
175-
}
176-
foreach ($NewOwner in $AddOwners) {
177-
$NewManagedBy.Add($NewOwner.addedFields.id)
178-
}
179-
180-
$NewManagedBy = $NewManagedBy | Sort-Object -Unique
181-
$params = @{ Identity = $userobj.groupid; ManagedBy = $NewManagedBy }
182-
$ExoBulkRequests.Add(@{
183-
CmdletInput = @{
184-
CmdletName = 'Set-DistributionGroup'
185-
Parameters = $params
186-
}
187-
})
188-
$ExoLogs.Add(@{
189-
message = "Added owner(s) $($AddOwners.value -join ', ') to $($GroupName) group"
190-
target = $userobj.groupid
191-
})
192-
} else {
170+
if ($GroupType -notin @('Distribution List', 'Mail-Enabled Security')) {
193171
$AddOwners | ForEach-Object {
194172
$Owner = $_.value
195173
$ID = $_.addedFields.id
@@ -219,22 +197,7 @@ function Invoke-EditGroup {
219197
$RemoveOwners = $userobj.RemoveOwner
220198
try {
221199
if ($RemoveOwners) {
222-
if ($GroupType -eq 'Distribution List' -or $GroupType -eq 'Mail-Enabled Security') {
223-
# get current owners
224-
$CurrentOwners = New-ExoRequest -tenantid $TenantId -cmdlet 'Get-DistributionGroup' -cmdParams @{ Identity = $userobj.groupid } -UseSystemMailbox $true | Select-Object -ExpandProperty ManagedBy
225-
$NewManagedBy = $CurrentOwners | Where-Object { $_ -notin $RemoveOwners.addedFields.id }
226-
$params = @{ Identity = $userobj.groupid; ManagedBy = $NewManagedBy }
227-
$ExoBulkRequests.Add(@{
228-
CmdletInput = @{
229-
CmdletName = 'Set-DistributionGroup'
230-
Parameters = $params
231-
}
232-
})
233-
$ExoLogs.Add(@{
234-
message = "Removed owner(s) $($RemoveOwners.value -join ', ') from $($GroupName) group"
235-
target = $userobj.groupid
236-
})
237-
} else {
200+
if ($GroupType -notin @('Distribution List', 'Mail-Enabled Security')) {
238201
$RemoveOwners | ForEach-Object {
239202
$ID = $_.addedFields.id
240203
$BulkRequests.Add(@{
@@ -253,6 +216,41 @@ function Invoke-EditGroup {
253216
Write-Warning "Error in RemoveOwners: $($_.Exception.Message)"
254217
}
255218

219+
if ($GroupType -in @( 'Distribution List', 'Mail-Enabled Security') -and ($AddOwners -or $RemoveOwners)) {
220+
$CurrentOwners = New-ExoRequest -tenantid $TenantId -cmdlet 'Get-DistributionGroup' -cmdParams @{ Identity = $userobj.groupid } -UseSystemMailbox $true | Select-Object -ExpandProperty ManagedBy
221+
222+
$NewManagedBy = [system.collections.generic.list[string]]::new()
223+
foreach ($CurrentOwner in $CurrentOwners) {
224+
if ($RemoveOwners -and $RemoveOwners.addedFields.id -contains $CurrentOwner) {
225+
$OwnerToRemove = $RemoveOwners | Where-Object { $_.addedFields.id -eq $CurrentOwner }
226+
$ExoLogs.Add(@{
227+
message = "Removed owner $($OwnerToRemove.label) from $($GroupName) group"
228+
target = $userobj.groupid
229+
})
230+
continue
231+
}
232+
$NewManagedBy.Add($CurrentOwner)
233+
}
234+
if ($AddOwners) {
235+
foreach ($NewOwner in $AddOwners) {
236+
$NewManagedBy.Add($NewOwner.addedFields.id)
237+
$ExoLogs.Add(@{
238+
message = "Added owner $($NewOwner.label) to $($GroupName) group"
239+
target = $userobj.groupid
240+
})
241+
}
242+
}
243+
244+
$NewManagedBy = $NewManagedBy | Sort-Object -Unique
245+
$params = @{ Identity = $userobj.groupid; ManagedBy = $NewManagedBy }
246+
$ExoBulkRequests.Add(@{
247+
CmdletInput = @{
248+
CmdletName = 'Set-DistributionGroup'
249+
Parameters = $params
250+
}
251+
})
252+
}
253+
256254
Write-Information "Graph Bulk Requests: $($BulkRequests.Count)"
257255
if ($BulkRequests.Count -gt 0) {
258256
#Write-Warning 'EditUser - Executing Graph Bulk Requests'

Modules/CIPPCore/Public/GraphHelper/New-ExoBulkRequest.ps1

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,17 @@ function New-ExoBulkRequest {
8585
}
8686
$BatchBodyJson = ConvertTo-Json -InputObject $BatchBodyObj -Depth 10
8787
$Results = Invoke-RestMethod $BatchURL -ResponseHeadersVariable responseHeaders -Method POST -Body $BatchBodyJson -Headers $Headers -ContentType 'application/json; charset=utf-8'
88-
$ReturnedData.AddRange(@($Results.responses))
88+
foreach ($Response in $Results.responses) {
89+
$ReturnedData.Add($Response)
90+
}
8991

9092
Write-Host "Batch #$($batches.IndexOf($batch) + 1) of $($batches.Count) processed"
9193
}
9294
} catch {
9395
# Error handling (omitted for brevity)
9496
}
9597

96-
Write-Information ($ReturnedHeaders | ConvertTo-Json -Depth 10)
98+
#Write-Information ($responseHeaders | ConvertTo-Json -Depth 10)
9799

98100
# Process the returned data
99101
if ($ReturnWithCommand) {
@@ -114,13 +116,14 @@ function New-ExoBulkRequest {
114116
}
115117
$body | Add-Member -MemberType NoteProperty -Name 'value' -Value $msg -Force
116118
}
117-
$resultValue = $body.value
118-
119-
if (-not $FinalData.ContainsKey($CmdletName)) {
120-
$FinalData[$CmdletName] = [System.Collections.Generic.List[object]]::new()
121-
$FinalData.$CmdletName.Add($resultValue)
122-
} else {
123-
$FinalData.$CmdletName.Add($resultValue)
119+
$resultValues = $body.value
120+
foreach ($resultValue in $resultValues) {
121+
if (-not $FinalData.ContainsKey($CmdletName)) {
122+
$FinalData[$CmdletName] = [System.Collections.Generic.List[object]]::new()
123+
$FinalData.$CmdletName.Add($resultValue)
124+
} else {
125+
$FinalData.$CmdletName.Add($resultValue)
126+
}
124127
}
125128
}
126129
} else {

Modules/CIPPCore/Public/Set-CIPPCalendarPermission.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function Set-CIPPCalendarPermission {
1313
)
1414

1515
try {
16+
1617
# If a pretty logging name is not provided, use the ID instead
1718
if ([string]::IsNullOrWhiteSpace($LoggingName) -and $RemoveAccess) {
1819
$LoggingName = $RemoveAccess
@@ -25,6 +26,7 @@ function Set-CIPPCalendarPermission {
2526
AccessRights = @($Permissions)
2627
User = $UserToGetPermissions
2728
}
29+
2830
if ($RemoveAccess) {
2931
if ($PSCmdlet.ShouldProcess("$UserID\$folderName", "Remove permissions for $LoggingName")) {
3032
$null = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Remove-MailboxFolderPermission' -cmdParams @{Identity = "$($UserID):\$folderName"; User = $RemoveAccess }
@@ -43,7 +45,9 @@ function Set-CIPPCalendarPermission {
4345
}
4446
}
4547
} catch {
46-
$ErrorMessage = Get-CippException -Message $_
48+
$ErrorMessage = Get-CippException -Exception $_
49+
Write-Warning "Error changing calendar permissions $($_.Exception.Message)"
50+
Write-Information $_.InvocationInfo.PositionMessage
4751
$Result = "Failed to set calendar permissions for $LoggingName on $UserID : $($ErrorMessage.NormalizedError)"
4852
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $Result -sev Error -LogData $ErrorMessage
4953
throw $Result

0 commit comments

Comments
 (0)