@@ -10,51 +10,114 @@ function Invoke-ExecJITAdmin {
10
10
[CmdletBinding ()]
11
11
param ($Request , $TriggerMetadata )
12
12
13
- $APIName = ' ExecJITAdmin '
13
+ $APIName = $Request .Params.CIPPEndpoint
14
14
$User = $Request.Headers
15
- $TenantFilter = $Request.body.TenantFilter .value ? $Request.body.TenantFilter .value : $Request.body.TenantFilter
16
- Write-LogMessage - Headers $User - API $APINAME - message ' Accessed this API' - Sev ' Debug'
15
+ $TenantFilter = $Request.Body.tenantFilter .value ? $Request.Body.tenantFilter .value : $Request.Body.tenantFilter
16
+ Write-LogMessage - Headers $User - API $APIName - message ' Accessed this API' - Sev ' Debug'
17
17
18
18
if ($Request.Query.Action -eq ' List' ) {
19
19
$Schema = Get-CIPPSchemaExtensions | Where-Object { $_.id -match ' _cippUser' } | Select-Object - First 1
20
- $Query = @ {
21
- TenantFilter = $Request.Query.TenantFilter
22
- Endpoint = ' users'
23
- Parameters = @ {
24
- ' $count' = ' true'
25
- ' $select' = " id,accountEnabled,displayName,userPrincipalName,$ ( $Schema.id ) "
26
- ' $filter' = " $ ( $Schema.id ) /jitAdminEnabled eq true or $ ( $Schema.id ) /jitAdminEnabled eq false"
20
+ if ($Request.Query.TenantFilter -ne ' AllTenants' ) {
21
+ # Single tenant logic
22
+ $Query = @ {
23
+ TenantFilter = $Request.Query.TenantFilter
24
+ Endpoint = ' users'
25
+ Parameters = @ {
26
+ ' $count' = ' true'
27
+ ' $select' = " id,accountEnabled,displayName,userPrincipalName,$ ( $Schema.id ) "
28
+ ' $filter' = " $ ( $Schema.id ) /jitAdminEnabled eq true or $ ( $Schema.id ) /jitAdminEnabled eq false"
29
+ }
27
30
}
28
- }
29
- $Users = Get-GraphRequestList @Query | Where-Object { $_.id }
30
- $BulkRequests = $Users | ForEach-Object { @ (
31
- @ {
32
- id = $_.id
33
- method = ' GET'
34
- url = " users/$ ( $_.id ) /memberOf/microsoft.graph.directoryRole/?`$ select=id,displayName"
31
+ $Users = Get-GraphRequestList @Query | Where-Object { $_.id }
32
+ $BulkRequests = $Users | ForEach-Object { @ (
33
+ @ {
34
+ id = $_.id
35
+ method = ' GET'
36
+ url = " users/$ ( $_.id ) /memberOf/microsoft.graph.directoryRole/?`$ select=id,displayName"
37
+ }
38
+ )
39
+ }
40
+ $RoleResults = New-GraphBulkRequest - tenantid $Request.Query.TenantFilter - Requests @ ($BulkRequests )
41
+ # Write-Information ($RoleResults | ConvertTo-Json -Depth 10 )
42
+ $Results = $Users | ForEach-Object {
43
+ $MemberOf = ($RoleResults | Where-Object - Property id -EQ $_.id ).body.value | Select-Object displayName, id
44
+ [PSCustomObject ]@ {
45
+ id = $_.id
46
+ displayName = $_.displayName
47
+ userPrincipalName = $_.userPrincipalName
48
+ accountEnabled = $_.accountEnabled
49
+ jitAdminEnabled = $_ .($Schema.id ).jitAdminEnabled
50
+ jitAdminExpiration = $_ .($Schema.id ).jitAdminExpiration
51
+ memberOf = $MemberOf
35
52
}
36
- )
37
- }
38
- $RoleResults = New-GraphBulkRequest - tenantid $Request.Query.TenantFilter - Requests @ ($BulkRequests )
39
- # Write-Information ($RoleResults | ConvertTo-Json -Depth 10 )
40
- $Results = $Users | ForEach-Object {
41
- $MemberOf = ($RoleResults | Where-Object - Property id -EQ $_.id ).body.value | Select-Object displayName, id
42
- [PSCustomObject ]@ {
43
- id = $_.id
44
- displayName = $_.displayName
45
- userPrincipalName = $_.userPrincipalName
46
- accountEnabled = $_.accountEnabled
47
- jitAdminEnabled = $_ .($Schema.id ).jitAdminEnabled
48
- jitAdminExpiration = $_ .($Schema.id ).jitAdminExpiration
49
- memberOf = $MemberOf
50
53
}
51
- }
52
54
53
- # Write-Information ($Results | ConvertTo-Json -Depth 10)
54
- $Body = @ {
55
- Results = @ ($Results )
56
- Metadata = @ {
57
- Parameters = $Query.Parameters
55
+ # Write-Information ($Results | ConvertTo-Json -Depth 10)
56
+ $Body = @ {
57
+ Results = @ ($Results )
58
+ Metadata = @ {
59
+ Parameters = $Query.Parameters
60
+ }
61
+ }
62
+ } else {
63
+ # AllTenants logic
64
+ $Results = [System.Collections.Generic.List [object ]]::new()
65
+ $Metadata = @ {}
66
+ $Table = Get-CIPPTable - TableName CacheJITAdmin
67
+ $PartitionKey = ' JITAdminUser'
68
+ $Filter = " PartitionKey eq '$PartitionKey '"
69
+ $Rows = Get-CIPPAzDataTableEntity @Table - filter $Filter | Where-Object - Property Timestamp -GT (Get-Date ).AddMinutes(-60 )
70
+
71
+ $QueueReference = ' {0}-{1}' -f $Request.Query.TenantFilter , $PartitionKey # $TenantFilter is 'AllTenants'
72
+ Write-Information " QueueReference: $QueueReference "
73
+ $RunningQueue = Invoke-ListCippQueue | Where-Object { $_.Reference -eq $QueueReference -and $_.Status -notmatch ' Completed' -and $_.Status -notmatch ' Failed' }
74
+
75
+ if ($RunningQueue ) {
76
+ $Metadata = [PSCustomObject ]@ {
77
+ QueueMessage = ' Still loading JIT Admin data for all tenants. Please check back in a few more minutes.'
78
+ }
79
+ } elseif (! $Rows -and ! $RunningQueue ) {
80
+ $TenantList = Get-Tenants - IncludeErrors
81
+ $Queue = New-CippQueueEntry - Name ' JIT Admin List - All Tenants' - Link ' /identity/administration/jit-admin?tenantFilter=AllTenants' - Reference $QueueReference - TotalTasks ($TenantList | Measure-Object ).Count
82
+
83
+ $Metadata = [PSCustomObject ]@ {
84
+ QueueMessage = ' Loading JIT Admin data for all tenants. Please check back in a few minutes.'
85
+ }
86
+ $InputObject = [PSCustomObject ]@ {
87
+ OrchestratorName = ' JITAdminOrchestrator'
88
+ QueueFunction = @ {
89
+ FunctionName = ' GetTenants'
90
+ QueueId = $Queue.RowKey
91
+ TenantParams = @ {
92
+ IncludeErrors = $true
93
+ }
94
+ DurableName = ' ExecJITAdminListAllTenants'
95
+ }
96
+ SkipLog = $true
97
+ }
98
+ Start-NewOrchestration - FunctionName ' CIPPOrchestrator' - InputObject ($InputObject | ConvertTo-Json - Depth 5 - Compress)
99
+ } else {
100
+ # There is data in the cache, so we will use that
101
+ Write-Information " Found $ ( $Rows.Count ) rows in the cache"
102
+ foreach ($row in $Rows ) {
103
+ $UserObject = $row.JITAdminUser | ConvertFrom-Json
104
+ $Results.Add (
105
+ [PSCustomObject ]@ {
106
+ Tenant = $row.Tenant
107
+ id = $UserObject.id
108
+ displayName = $UserObject.displayName
109
+ userPrincipalName = $UserObject.userPrincipalName
110
+ accountEnabled = $UserObject.accountEnabled
111
+ jitAdminEnabled = $UserObject.jitAdminEnabled
112
+ jitAdminExpiration = $UserObject.jitAdminExpiration
113
+ memberOf = $UserObject.memberOf
114
+ }
115
+ )
116
+ }
117
+ }
118
+ $Body = @ {
119
+ Results = @ ($Results )
120
+ Metadata = $Metadata
58
121
}
59
122
}
60
123
} else {
0 commit comments