Skip to content

Commit 9b939c5

Browse files
committed
Added endpoints for new quarantine policies page.
1 parent bc9d1f1 commit 9b939c5

File tree

4 files changed

+237
-0
lines changed

4 files changed

+237
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using namespace System.Net
2+
3+
Function Invoke-AddQuarantinePolicy {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
Exchange.Spamfilter.ReadWrite
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $Request.Params.CIPPEndpoint
14+
$Headers = $Request.Headers
15+
Write-LogMessage -Headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
16+
17+
$Tenants = ($Request.body.selectedTenants).value
18+
19+
# If allTenants is selected, get all tenants from table and overwrite any other tenant selection
20+
if ("AllTenants" -in $Tenants) {
21+
$TenantTable = Get-CIPPTable -tablename 'Tenants'
22+
$tenants = (Get-CIPPAzDataTableEntity @TenantTable -Filter "PartitionKey eq 'Tenants'").defaultDomainName
23+
}
24+
25+
$Result = foreach ($TenantFilter in $tenants) {
26+
try {
27+
$ReleaseActionPreference = $Request.Body.ReleaseActionPreference.value ?? $Request.Body.ReleaseActionPreference
28+
29+
$EndUserQuarantinePermissions = @{
30+
PermissionToBlockSender = $Request.Body.BlockSender
31+
PermissionToDelete = $Request.Body.Delete
32+
PermissionToPreview = $Request.Body.Preview
33+
PermissionToRelease = $ReleaseActionPreference -eq "Release" ? $true : $false
34+
PermissionToRequestRelease = $ReleaseActionPreference -eq "RequestRelease" ? $true : $false
35+
PermissionToAllowSender = $Request.Body.AllowSender
36+
}
37+
38+
$Params = @{
39+
Identity = $Request.Body.Name
40+
EndUserQuarantinePermissions = $EndUserQuarantinePermissions
41+
ESNEnabled = $Request.Body.QuarantineNotification
42+
IncludeMessagesFromBlockedSenderAddress = $Request.Body.IncludeMessagesFromBlockedSenderAddress
43+
action = "New"
44+
tenantFilter = $TenantFilter
45+
APIName = $APIName
46+
}
47+
48+
Set-CIPPQuarantinePolicy @Params
49+
$Message = "Created Quarantine policy '$($Request.Body.Name)' for tenant '$($TenantFilter)'"
50+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Message -Sev Info
51+
$Message
52+
53+
}
54+
catch {
55+
$ErrorMessage = Get-CippException -Exception $_
56+
$Message = "Failed to create Quarantine policy '$($Request.Body.Name)' for tenant '$($TenantFilter)' - $($ErrorMessage.NormalizedError)"
57+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Message -Sev Error -LogData $ErrorMessage
58+
$Message
59+
}
60+
}
61+
62+
# Associate values to output bindings by calling 'Push-OutputBinding'.
63+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
64+
StatusCode = [HttpStatusCode]::OK
65+
Body = @{Results = @($Result) }
66+
})
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using namespace System.Net
2+
3+
Function Invoke-EditQuarantinePolicy {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
Exchange.Spamfilter.ReadWrite
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $Request.Params.CIPPEndpoint
14+
$Headers = $Request.Headers
15+
Write-LogMessage -Headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
16+
17+
$TenantFilter = $Request.Query.TenantFilter ?? $Request.Body.TenantFilter
18+
19+
if ($Request.Query.Type -eq "GlobalQuarantinePolicy") {
20+
21+
$Frequency = $Request.Body.EndUserSpamNotificationFrequency.value ?? $Request.Body.EndUserSpamNotificationFrequency
22+
# If request EndUserSpamNotificationFrequency it not set to a ISO 8601 timeformat, convert it to one.
23+
# This happens if the user doesn't change the Notification Frequency value in the UI. Because of a "bug" with setDefaultValue function with the cippApiDialog, where "label" is set to both label and value.
24+
$EndUserSpamNotificationFrequency = switch ($Frequency) {
25+
"4 Hours" { "PT4H" }
26+
"Daily" { "P1D" }
27+
"Weekly" { "P7D" }
28+
Default { $Frequency }
29+
}
30+
31+
$Params = @{
32+
Identity = $Request.Body.Identity
33+
# Convert the requested frequency from ISO 8601 to a TimeSpan object
34+
EndUserSpamNotificationFrequency = [System.Xml.XmlConvert]::ToTimeSpan($EndUserSpamNotificationFrequency)
35+
EndUserSpamNotificationCustomFromAddress = $Request.Body.EndUserSpamNotificationCustomFromAddress
36+
OrganizationBrandingEnabled = $Request.Body.OrganizationBrandingEnabled
37+
}
38+
}
39+
else {
40+
$ReleaseActionPreference = $Request.Body.ReleaseActionPreference.value ?? $Request.Body.ReleaseActionPreference
41+
42+
$EndUserQuarantinePermissions = @{
43+
PermissionToBlockSender = $Request.Body.BlockSender
44+
PermissionToDelete = $Request.Body.Delete
45+
PermissionToPreview = $Request.Body.Preview
46+
PermissionToRelease = $ReleaseActionPreference -eq "Release" ? $true : $false
47+
PermissionToRequestRelease = $ReleaseActionPreference -eq "RequestRelease" ? $true : $false
48+
PermissionToAllowSender = $Request.Body.AllowSender
49+
}
50+
51+
$Params = @{
52+
Identity = $Request.Body.Identity
53+
EndUserQuarantinePermissions = $EndUserQuarantinePermissions
54+
ESNEnabled = $Request.Body.QuarantineNotification
55+
IncludeMessagesFromBlockedSenderAddress = $Request.Body.IncludeMessagesFromBlockedSenderAddress
56+
action = $Request.Body.Action ?? "Set"
57+
}
58+
}
59+
60+
try {
61+
Set-CIPPQuarantinePolicy @Params -tenantFilter $TenantFilter -APIName $APIName
62+
63+
$Result = "Updated Quarantine policy '$($Request.Body.Name)'"
64+
$StatusCode = [HttpStatusCode]::OK
65+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Result -Sev Info
66+
}
67+
catch {
68+
$Result = "Failed to update Quarantine policy '$($Request.Body.Name)' - $($_)"
69+
$StatusCode = [HttpStatusCode]::Forbidden
70+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Result -Sev Error -LogData $ErrorMessage
71+
}
72+
73+
# Associate values to output bindings by calling 'Push-OutputBinding'.
74+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
75+
StatusCode = $StatusCode
76+
Body = @{Results = $Result }
77+
})
78+
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function Invoke-ListQuarantinePolicy {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
Exchange.SpamFilter.Read
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
$APIName = $Request.Params.CIPPEndpoint
12+
$Headers = $Request.Headers
13+
Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
14+
15+
# Interact with query parameters or the body of the request.
16+
$TenantFilter = $Request.Query.TenantFilter ?? $Request.body.TenantFilter
17+
$QuarantinePolicyType = $Request.Query.Type ?? 'QuarantinePolicy'
18+
19+
$Policies = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Get-QuarantinePolicy' -cmdParams @{QuarantinePolicyType=$QuarantinePolicyType} | Select-Object -Property * -ExcludeProperty *odata*, *data.type*
20+
21+
write-host $($Request | ConvertTo-Json -Depth 10)
22+
23+
if ($QuarantinePolicyType -eq 'QuarantinePolicy') {
24+
# Convert the string EndUserQuarantinePermissions to individual properties
25+
$Policies | ForEach-Object {
26+
$Permissions = Convert-QuarantinePermissionsValue -InputObject $_.EndUserQuarantinePermissions
27+
foreach ($Perm in $Permissions.GetEnumerator()) {
28+
$_ | Add-Member -MemberType NoteProperty -Name ($Perm.Key -replace "PermissionTo", "" ) -Value $Perm.Value
29+
}
30+
}
31+
32+
# "convert" to values display in the UI and Builtin used for filtering
33+
$Policies = $Policies | Select-Object -Property *,
34+
@{ Name = 'QuarantineNotification'; Expression = { $_.ESNEnabled -eq $true ? $true : $false} },
35+
@{ Name = 'ReleaseActionPreference'; Expression = { $_.Release -eq $true ? "Release" : "RequestRelease"} },
36+
@{ Name = 'Builtin'; Expression = { $_.Guid -eq "00000000-0000-0000-0000-000000000000" ? $true : $false} }
37+
}
38+
39+
40+
# Associate values to output bindings by calling 'Push-OutputBinding'.
41+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
42+
StatusCode = [HttpStatusCode]::OK
43+
Body = @($Policies)
44+
})
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using namespace System.Net
2+
3+
Function Invoke-RemoveQuarantinePolicy {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
Exchange.Spamfilter.ReadWrite
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $Request.Params.CIPPEndpoint
14+
$Headers = $Request.Headers
15+
Write-LogMessage -Headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
16+
$TenantFilter = $Request.Query.TenantFilter ?? $Request.Body.TenantFilter
17+
$PolicyName = $Request.Query.Name ?? $Request.Body.Name
18+
$Identity = $Request.Query.Identity ?? $Request.Body.Identity
19+
20+
try {
21+
$Params = @{
22+
Identity = ($Identity -eq "00000000-0000-0000-0000-000000000000" ? $PolicyName : $Identity)
23+
}
24+
25+
$null = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Remove-QuarantinePolicy' -cmdParams $Params -useSystemMailbox $true
26+
27+
$Result = "Deleted Quarantine policy '$($PolicyName)'"
28+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Result -Sev Info
29+
$StatusCode = [HttpStatusCode]::OK
30+
} catch {
31+
$ErrorMessage = Get-CippException -Exception $_
32+
$Result = "Failed to remove Quarantine policy '$($PolicyName)' - $($ErrorMessage.NormalizedError -replace '\|Microsoft.Exchange.Management.Tasks.ValidationException\|', '')"
33+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Result -Sev Error -LogData $ErrorMessage
34+
$StatusCode = [HttpStatusCode]::Forbidden
35+
}
36+
37+
$StatusCode = [HttpStatusCode]::OK
38+
39+
# Associate values to output bindings by calling 'Push-OutputBinding'.
40+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
41+
StatusCode = $StatusCode
42+
Body = @{Results = $Result }
43+
})
44+
45+
}

0 commit comments

Comments
 (0)