Skip to content

Commit

Permalink
Merge pull request #1166 from kris6673/dev
Browse files Browse the repository at this point in the history
Add ExternalDirectoryObjectId to ListMailbox, improve logging, and fix calendar permission bug
  • Loading branch information
JohnDuprey authored Oct 30, 2024
2 parents cc9f3bd + c8f1243 commit 2cb07f9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ Function Invoke-ExecEditCalendarPermissions {
param($Request, $TriggerMetadata)

$APIName = $TriggerMetadata.FunctionName
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
$UserID = ($request.query.UserID)
$User = $Request.headers.'x-ms-client-principal'
Write-LogMessage -user $User -API $APINAME -message 'Accessed this API' -Sev 'Debug'

$UserID = ($Request.query.UserID)
$LoggingName = $Request.query.LoggingName
$UserToGetPermissions = $Request.query.UserToGetPermissions
$Tenantfilter = $request.Query.tenantfilter
$Tenantfilter = $Request.Query.tenantfilter
$Permissions = @($Request.query.permissions)
$folderName = $Request.query.folderName


try {
if ($Request.query.removeaccess) {
$result = Set-CIPPCalendarPermission -UserID $UserID -folderName $folderName -RemoveAccess $Request.query.removeaccess -TenantFilter $TenantFilter
$Result = Set-CIPPCalendarPermission -UserID $UserID -folderName $folderName -RemoveAccess $Request.query.removeaccess -TenantFilter $TenantFilter -LoggingName $LoggingName
} else {
$result = Set-CIPPCalendarPermission -UserID $UserID -folderName $folderName -TenantFilter $Tenantfilter -UserToGetPermissions $UserToGetPermissions -Permissions $Permissions
$Result = "Successfully set permissions on folder $($CalParam.Identity). The user $UserToGetPermissions now has $Permissions permissions on this folder."
$Result = Set-CIPPCalendarPermission -UserID $UserID -folderName $folderName -TenantFilter $Tenantfilter -UserToGetPermissions $UserToGetPermissions -LoggingName $LoggingName -Permissions $Permissions
}
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception
Expand Down
4 changes: 2 additions & 2 deletions Modules/CIPPCore/Public/Entrypoints/Invoke-ListMailboxes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Function Invoke-ListMailboxes {
# Interact with query parameters or the body of the request.
$TenantFilter = $Request.Query.TenantFilter
try {
$Select = 'id,ExchangeGuid,ArchiveGuid,UserPrincipalName,DisplayName,PrimarySMTPAddress,RecipientType,RecipientTypeDetails,EmailAddresses,WhenSoftDeleted,IsInactiveMailbox,ForwardingSmtpAddress,DeliverToMailboxAndForward,ForwardingAddress'
$Select = 'id,ExchangeGuid,ExternalDirectoryObjectId,ArchiveGuid,UserPrincipalName,DisplayName,PrimarySMTPAddress,RecipientType,RecipientTypeDetails,EmailAddresses,WhenSoftDeleted,IsInactiveMailbox,ForwardingSmtpAddress,DeliverToMailboxAndForward,ForwardingAddress'
$ExoRequest = @{
tenantid = $TenantFilter
cmdlet = 'Get-Mailbox'
Expand Down Expand Up @@ -59,7 +59,7 @@ Function Invoke-ListMailboxes {
}

Write-Host ($ExoRequest | ConvertTo-Json)
$GraphRequest = (New-ExoRequest @ExoRequest) | Select-Object id, ExchangeGuid, ArchiveGuid, WhenSoftDeleted, @{ Name = 'UPN'; Expression = { $_.'UserPrincipalName' } },
$GraphRequest = (New-ExoRequest @ExoRequest) | Select-Object id, ExchangeGuid, ExternalDirectoryObjectId, ArchiveGuid, WhenSoftDeleted, @{ Name = 'UPN'; Expression = { $_.'UserPrincipalName' } },

@{ Name = 'displayName'; Expression = { $_.'DisplayName' } },
@{ Name = 'primarySmtpAddress'; Expression = { $_.'PrimarySMTPAddress' } },
Expand Down
20 changes: 14 additions & 6 deletions Modules/CIPPCore/Public/Set-CIPPCalendarPermission.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,38 @@ function Set-CIPPCalendarPermission {
$UserID,
$folderName,
$UserToGetPermissions,
$LoggingName,
$Permissions
)

try {
# If a pretty logging name is not provided, use the ID instead
if ([string]::IsNullOrWhiteSpace($LoggingName) -and $RemoveAccess) {
$LoggingName = $RemoveAccess
} elseif ([string]::IsNullOrWhiteSpace($LoggingName) -and $UserToGetPermissions) {
$LoggingName = $UserToGetPermissions
}

$CalParam = [PSCustomObject]@{
Identity = "$($UserID):\$folderName"
AccessRights = @($Permissions)
User = $UserToGetPermissions
}
if ($RemoveAccess) {
if ($PSCmdlet.ShouldProcess("$UserID\$folderName", "Remove permissions for $RemoveAccess")) {
if ($PSCmdlet.ShouldProcess("$UserID\$folderName", "Remove permissions for $LoggingName")) {
$null = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Remove-MailboxFolderPermission' -cmdParams @{Identity = "$($UserID):\$folderName"; User = $RemoveAccess }
$Result = "Successfully removed access for $RemoveAccess from calendar $($CalParam.Identity)"
Write-LogMessage -API 'CalendarPermissions' -tenant $TenantFilter -message "Successfully removed access for $RemoveAccess from calendar $($UserID)" -sev Debug
$Result = "Successfully removed access for $LoggingName from calendar $($CalParam.Identity)"
Write-LogMessage -API 'CalendarPermissions' -tenant $TenantFilter -message "Successfully removed access for $LoggingName from calendar $($UserID)" -sev Info
}
} else {
if ($PSCmdlet.ShouldProcess("$UserID\$folderName", "Set permissions for $UserToGetPermissions to $Permissions")) {
if ($PSCmdlet.ShouldProcess("$UserID\$folderName", "Set permissions for $LoggingName to $Permissions")) {
try {
$null = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Set-MailboxFolderPermission' -cmdParams $CalParam -Anchor $UserID
} catch {
$null = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Add-MailboxFolderPermission' -cmdParams $CalParam -Anchor $UserID
}
Write-LogMessage -API 'CalendarPermissions' -tenant $TenantFilter -message "Calendar permissions added for $UserToGetPermissions on $UserID." -sev Debug
$Result = "Successfully set permissions on folder $($CalParam.Identity). The user $UserToGetPermissions now has $Permissions permissions on this folder."
Write-LogMessage -API 'CalendarPermissions' -tenant $TenantFilter -message "Calendar permissions added for $LoggingName on $UserID." -sev Info
$Result = "Successfully set permissions on folder $($CalParam.Identity). The user $LoggingName now has $Permissions permissions on this folder."
}
}
} catch {
Expand Down

0 comments on commit 2cb07f9

Please sign in to comment.