Skip to content

Commit 9148b20

Browse files
committed
added action to "Deploy to Custom Group" for authentication methods
1 parent b884185 commit 9148b20

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,47 @@ function Invoke-SetAuthMethod {
1515
$State = if ($Request.Body.state -eq 'enabled') { $true } else { $false }
1616
$TenantFilter = $Request.Body.tenantFilter
1717
$AuthenticationMethodId = $Request.Body.Id
18+
$GroupIdsRaw = $Request.Body.GroupIds
19+
20+
function Get-StandardizedList {
21+
param($InputObject)
22+
23+
if ($null -eq $InputObject) { return @() }
24+
25+
if ($InputObject -is [string]) {
26+
return @(
27+
$InputObject -split ',' |
28+
ForEach-Object { $_.Trim() } |
29+
Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
30+
)
31+
}
32+
33+
if ($InputObject -is [array] -or $InputObject -is [System.Collections.IEnumerable]) {
34+
return @(
35+
$InputObject |
36+
ForEach-Object { "$_".Trim() } |
37+
Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
38+
)
39+
}
40+
41+
return @("$InputObject".Trim()) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
42+
}
43+
44+
$GroupIds = Get-StandardizedList -InputObject $GroupIdsRaw
1845

1946

2047
try {
21-
$Result = Set-CIPPAuthenticationPolicy -Tenant $TenantFilter -APIName $APIName -AuthenticationMethodId $AuthenticationMethodId -Enabled $State -Headers $Headers
48+
$Params = @{
49+
Tenant = $TenantFilter
50+
APIName = $APIName
51+
AuthenticationMethodId = $AuthenticationMethodId
52+
Enabled = $State
53+
Headers = $Headers
54+
}
55+
if (@($GroupIds).Count -gt 0) {
56+
$Params.GroupIds = @($GroupIds)
57+
}
58+
$Result = Set-CIPPAuthenticationPolicy @Params
2259
$StatusCode = [HttpStatusCode]::OK
2360
} catch {
2461
$Result = $_.Exception.Message

Modules/CIPPCore/Public/Set-CIPPAuthenticationPolicy.ps1

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ function Set-CIPPAuthenticationPolicy {
1010
$TAPDefaultLifeTime = 60, #minutes
1111
$TAPDefaultLength = 8, #TAP password generated length in chars
1212
$TAPisUsableOnce = $true,
13+
[Parameter()][string[]]$GroupIds,
1314
[Parameter()][ValidateRange(1, 395)]$QRCodeLifetimeInDays = 365,
1415
[Parameter()][ValidateRange(8, 20)]$QRCodePinLength = 8,
1516
$APIName = 'Set Authentication Policy',
@@ -118,6 +119,40 @@ function Set-CIPPAuthenticationPolicy {
118119
throw "Somehow you hit the default case with an input of $AuthenticationMethodId . You probably made a typo in the input for AuthenticationMethodId. It`'s case sensitive."
119120
}
120121
}
122+
123+
if ($PSBoundParameters.ContainsKey('GroupIds') -and @($GroupIds).Count -gt 0) {
124+
$ResolvedGroupIds = @(
125+
@($GroupIds) |
126+
ForEach-Object { "$_".Trim() } |
127+
Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
128+
Select-Object -Unique
129+
)
130+
131+
if ($ResolvedGroupIds.Count -gt 0) {
132+
$TargetTemplate = $null
133+
if ($CurrentInfo.includeTargets -and @($CurrentInfo.includeTargets).Count -gt 0) {
134+
$TargetTemplate = $CurrentInfo.includeTargets | Select-Object -First 1
135+
}
136+
137+
$CurrentInfo.includeTargets = @(
138+
foreach ($GroupId in $ResolvedGroupIds) {
139+
$TargetProperties = [ordered]@{}
140+
if ($TargetTemplate) {
141+
foreach ($Property in $TargetTemplate.PSObject.Properties) {
142+
if ($Property.Name -ne 'id' -and $Property.Name -ne 'targetType') {
143+
$TargetProperties[$Property.Name] = $Property.Value
144+
}
145+
}
146+
}
147+
$TargetProperties.targetType = 'group'
148+
$TargetProperties.id = $GroupId
149+
[pscustomobject]$TargetProperties
150+
}
151+
)
152+
$OptionalLogMessage = "$OptionalLogMessage and targeted groups set to $($ResolvedGroupIds -join ', ')"
153+
}
154+
}
155+
121156
# Set state of the authentication method
122157
try {
123158
if ($PSCmdlet.ShouldProcess($AuthenticationMethodId, "Set state to $State $OptionalLogMessage")) {

0 commit comments

Comments
 (0)