Skip to content

Commit 593911e

Browse files
Feat: Add Invoke-ExecSetCASMailbox function for CAS settings management (KelvinTegelaar#2076)
Frontend PR: KelvinTegelaar/CIPP#6110
2 parents 9a176cc + 49e8af8 commit 593911e

2 files changed

Lines changed: 82 additions & 9 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
function Invoke-ExecSetCASMailbox {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
Exchange.Mailbox.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
$APIName = $Request.Params.CIPPEndpoint
12+
$Headers = $Request.Headers
13+
14+
$TenantFilter = $Request.Body.tenantFilter
15+
$Identity = $Request.Body.Identity
16+
$DisplayName = $Request.Body.DisplayName ?? $Identity
17+
18+
# The CAS protocols we allow toggling. Note SmtpClientAuthenticationDisabled is inverted:
19+
# $true means SMTP client authentication is DISABLED for the mailbox.
20+
$ValidProtocols = @(
21+
'OWAEnabled'
22+
'ECPEnabled'
23+
'IMAPEnabled'
24+
'POPEnabled'
25+
'MAPIEnabled'
26+
'EWSEnabled'
27+
'ActiveSyncEnabled'
28+
'SmtpClientAuthenticationDisabled'
29+
)
30+
31+
# Build the cmdlet parameters from any valid protocol values supplied in the body.
32+
$CmdParams = @{ Identity = $Identity }
33+
foreach ($Protocol in $ValidProtocols) {
34+
if ($null -ne $Request.Body.$Protocol) {
35+
$CmdParams[$Protocol] = [System.Convert]::ToBoolean($Request.Body.$Protocol)
36+
}
37+
}
38+
39+
# SMTP client authentication can only be turned off via this endpoint. Drop an enable
40+
# attempt (SmtpClientAuthenticationDisabled = $false) but still apply the other protocols.
41+
$Warnings = [System.Collections.Generic.List[string]]::new()
42+
if ($CmdParams.ContainsKey('SmtpClientAuthenticationDisabled') -and $CmdParams['SmtpClientAuthenticationDisabled'] -eq $false) {
43+
$null = $CmdParams.Remove('SmtpClientAuthenticationDisabled')
44+
$Warnings.Add('SMTP Client Authentication can only be disabled, not enabled, and was left unchanged.')
45+
}
46+
47+
# Nothing left to apply: return the warning if we dropped one, otherwise a generic message.
48+
if ($CmdParams.Keys.Count -le 1) {
49+
$Results = $Warnings.Count -gt 0 ? ($Warnings -join ' ') : 'No CAS protocol settings were supplied.'
50+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Results -Sev 'Info'
51+
return ([HttpResponseContext]@{
52+
StatusCode = [HttpStatusCode]::BadRequest
53+
Body = @{ 'Results' = $Results }
54+
})
55+
}
56+
57+
# Human readable summary of the change(s) for logging and the API result.
58+
$ChangeSummary = ($CmdParams.GetEnumerator() | Where-Object { $_.Key -ne 'Identity' } | ForEach-Object {
59+
'{0} = {1}' -f $_.Key, $_.Value
60+
}) -join ', '
61+
62+
try {
63+
$null = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Set-CASMailbox' -cmdParams $CmdParams
64+
$Results = "Successfully set CAS settings for $DisplayName ($ChangeSummary)"
65+
if ($Warnings.Count -gt 0) {
66+
$Results = '{0}. {1}' -f $Results, ($Warnings -join ' ')
67+
}
68+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Results -Sev Info
69+
$StatusCode = [HttpStatusCode]::OK
70+
} catch {
71+
$ErrorMessage = Get-CippException -Exception $_
72+
$Results = "Failed to set CAS settings for $DisplayName. Error: $($ErrorMessage.NormalizedError)"
73+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Results -Sev Error -LogData $ErrorMessage
74+
$StatusCode = [HttpStatusCode]::InternalServerError
75+
}
76+
77+
return ([HttpResponseContext]@{
78+
StatusCode = $StatusCode
79+
Body = @{ 'Results' = $Results }
80+
})
81+
}

Modules/CIPPHTTP/Public/Entrypoints/HTTP Functions/Email-Exchange/Reports/Invoke-ListMailboxCAS.ps1

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@ function Invoke-ListMailboxCAS {
1010
# Interact with query parameters or the body of the request.
1111
$TenantFilter = $Request.Query.TenantFilter
1212
try {
13-
$GraphRequest = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Get-CasMailbox' | Select-Object @{ Name = 'displayName'; Expression = { $_.'DisplayName' } },
14-
@{ Name = 'primarySmtpAddress'; Expression = { $_.'PrimarySMTPAddress' } },
15-
@{ Name = 'ecpenabled'; Expression = { $_.'ECPEnabled' } },
16-
@{ Name = 'owaenabled'; Expression = { $_.'OWAEnabled' } },
17-
@{ Name = 'imapenabled'; Expression = { $_.'IMAPEnabled' } },
18-
@{ Name = 'popenabled'; Expression = { $_.'POPEnabled' } },
19-
@{ Name = 'mapienabled'; Expression = { $_.'MAPIEnabled' } },
20-
@{ Name = 'ewsenabled'; Expression = { $_.'EWSEnabled' } },
21-
@{ Name = 'activesyncenabled'; Expression = { $_.'ActiveSyncEnabled' } }
13+
$GraphRequest = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Get-CasMailbox' | Select-Object DisplayName, PrimarySmtpAddress, Guid, ECPEnabled, OWAEnabled, IMAPEnabled, POPEnabled, MAPIEnabled, EWSEnabled, ActiveSyncEnabled, SmtpClientAuthenticationDisabled
2214
$StatusCode = [HttpStatusCode]::OK
2315
} catch {
2416
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message

0 commit comments

Comments
 (0)