Skip to content

Commit 2ca12dd

Browse files
authored
Merge pull request #262 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 84a5b65 + dc0df58 commit 2ca12dd

File tree

9 files changed

+301
-87
lines changed

9 files changed

+301
-87
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
function Push-ExecJITAdminListAllTenants {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
#>
6+
param($Item)
7+
8+
$Tenant = Get-Tenants -TenantFilter $Item.customerId
9+
$DomainName = $Tenant.defaultDomainName
10+
$Table = Get-CIPPTable -TableName CacheJITAdmin
11+
12+
try {
13+
# Get schema extensions
14+
$Schema = Get-CIPPSchemaExtensions | Where-Object { $_.id -match '_cippUser' } | Select-Object -First 1
15+
16+
# Query users with JIT Admin enabled
17+
$Query = @{
18+
TenantFilter = $DomainName # Use $DomainName for the current tenant
19+
Endpoint = 'users'
20+
Parameters = @{
21+
'$count' = 'true'
22+
'$select' = "id,accountEnabled,displayName,userPrincipalName,$($Schema.id)"
23+
'$filter' = "$($Schema.id)/jitAdminEnabled eq true or $($Schema.id)/jitAdminEnabled eq false" # Fetches both states to cache current status
24+
}
25+
}
26+
$Users = Get-GraphRequestList @Query | Where-Object { $_.id }
27+
28+
if ($Users) {
29+
# Get role memberships
30+
$BulkRequests = $Users | ForEach-Object { @(
31+
@{
32+
id = $_.id
33+
method = 'GET'
34+
url = "users/$($_.id)/memberOf/microsoft.graph.directoryRole/?`$select=id,displayName"
35+
}
36+
)
37+
}
38+
# Ensure $BulkRequests is not empty or null before making the bulk request
39+
if ($BulkRequests -and $BulkRequests.Count -gt 0) {
40+
$RoleResults = New-GraphBulkRequest -tenantid $DomainName -Requests @($BulkRequests)
41+
42+
# Format the data
43+
$Results = $Users | ForEach-Object {
44+
$currentUser = $_ # Capture current user in the loop
45+
$MemberOf = @() # Initialize as empty array
46+
if ($RoleResults) {
47+
$userRoleResult = $RoleResults | Where-Object -Property id -EQ $currentUser.id
48+
if ($userRoleResult -and $userRoleResult.body -and $userRoleResult.body.value) {
49+
$MemberOf = $userRoleResult.body.value | Select-Object displayName, id
50+
}
51+
}
52+
53+
$jitAdminData = $currentUser.($Schema.id)
54+
$jitAdminEnabled = if ($jitAdminData -and $jitAdminData.PSObject.Properties['jitAdminEnabled']) { $jitAdminData.jitAdminEnabled } else { $false }
55+
$jitAdminExpiration = if ($jitAdminData -and $jitAdminData.PSObject.Properties['jitAdminExpiration']) { $jitAdminData.jitAdminExpiration } else { $null }
56+
57+
[PSCustomObject]@{
58+
id = $currentUser.id
59+
displayName = $currentUser.displayName
60+
userPrincipalName = $currentUser.userPrincipalName
61+
accountEnabled = $currentUser.accountEnabled
62+
jitAdminEnabled = $jitAdminEnabled
63+
jitAdminExpiration = $jitAdminExpiration
64+
memberOf = ($MemberOf | ConvertTo-Json -Depth 5 -Compress)
65+
}
66+
}
67+
68+
# Add to Azure Table
69+
foreach ($result in $Results) {
70+
$GUID = (New-Guid).Guid
71+
Write-Host ($result | ConvertTo-Json -Depth 10 -Compress)
72+
$GraphRequest = @{
73+
JITAdminUser = [string]($result | ConvertTo-Json -Depth 10 -Compress)
74+
RowKey = [string]$GUID
75+
PartitionKey = 'JITAdminUser'
76+
Tenant = [string]$DomainName
77+
UserId = [string]$result.id # Add UserId for easier querying if needed
78+
UserUPN = [string]$result.userPrincipalName # Add UserUPN for easier querying
79+
}
80+
Add-CIPPAzDataTableEntity @Table -Entity $GraphRequest -Force | Out-Null
81+
}
82+
} else {
83+
# No users with JIT Admin attributes found, or no users at all
84+
Write-Host "No JIT Admin users or no users found to process for tenant $DomainName."
85+
}
86+
} else {
87+
Write-Host "No users found for tenant $DomainName."
88+
}
89+
90+
} catch {
91+
$GUID = (New-Guid).Guid
92+
$ErrorMessage = "Could not process JIT Admin users for Tenant: $($DomainName). Error: $($_.Exception.Message)"
93+
if ($_.ScriptStackTrace) {
94+
$ErrorMessage += " StackTrace: $($_.ScriptStackTrace)"
95+
}
96+
$ErrorJson = ConvertTo-Json -InputObject @{
97+
Tenant = $DomainName
98+
Error = $ErrorMessage
99+
Exception = ($_.Exception.Message | ConvertTo-Json -Depth 3 -Compress)
100+
Timestamp = (Get-Date).ToString('s')
101+
}
102+
$GraphRequest = @{
103+
JITAdminUser = [string]$ErrorJson
104+
RowKey = [string]$GUID
105+
PartitionKey = 'JITAdminUser'
106+
Tenant = [string]$DomainName
107+
}
108+
Add-CIPPAzDataTableEntity @Table -Entity $GraphRequest -Force | Out-Null
109+
Write-Error ('Error processing JIT Admin for {0}: {1}' -f $DomainName, $_.Exception.Message)
110+
}
111+
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Core/Invoke-ExecAddAlert.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ function Invoke-ExecAddAlert {
1717
$Severity = 'Alert'
1818

1919
$Result = if ($Request.Body.sendEmailNow -or $Request.Body.sendWebhookNow -eq $true -or $Request.Body.writeLog -eq $true -or $Request.Body.sendPsaNow -eq $true) {
20+
$sev = ([pscustomobject]$Request.body.Severity).value -join (',')
21+
if ($Request.body.email -or $Request.body.webhook) {
22+
Write-Host 'found config, setting'
23+
$config = @{
24+
email = $Request.body.email
25+
webhook = $Request.body.webhook
26+
onepertenant = $Request.body.onePerTenant
27+
logsToInclude = $Request.body.logsToInclude
28+
sendtoIntegration = $true
29+
sev = $sev
30+
}
31+
Write-Host "setting notification config to $($config | ConvertTo-Json)"
32+
$Results = Set-cippNotificationConfig @Config
33+
Write-Host $Results
34+
}
2035
$Title = 'CIPP Notification Test'
2136
if ($Request.Body.sendEmailNow -eq $true) {
2237
$CIPPAlert = @{

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Settings/Invoke-ExecNotificationConfig.ps1

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,16 @@ Function Invoke-ExecNotificationConfig {
1414
$Headers = $Request.Headers
1515
Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
1616

17-
18-
1917
$sev = ([pscustomobject]$Request.body.Severity).value -join (',')
20-
$results = try {
21-
$Table = Get-CIPPTable -TableName SchedulerConfig
22-
$SchedulerConfig = @{
23-
'tenant' = 'Any'
24-
'tenantid' = 'TenantId'
25-
'type' = 'CIPPNotifications'
26-
'schedule' = 'Every 15 minutes'
27-
'Severity' = [string]$sev
28-
'email' = "$($Request.Body.email)"
29-
'webhook' = "$($Request.Body.webhook)"
30-
'onePerTenant' = [boolean]$Request.Body.onePerTenant
31-
'sendtoIntegration' = [boolean]$Request.Body.sendtoIntegration
32-
'includeTenantId' = [boolean]$Request.Body.includeTenantId
33-
'PartitionKey' = 'CippNotifications'
34-
'RowKey' = 'CippNotifications'
35-
}
36-
foreach ($logvalue in [pscustomobject]$Request.body.logsToInclude) {
37-
$SchedulerConfig[([pscustomobject]$logvalue.value)] = $true
38-
}
39-
40-
Add-CIPPAzDataTableEntity @Table -Entity $SchedulerConfig -Force | Out-Null
41-
'Successfully set the configuration'
42-
} catch {
43-
"Failed to set configuration: $($_.Exception.message)"
18+
$config = @{
19+
email = $Request.body.email
20+
webhook = $Request.body.webhook
21+
onepertenant = $Request.body.onePerTenant
22+
logsToInclude = $Request.body.logsToInclude
23+
sendtoIntegration = $Request.body.sendtoIntegration
24+
sev = $sev
4425
}
45-
46-
26+
$Results = Set-cippNotificationConfig @Config
4727
$body = [pscustomobject]@{'Results' = $Results }
4828

4929
# Associate values to output bindings by calling 'Push-OutputBinding'.

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Administration/Groups/Invoke-AddGroupTemplate.ps1

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

3-
Function Invoke-AddGroupTemplate {
3+
function Invoke-AddGroupTemplate {
44
<#
55
.FUNCTIONALITY
66
Entrypoint,AnyTenant
@@ -13,27 +13,27 @@ Function Invoke-AddGroupTemplate {
1313
$Headers = $Request.Headers
1414
Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
1515

16-
$GUID = (New-Guid).GUID
16+
$GUID = $Request.Body.GUID ?? (New-Guid).GUID
1717
try {
18-
if (!$Request.body.displayname) { throw 'You must enter a displayname' }
18+
if (!$Request.Body.displayname) { throw 'You must enter a displayname' }
1919

2020
$object = [PSCustomObject]@{
21-
Displayname = $request.body.displayName
22-
Description = $request.body.description
23-
groupType = $request.body.groupType
24-
MembershipRules = $request.body.membershipRules
25-
allowExternal = $request.body.allowExternal
26-
username = $request.body.username
21+
displayName = $Request.Body.displayName
22+
description = $Request.Body.description
23+
groupType = $Request.Body.groupType
24+
membershipRules = $Request.Body.membershipRules
25+
allowExternal = $Request.Body.allowExternal
26+
username = $Request.Body.username
2727
GUID = $GUID
2828
} | ConvertTo-Json
2929
$Table = Get-CippTable -tablename 'templates'
3030
$Table.Force = $true
31-
Add-CIPPAzDataTableEntity @Table -Entity @{
31+
Add-CIPPAzDataTableEntity @Table -Force -Entity @{
3232
JSON = "$object"
3333
RowKey = "$GUID"
3434
PartitionKey = 'GroupTemplate'
3535
}
36-
Write-LogMessage -headers $Request.Headers -API $APINAME -message "Created Group template named $($Request.body.displayname) with GUID $GUID" -Sev 'Debug'
36+
Write-LogMessage -headers $Request.Headers -API $APINAME -message "Created Group template named $($Request.Body.displayname) with GUID $GUID" -Sev 'Debug'
3737

3838
$body = [pscustomobject]@{'Results' = 'Successfully added template' }
3939
} catch {

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Administration/Groups/Invoke-ListGroupTemplates.ps1

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

3-
Function Invoke-ListGroupTemplates {
3+
function Invoke-ListGroupTemplates {
44
<#
55
.FUNCTIONALITY
66
Entrypoint,AnyTenant
@@ -22,8 +22,15 @@ Function Invoke-ListGroupTemplates {
2222
$Filter = "PartitionKey eq 'GroupTemplate'"
2323
$Templates = (Get-CIPPAzDataTableEntity @Table -Filter $Filter) | ForEach-Object {
2424
$data = $_.JSON | ConvertFrom-Json
25-
$data | Add-Member -MemberType NoteProperty -Name GUID -Value $_.RowKey -Force
26-
$data
25+
[PSCustomObject]@{
26+
displayName = $data.displayName
27+
description = $data.description
28+
groupType = $data.groupType
29+
membershipRules = $data.membershipRules
30+
allowExternal = $data.allowExternal
31+
username = $data.username
32+
GUID = $_.RowKey
33+
}
2734
} | Sort-Object -Property displayName
2835

2936
if ($Request.query.ID) { $Templates = $Templates | Where-Object -Property GUID -EQ $Request.query.id }

0 commit comments

Comments
 (0)