@@ -47,11 +47,34 @@ function Invoke-ListAlertsQueue {
4747 }
4848
4949 if ($AllowedTenants -notcontains ' AllTenants' ) {
50+ $HasAccess = $false
5051 foreach ($Tenant in $Tenants ) {
51- if ($AllowedTenants -contains $Tenant.customerId ) {
52- $AllTasksArrayList.Add ($TaskEntry )
53- break
52+ if ($Tenant.type -eq ' Group' ) {
53+ try {
54+ $GroupFilter = @ ([PSCustomObject ]@ {
55+ type = ' Group'
56+ value = $Tenant.value
57+ label = $Tenant.label
58+ })
59+ $ExpandedGroupTenants = Expand-CIPPTenantGroups - TenantFilter $GroupFilter
60+ foreach ($ExpandedTenant in $ExpandedGroupTenants ) {
61+ if ($AllowedTenants -contains $ExpandedTenant.addedFields.customerId ) {
62+ $HasAccess = $true
63+ break
64+ }
65+ }
66+ } catch {
67+ Write-Warning " Failed to expand tenant group for webhook access check: $ ( $_.Exception.Message ) "
68+ }
69+ } else {
70+ if ($AllowedTenants -contains $Tenant.customerId ) {
71+ $HasAccess = $true
72+ }
5473 }
74+ if ($HasAccess ) { break }
75+ }
76+ if ($HasAccess ) {
77+ $AllTasksArrayList.Add ($TaskEntry )
5578 }
5679 } else {
5780 $AllTasksArrayList.Add ($TaskEntry )
@@ -133,45 +156,68 @@ function Invoke-ListAlertsQueue {
133156 }
134157
135158 if ($AllowedTenants -notcontains ' AllTenants' ) {
136- # For tenant groups, we need to expand and check access
159+ $HasAccess = $false
137160 if ($Task.TenantGroup ) {
161+ # Expand legacy TenantGroup field and check access
138162 try {
139163 $TenantGroupObject = $Task.TenantGroup | ConvertFrom-Json - ErrorAction SilentlyContinue
140164 if ($TenantGroupObject ) {
141- # Create a tenant filter object for expansion
142165 $TenantFilterForExpansion = @ ([PSCustomObject ]@ {
143166 type = ' Group'
144167 value = $TenantGroupObject.value
145168 label = $TenantGroupObject.label
146169 })
147-
148- # Expand the tenant group to individual tenants
149170 $ExpandedTenants = Expand-CIPPTenantGroups - TenantFilter $TenantFilterForExpansion
150-
151- # Check if user has access to any tenant in the group
152- $HasAccess = $false
153171 foreach ($ExpandedTenant in $ExpandedTenants ) {
154172 $TenantInfo = $TenantList | Where-Object - Property defaultDomainName -EQ $ExpandedTenant.value
155173 if ($TenantInfo -and $AllowedTenants -contains $TenantInfo.customerId ) {
156174 $HasAccess = $true
157175 break
158176 }
159177 }
160-
161- if ($HasAccess ) {
162- $AllTasksArrayList.Add ($TaskEntry )
163- }
164178 }
165179 } catch {
166180 Write-Warning " Failed to expand tenant group for access check: $ ( $_.Exception.Message ) "
167181 }
182+ } elseif ($Task.Tenants ) {
183+ # Multi-tenant alert - may contain groups or individual tenants
184+ try {
185+ $TenantsParsed = $Task.Tenants | ConvertFrom-Json - ErrorAction Stop
186+ foreach ($TenantItem in $TenantsParsed ) {
187+ if ($TenantItem.type -eq ' Group' ) {
188+ $GroupFilter = @ ([PSCustomObject ]@ {
189+ type = ' Group'
190+ value = $TenantItem.value
191+ label = $TenantItem.label
192+ })
193+ $ExpandedGroupTenants = Expand-CIPPTenantGroups - TenantFilter $GroupFilter
194+ foreach ($ExpandedTenant in $ExpandedGroupTenants ) {
195+ if ($AllowedTenants -contains $ExpandedTenant.addedFields.customerId ) {
196+ $HasAccess = $true
197+ break
198+ }
199+ }
200+ } else {
201+ $TenantInfo = $TenantList | Where-Object - Property defaultDomainName -EQ $TenantItem.value
202+ if ($TenantInfo -and $AllowedTenants -contains $TenantInfo.customerId ) {
203+ $HasAccess = $true
204+ }
205+ }
206+ if ($HasAccess ) { break }
207+ }
208+ } catch {
209+ Write-Warning " Failed to parse Tenants for access check on task $ ( $Task.RowKey ) : $ ( $_.Exception.Message ) "
210+ }
168211 } else {
169- # Regular tenant access check
212+ # Regular single- tenant access check
170213 $Tenant = $TenantList | Where-Object - Property defaultDomainName -EQ $Task.Tenant
171214 if ($AllowedTenants -contains $Tenant.customerId ) {
172- $AllTasksArrayList .Add ( $TaskEntry )
215+ $HasAccess = $true
173216 }
174217 }
218+ if ($HasAccess ) {
219+ $AllTasksArrayList.Add ($TaskEntry )
220+ }
175221 } else {
176222 $AllTasksArrayList.Add ($TaskEntry )
177223 }
0 commit comments