Skip to content

Commit 6242c7f

Browse files
Merge pull request KelvinTegelaar#1450 from ngms-psh/feat-Custom-QuarantinePolicies
Feat: Add Custom Quarantine Policies (Additional PR)
2 parents 2003b81 + 0116284 commit 6242c7f

File tree

8 files changed

+766
-95
lines changed

8 files changed

+766
-95
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
function Convert-QuarantinePermissionsValue {
2+
[CmdletBinding(DefaultParameterSetName = 'DecimalValue')]
3+
param (
4+
[Parameter(Mandatory, Position = 0, ParameterSetName = "StringValue")]
5+
[ValidateNotNullOrEmpty()]
6+
[string]$InputObject,
7+
8+
[Parameter(Position = 0, ParameterSetName = "DecimalValue")]
9+
[int]$PermissionToViewHeader = 0,
10+
[Parameter(Position = 1, ParameterSetName = "DecimalValue")]
11+
[int]$PermissionToDownload = 0,
12+
[Parameter(Mandatory, Position = 2, ParameterSetName = "DecimalValue")]
13+
[int]$PermissionToAllowSender,
14+
[Parameter(Mandatory, Position = 3, ParameterSetName = "DecimalValue")]
15+
[int]$PermissionToBlockSender,
16+
[Parameter(Mandatory, Position = 4, ParameterSetName = "DecimalValue")]
17+
[int]$PermissionToRequestRelease,
18+
[Parameter(Mandatory, Position = 5, ParameterSetName = "DecimalValue")]
19+
[int]$PermissionToRelease,
20+
[Parameter(Mandatory, Position = 6, ParameterSetName = "DecimalValue")]
21+
[int]$PermissionToPreview,
22+
[Parameter(Mandatory, Position = 7, ParameterSetName = "DecimalValue")]
23+
[int]$PermissionToDelete
24+
)
25+
26+
#Converts string value with EndUserQuarantinePermissions received from Get-QuarantinePolicy
27+
if (($PSCmdlet.ParameterSetName) -eq "StringValue") {
28+
try {
29+
# Remove square brackets and split into lines
30+
$InputObject = $InputObject.Trim('[', ']')
31+
$hashtable = @{}
32+
$InputObject -split "`n" | ForEach-Object {
33+
$key, $value = $_ -split ":\s*"
34+
$hashtable[$key.Trim()] = [System.Convert]::ToBoolean($value.Trim())
35+
}
36+
return $hashtable
37+
}
38+
catch {
39+
throw "Convert-QuarantinePermissionsValue: Failed to convert string to hashtable."
40+
}
41+
}
42+
43+
#Converts selected end user quarantine permissions to decimal value used by EndUserQuarantinePermissionsValue property in New-QuarantinePolicy and Set-QuarantinePolicy
44+
elseif (($PSCmdlet.ParameterSetName) -eq "DecimalValue") {
45+
try {
46+
# both PermissionToRequestRelease and PermissionToRelease cannot be set to true at the same time
47+
if($PermissionToRequestRelease -eq 1 -and $PermissionToRelease -eq 1) {
48+
throw "PermissionToRequestRelease and PermissionToRelease cannot both be set to true."
49+
}
50+
51+
# Convert each permission to a binary string
52+
$BinaryValue = [string]@(
53+
$PermissionToViewHeader,
54+
$PermissionToDownload,
55+
$PermissionToAllowSender,
56+
$PermissionToBlockSender,
57+
$PermissionToRequestRelease,
58+
$PermissionToRelease,
59+
$PermissionToPreview,
60+
$PermissionToDelete
61+
) -replace '\s',''
62+
63+
# Convert the binary string to an Decimal value
64+
return [convert]::ToInt32($BinaryValue,2)
65+
}
66+
catch {
67+
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
68+
throw "Convert-QuarantinePermissionsValue: Failed to convert QuarantinePermissions to QuarantinePermissionsValue. Error: $ErrorMessage"
69+
}
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 and overwrite any other tenant selection
20+
if ("AllTenants" -in $Tenants) {
21+
$tenants = (Get-Tenants).defaultDomainName
22+
}
23+
24+
$Result = foreach ($TenantFilter in $tenants) {
25+
try {
26+
$ReleaseActionPreference = $Request.Body.ReleaseActionPreference.value ?? $Request.Body.ReleaseActionPreference
27+
28+
$EndUserQuarantinePermissions = @{
29+
PermissionToBlockSender = $Request.Body.BlockSender
30+
PermissionToDelete = $Request.Body.Delete
31+
PermissionToPreview = $Request.Body.Preview
32+
PermissionToRelease = $ReleaseActionPreference -eq "Release" ? $true : $false
33+
PermissionToRequestRelease = $ReleaseActionPreference -eq "RequestRelease" ? $true : $false
34+
PermissionToAllowSender = $Request.Body.AllowSender
35+
}
36+
37+
$Params = @{
38+
Identity = $Request.Body.Name
39+
EndUserQuarantinePermissions = $EndUserQuarantinePermissions
40+
ESNEnabled = $Request.Body.QuarantineNotification
41+
IncludeMessagesFromBlockedSenderAddress = $Request.Body.IncludeMessagesFromBlockedSenderAddress
42+
action = "New"
43+
tenantFilter = $TenantFilter
44+
APIName = $APIName
45+
}
46+
47+
Set-CIPPQuarantinePolicy @Params
48+
$Message = "Created Quarantine policy '$($Request.Body.Name)' for tenant '$($TenantFilter)'"
49+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Message -Sev Info
50+
$Message
51+
52+
}
53+
catch {
54+
$ErrorMessage = Get-CippException -Exception $_
55+
$Message = "Failed to create Quarantine policy '$($Request.Body.Name)' for tenant '$($TenantFilter)' - $($ErrorMessage.NormalizedError)"
56+
Write-LogMessage -Headers $Headers -API $APIName -tenant $TenantFilter -message $Message -Sev Error -LogData $ErrorMessage
57+
$Message
58+
}
59+
}
60+
61+
# Associate values to output bindings by calling 'Push-OutputBinding'.
62+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
63+
StatusCode = [HttpStatusCode]::OK
64+
Body = @{Results = @($Result) }
65+
})
66+
67+
}
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)