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

Add ExternalDirectoryObjectId to ListMailbox, improve logging, and fix calendar permission bug #1166

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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