Skip to content

Commit e2fb8e4

Browse files
authored
Merge pull request #350 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents bf03e8b + 1e300e4 commit e2fb8e4

File tree

5 files changed

+92
-23
lines changed

5 files changed

+92
-23
lines changed

Modules/CIPPCore/Public/Add-CIPPScheduledTask.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ function Add-CIPPScheduledTask {
9999
$excludedTenants = if ($task.excludedTenants.value) {
100100
$task.excludedTenants.value -join ','
101101
}
102-
102+
103103
# Handle tenant filter - support both single tenant and tenant groups
104104
$tenantFilter = $task.TenantFilter.value ? $task.TenantFilter.value : $task.TenantFilter
105105
$originalTenantFilter = $task.TenantFilter
106-
106+
107107
# If tenant filter is a complex object (from form), extract the value
108108
if ($tenantFilter -is [PSCustomObject] -and $tenantFilter.value) {
109109
$originalTenantFilter = $tenantFilter
110110
$tenantFilter = $tenantFilter.value
111111
}
112-
112+
113113
# If tenant filter is a string but still seems to be JSON, try to parse it
114114
if ($tenantFilter -is [string] -and $tenantFilter.StartsWith('{')) {
115115
try {
@@ -123,7 +123,7 @@ function Add-CIPPScheduledTask {
123123
Write-Warning "Could not parse tenant filter JSON: $tenantFilter"
124124
}
125125
}
126-
126+
127127
$entity = @{
128128
PartitionKey = [string]'ScheduledTask'
129129
TaskState = [string]'Planned'
@@ -140,7 +140,7 @@ function Add-CIPPScheduledTask {
140140
Hidden = [bool]$Hidden
141141
Results = 'Planned'
142142
}
143-
143+
144144
# Store the original tenant filter for group expansion during execution
145145
if ($originalTenantFilter -is [PSCustomObject] -and $originalTenantFilter.type -eq 'Group') {
146146
$entity['TenantGroup'] = [string]($originalTenantFilter | ConvertTo-Json -Compress)

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Scheduler/Invoke-ListScheduledItemDetails.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function Invoke-ListScheduledItemDetails {
6666
$TenantGroupForDisplay = [PSCustomObject]@{
6767
label = $TenantGroupObject.label
6868
value = $TenantGroupObject.value
69-
type = 'Group'
69+
type = 'Group'
7070
}
7171
$Task | Add-Member -NotePropertyName TenantGroupInfo -NotePropertyValue $TenantGroupForDisplay -Force
7272
# Update the tenant to show the group object for proper formatting
@@ -81,7 +81,7 @@ function Invoke-ListScheduledItemDetails {
8181
$TenantForDisplay = [PSCustomObject]@{
8282
label = $Task.Tenant
8383
value = $Task.Tenant
84-
type = 'Tenant'
84+
type = 'Tenant'
8585
}
8686
$Task.Tenant = $TenantForDisplay
8787
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Scheduler/Invoke-ListScheduledItems.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,18 @@ function Invoke-ListScheduledItems {
8989
}
9090
$Task | Add-Member -NotePropertyName TenantGroupInfo -NotePropertyValue $TenantGroupForDisplay -Force
9191
# Update the tenant to show the group object for proper formatting
92-
$Task.Tenant = @($TenantGroupForDisplay)
92+
$Task.Tenant = $TenantGroupForDisplay
9393
}
9494
} catch {
9595
Write-Warning "Failed to parse tenant group information for task $($Task.RowKey): $($_.Exception.Message)"
9696
# Fall back to keeping original tenant value
9797
}
9898
} else {
99-
$Task.Tenant = @($Task.Tenant)
99+
$Task.Tenant = [PSCustomObject]@{
100+
label = $Task.Tenant
101+
value = $Task.Tenant
102+
type = 'Tenant'
103+
}
100104
}
101105

102106
$Task

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

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-ListAlertsQueue {
3+
function Invoke-ListAlertsQueue {
44
<#
55
.FUNCTIONALITY
66
Entrypoint
@@ -70,22 +70,87 @@ Function Invoke-ListAlertsQueue {
7070
$ExcludedTenants = @()
7171
}
7272

73+
# Handle tenant group display information for alerts
74+
$TenantsForDisplay = @()
75+
if ($Task.TenantGroup) {
76+
try {
77+
$TenantGroupObject = $Task.TenantGroup | ConvertFrom-Json -ErrorAction SilentlyContinue
78+
if ($TenantGroupObject) {
79+
# Create a tenant group object for display
80+
$TenantGroupForDisplay = [PSCustomObject]@{
81+
label = $TenantGroupObject.label
82+
value = $TenantGroupObject.value
83+
type = 'Group'
84+
}
85+
$TenantsForDisplay = @($TenantGroupForDisplay)
86+
}
87+
} catch {
88+
Write-Warning "Failed to parse tenant group information for alert task $($Task.RowKey): $($_.Exception.Message)"
89+
# Fall back to regular tenant display
90+
$TenantsForDisplay = @($Task.Tenant)
91+
}
92+
} else {
93+
# For regular tenants, create a tenant object for consistent formatting
94+
$TenantForDisplay = [PSCustomObject]@{
95+
label = $Task.Tenant
96+
value = $Task.Tenant
97+
type = 'Tenant'
98+
}
99+
$TenantsForDisplay = @($TenantForDisplay)
100+
}
101+
73102
$TaskEntry = [PSCustomObject]@{
74103
RowKey = $Task.RowKey
75104
PartitionKey = $Task.PartitionKey
76105
excludedTenants = @($ExcludedTenants)
77-
Tenants = @($Task.Tenant)
106+
Tenants = $TenantsForDisplay
78107
Conditions = $Task.Name
79108
Actions = $Task.PostExecution
80109
LogType = 'Scripted'
81110
EventType = 'Scheduled Task'
82111
RepeatsEvery = $Task.Recurrence
83112
RawAlert = $Task
84113
}
114+
85115
if ($AllowedTenants -notcontains 'AllTenants') {
86-
$Tenant = $TenantList | Where-Object -Property defaultDomainName -EQ $Task.Tenant
87-
if ($AllowedTenants -contains $Tenant.customerId) {
88-
$AllTasksArrayList.Add($TaskEntry)
116+
# For tenant groups, we need to expand and check access
117+
if ($Task.TenantGroup) {
118+
try {
119+
$TenantGroupObject = $Task.TenantGroup | ConvertFrom-Json -ErrorAction SilentlyContinue
120+
if ($TenantGroupObject) {
121+
# Create a tenant filter object for expansion
122+
$TenantFilterForExpansion = @([PSCustomObject]@{
123+
type = 'Group'
124+
value = $TenantGroupObject.value
125+
label = $TenantGroupObject.label
126+
})
127+
128+
# Expand the tenant group to individual tenants
129+
$ExpandedTenants = Expand-CIPPTenantGroups -TenantFilter $TenantFilterForExpansion
130+
131+
# Check if user has access to any tenant in the group
132+
$HasAccess = $false
133+
foreach ($ExpandedTenant in $ExpandedTenants) {
134+
$TenantInfo = $TenantList | Where-Object -Property defaultDomainName -EQ $ExpandedTenant.value
135+
if ($TenantInfo -and $AllowedTenants -contains $TenantInfo.customerId) {
136+
$HasAccess = $true
137+
break
138+
}
139+
}
140+
141+
if ($HasAccess) {
142+
$AllTasksArrayList.Add($TaskEntry)
143+
}
144+
}
145+
} catch {
146+
Write-Warning "Failed to expand tenant group for access check: $($_.Exception.Message)"
147+
}
148+
} else {
149+
# Regular tenant access check
150+
$Tenant = $TenantList | Where-Object -Property defaultDomainName -EQ $Task.Tenant
151+
if ($AllowedTenants -contains $Tenant.customerId) {
152+
$AllTasksArrayList.Add($TaskEntry)
153+
}
89154
}
90155
} else {
91156
$AllTasksArrayList.Add($TaskEntry)

Modules/CIPPCore/Public/Entrypoints/Orchestrator Functions/Start-UserTasksOrchestrator.ps1

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,20 @@ function Start-UserTasksOrchestrator {
5555
try {
5656
$TenantGroupObject = $task.TenantGroup | ConvertFrom-Json
5757
Write-Host "Expanding tenant group: $($TenantGroupObject.label) with ID: $($TenantGroupObject.value)"
58-
58+
5959
# Create a tenant filter object for expansion
6060
$TenantFilterForExpansion = @([PSCustomObject]@{
61-
type = 'Group'
62-
value = $TenantGroupObject.value
63-
label = $TenantGroupObject.label
64-
})
65-
61+
type = 'Group'
62+
value = $TenantGroupObject.value
63+
label = $TenantGroupObject.label
64+
})
65+
6666
# Expand the tenant group to individual tenants
6767
$ExpandedTenants = Expand-CIPPTenantGroups -TenantFilter $TenantFilterForExpansion
68-
68+
6969
$ExcludedTenants = $task.excludedTenants -split ','
7070
Write-Host "Excluded Tenants from this task: $ExcludedTenants"
71-
71+
7272
$GroupTenantCommands = foreach ($ExpandedTenant in $ExpandedTenants | Where-Object { $_.value -notin $ExcludedTenants }) {
7373
$NewParams = $task.Parameters.Clone()
7474
if ((Get-Command $task.Command).Parameters.TenantFilter) {
@@ -85,7 +85,7 @@ function Start-UserTasksOrchestrator {
8585
} catch {
8686
Write-Host "Error expanding tenant group: $($_.Exception.Message)"
8787
Write-LogMessage -API 'Scheduler_UserTasks' -tenant $tenant -message "Failed to expand tenant group for task $($task.Name): $($_.Exception.Message)" -sev Error
88-
88+
8989
# Fall back to treating as single tenant
9090
if ((Get-Command $task.Command).Parameters.TenantFilter) {
9191
$ScheduledCommand.Parameters['TenantFilter'] = $task.Tenant

0 commit comments

Comments
 (0)