Skip to content

Commit 1ee6169

Browse files
author
Luis Mengel
committed
add multi-tenant support for Scripted CIPP Alerts
1 parent 89f8894 commit 1ee6169

2 files changed

Lines changed: 102 additions & 2 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
function Invoke-AddScriptedAlert {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
CIPP.Alert.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
$tenantsJsonForStorage = $null
12+
13+
if ($Request.Body.tenantFilter -is [array] -and @($Request.Body.tenantFilter).Count -eq 1) {
14+
$Request.Body | Add-Member -MemberType NoteProperty -Name 'tenantFilter' -Value $Request.Body.tenantFilter[0] -Force
15+
}
16+
17+
if ($Request.Body.tenantFilter -is [array] -and @($Request.Body.tenantFilter).Count -gt 1) {
18+
try {
19+
$originalSelection = @($Request.Body.tenantFilter)
20+
$tenantsJsonForStorage = $originalSelection | ConvertTo-Json -Compress -Depth 10
21+
22+
$hasAllTenants = @($originalSelection | Where-Object { $_.value -eq 'AllTenants' }).Count -gt 0
23+
24+
if (-not $hasAllTenants) {
25+
$ExpandedSelection = Expand-CIPPTenantGroups -TenantFilter $originalSelection
26+
$targetDomains = @($ExpandedSelection | ForEach-Object { $_.value })
27+
28+
$AllTenantsList = Get-Tenants -IncludeErrors
29+
$computedExcluded = @($AllTenantsList.defaultDomainName | Where-Object { $_ -notin $targetDomains })
30+
31+
$existingExcluded = @()
32+
if ($Request.Body.PSObject.Properties['excludedTenants'] -and $Request.Body.excludedTenants) {
33+
$existingExcluded = @($Request.Body.excludedTenants | ForEach-Object { $_.value ?? $_ })
34+
}
35+
$mergedExcluded = @($existingExcluded + $computedExcluded) | Where-Object { $_ } | Select-Object -Unique
36+
37+
$excludedValue = @($mergedExcluded | ForEach-Object {
38+
[PSCustomObject]@{ value = $_; label = $_ }
39+
})
40+
$Request.Body | Add-Member -MemberType NoteProperty -Name 'excludedTenants' -Value $excludedValue -Force
41+
}
42+
43+
if (-not $Request.Body.PSObject.Properties['RowKey'] -or -not $Request.Body.RowKey) {
44+
$Request.Body | Add-Member -MemberType NoteProperty -Name 'RowKey' -Value ((New-Guid).Guid) -Force
45+
}
46+
47+
$tenantFilterValue = [PSCustomObject]@{
48+
value = 'AllTenants'
49+
label = '*All Tenants'
50+
type = 'Tenant'
51+
}
52+
$Request.Body | Add-Member -MemberType NoteProperty -Name 'tenantFilter' -Value $tenantFilterValue -Force
53+
} catch {
54+
Write-Warning "Failed to process multi-tenant alert selection: $($_.Exception.Message)"
55+
$tenantsJsonForStorage = $null
56+
}
57+
}
58+
59+
$ForwardRequest = @{
60+
Query = @{ hidden = 'true' }
61+
Body = $Request.Body
62+
Headers = $Request.Headers
63+
}
64+
$Response = Invoke-AddScheduledItem -Request $ForwardRequest -TriggerMetadata $TriggerMetadata
65+
66+
if ($tenantsJsonForStorage) {
67+
try {
68+
$Table = Get-CIPPTable -TableName 'ScheduledTasks'
69+
$null = Update-AzDataTableEntity -Force @Table -Entity @{
70+
PartitionKey = 'ScheduledTask'
71+
RowKey = [string]$Request.Body.RowKey
72+
Tenants = [string]$tenantsJsonForStorage
73+
}
74+
} catch {
75+
Write-Warning "Failed to persist multi-tenant selection for alert: $($_.Exception.Message)"
76+
}
77+
}
78+
79+
return $Response
80+
}

‎Modules/CIPPHTTP/Public/Entrypoints/HTTP Functions/Tenant/Administration/Alerts/Invoke-ListAlertsQueue.ps1‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,29 @@ function Invoke-ListAlertsQueue {
6565
$ExcludedTenants = @()
6666
}
6767

68-
# Handle tenant group display information for alerts
68+
# Handle tenant display information for alerts
6969
$TenantsForDisplay = @()
70-
if ($Task.TenantGroup) {
70+
if ($Task.Tenants) {
71+
# Multi tenant alert
72+
try {
73+
$TenantsParsed = $Task.Tenants | ConvertFrom-Json -Depth 10 -ErrorAction Stop
74+
$TenantsForDisplay = @($TenantsParsed | ForEach-Object {
75+
[PSCustomObject]@{
76+
label = $_.label ?? $_.value
77+
value = $_.value
78+
type = $_.type ?? 'Tenant'
79+
}
80+
})
81+
$ExcludedTenants = @()
82+
} catch {
83+
Write-Warning "Failed to parse Tenants for alert task $($Task.RowKey): $($_.Exception.Message)"
84+
$TenantsForDisplay = @([PSCustomObject]@{
85+
label = $Task.Tenant
86+
value = $Task.Tenant
87+
type = 'Tenant'
88+
})
89+
}
90+
} elseif ($Task.TenantGroup) {
7191
try {
7292
$TenantGroupObject = $Task.TenantGroup | ConvertFrom-Json -ErrorAction SilentlyContinue
7393
if ($TenantGroupObject) {

0 commit comments

Comments
 (0)