Skip to content

Commit 773baba

Browse files
authored
Merge pull request #445 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 5763997 + 1de164f commit 773baba

File tree

5 files changed

+464
-165
lines changed

5 files changed

+464
-165
lines changed

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

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -19,80 +19,15 @@ function Invoke-AddGroup {
1919

2020
$Results = foreach ($tenant in $SelectedTenants) {
2121
try {
22-
$Email = if ($GroupObject.primDomain.value) { "$($GroupObject.username)@$($GroupObject.primDomain.value)" } else { "$($GroupObject.username)@$($tenant)" }
23-
if ($GroupObject.groupType -in 'Generic', 'azurerole', 'dynamic', 'm365') {
22+
# Use the centralized New-CIPPGroup function
23+
$Result = New-CIPPGroup -GroupObject $GroupObject -TenantFilter $tenant -APIName $APIName -ExecutingUser $Request.Headers.'x-ms-client-principal-name'
2424

25-
$BodyParams = [pscustomobject] @{
26-
'displayName' = $GroupObject.displayName
27-
'description' = $GroupObject.description
28-
'mailNickname' = $GroupObject.username
29-
mailEnabled = [bool]$false
30-
securityEnabled = [bool]$true
31-
isAssignableToRole = [bool]($GroupObject | Where-Object -Property groupType -EQ 'AzureRole')
32-
}
33-
if ($GroupObject.membershipRules) {
34-
$BodyParams | Add-Member -NotePropertyName 'membershipRule' -NotePropertyValue ($GroupObject.membershipRules)
35-
$BodyParams | Add-Member -NotePropertyName 'membershipRuleProcessingState' -NotePropertyValue 'On'
36-
if ($GroupObject.groupType -eq 'm365') {
37-
$BodyParams | Add-Member -NotePropertyName 'groupTypes' -NotePropertyValue @('Unified', 'DynamicMembership')
38-
$BodyParams.mailEnabled = $true
39-
} else {
40-
$BodyParams | Add-Member -NotePropertyName 'groupTypes' -NotePropertyValue @('DynamicMembership')
41-
}
42-
# Skip adding static members if we're using dynamic membership
43-
$SkipStaticMembers = $true
44-
} elseif ($GroupObject.groupType -eq 'm365') {
45-
$BodyParams | Add-Member -NotePropertyName 'groupTypes' -NotePropertyValue @('Unified')
46-
$BodyParams.mailEnabled = $true
47-
}
48-
if ($GroupObject.owners) {
49-
$BodyParams | Add-Member -NotePropertyName '[email protected]' -NotePropertyValue (($GroupObject.owners) | ForEach-Object { "https://graph.microsoft.com/v1.0/users/$($_.value)" })
50-
$BodyParams.'[email protected]' = @($BodyParams.'[email protected]')
51-
}
52-
if ($GroupObject.members -and -not $SkipStaticMembers) {
53-
$BodyParams | Add-Member -NotePropertyName '[email protected]' -NotePropertyValue (($GroupObject.members) | ForEach-Object { "https://graph.microsoft.com/v1.0/users/$($_.value)" })
54-
$BodyParams.'[email protected]' = @($BodyParams.'[email protected]')
55-
}
56-
$GraphRequest = New-GraphPostRequest -uri 'https://graph.microsoft.com/beta/groups' -tenantid $tenant -type POST -body (ConvertTo-Json -InputObject $BodyParams -Depth 10) -Verbose
25+
if ($Result.Success) {
26+
"Successfully created group $($GroupObject.displayName) for $($tenant)"
27+
$StatusCode = [HttpStatusCode]::OK
5728
} else {
58-
if ($GroupObject.groupType -eq 'dynamicDistribution') {
59-
$ExoParams = @{
60-
Name = $GroupObject.displayName
61-
RecipientFilter = $GroupObject.membershipRules
62-
PrimarySmtpAddress = $Email
63-
}
64-
$GraphRequest = New-ExoRequest -tenantid $tenant -cmdlet 'New-DynamicDistributionGroup' -cmdParams $ExoParams
65-
66-
if (!$GroupObject.allowExternal) {
67-
$SetParams = @{
68-
RequireSenderAuthenticationEnabled = [bool]!$GroupObject.allowExternal
69-
Name = $GroupObject.displayName
70-
PrimarySmtpAddress = $Email
71-
}
72-
$GraphRequest = New-ExoRequest -tenantid $tenant -cmdlet 'Set-DynamicDistributionGroup' -cmdParams $SetParams
73-
}
74-
} else {
75-
$ExoParams = @{
76-
Name = $GroupObject.displayName
77-
Alias = $GroupObject.username
78-
Description = $GroupObject.description
79-
PrimarySmtpAddress = $Email
80-
Type = $GroupObject.groupType
81-
RequireSenderAuthenticationEnabled = [bool]!$GroupObject.allowExternal
82-
}
83-
if ($GroupObject.owners) {
84-
$ExoParams.ManagedBy = @($GroupObject.owners.value)
85-
}
86-
if ($GroupObject.members) {
87-
$ExoParams.Members = @($GroupObject.members.value)
88-
}
89-
$GraphRequest = New-ExoRequest -tenantid $tenant -cmdlet 'New-DistributionGroup' -cmdParams $ExoParams
90-
}
29+
throw $Result.Message
9130
}
92-
93-
"Successfully created group $($GroupObject.displayName) for $($tenant)"
94-
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $tenant -message "Created group $($GroupObject.displayName) with id $($GraphRequest.id)" -Sev Info
95-
$StatusCode = [HttpStatusCode]::OK
9631
} catch {
9732
$ErrorMessage = Get-CippException -Exception $_
9833
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $tenant -message "Group creation API failed. $($ErrorMessage.NormalizedError)" -Sev Error -LogData $ErrorMessage

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,41 @@ function Invoke-AddGroupTemplate {
1515

1616
$GUID = $Request.Body.GUID ?? (New-Guid).GUID
1717
try {
18-
if (!$Request.Body.displayname) { throw 'You must enter a displayname' }
19-
$groupType = switch -wildcard ($Request.Body.groupType) {
20-
'*dynamic*' { 'dynamic' }
21-
'*azurerole*' { 'azurerole' }
22-
'*unified*' { 'm365' }
23-
'*Microsoft*' { 'm365' }
24-
'*generic*' { 'generic' }
25-
'*mail*' { 'mailenabledsecurity' }
26-
'*Distribution*' { 'distribution' }
27-
'*security*' { 'security' }
18+
if (!$Request.Body.displayName) {
19+
throw 'You must enter a displayname'
20+
}
21+
22+
# Normalize group type to match New-CIPPGroup expectations (handle both camelCase and lowercase)
23+
$groupType = switch -wildcard ($Request.Body.groupType.ToLower()) {
24+
'*dynamicdistribution*' { 'dynamicDistribution'; break } # Check this first before *dynamic* and *distribution*
25+
'*dynamic*' { 'dynamic'; break }
26+
'*azurerole*' { 'azureRole'; break }
27+
'*unified*' { 'm365'; break }
28+
'*microsoft*' { 'm365'; break }
29+
'*m365*' { 'm365'; break }
30+
'*generic*' { 'generic'; break }
31+
'*security*' { 'security'; break }
32+
'*distribution*' { 'distribution'; break }
33+
'*mail*' { 'distribution'; break }
2834
default { $Request.Body.groupType }
2935
}
30-
if ($Request.body.membershipRules) { $groupType = 'dynamic' }
36+
37+
# Override to dynamic if membership rules are provided (for backward compatibility)
38+
# but only if it's not already a dynamicDistribution group
39+
if ($Request.body.membershipRules -and $groupType -notin @('dynamicDistribution')) {
40+
$groupType = 'dynamic'
41+
}
42+
# Normalize field names to handle different casing from various forms
43+
$displayName = $Request.Body.displayName ?? $Request.Body.Displayname ?? $Request.Body.displayname
44+
$description = $Request.Body.description ?? $Request.Body.Description
45+
3146
$object = [PSCustomObject]@{
32-
displayName = $Request.Body.displayName
33-
description = $Request.Body.description
47+
displayName = $displayName
48+
description = $description
3449
groupType = $groupType
3550
membershipRules = $Request.Body.membershipRules
3651
allowExternal = $Request.Body.allowExternal
37-
username = $Request.Body.username
52+
username = $Request.Body.username # Can contain variables like @%tenantfilter%
3853
GUID = $GUID
3954
} | ConvertTo-Json
4055
$Table = Get-CippTable -tablename 'templates'
@@ -44,7 +59,7 @@ function Invoke-AddGroupTemplate {
4459
RowKey = "$GUID"
4560
PartitionKey = 'GroupTemplate'
4661
}
47-
Write-LogMessage -headers $Request.Headers -API $APINAME -message "Created Group template named $($Request.Body.displayname) with GUID $GUID" -Sev 'Debug'
62+
Write-LogMessage -headers $Request.Headers -API $APINAME -message "Created Group template named $displayName with GUID $GUID" -Sev 'Debug'
4863

4964
$body = [pscustomobject]@{'Results' = 'Successfully added template' }
5065
} catch {

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,26 @@ function Invoke-ListGroupTemplates {
2222
$Filter = "PartitionKey eq 'GroupTemplate'"
2323
$Templates = (Get-CIPPAzDataTableEntity @Table -Filter $Filter) | ForEach-Object {
2424
$data = $_.JSON | ConvertFrom-Json
25+
26+
# Normalize groupType to camelCase for consistent frontend handling
27+
$normalizedGroupType = switch -Wildcard ($data.groupType.ToLower()) {
28+
'*dynamicdistribution*' { 'dynamicDistribution'; break }
29+
'*dynamic*' { 'dynamic'; break }
30+
'*azurerole*' { 'azureRole'; break }
31+
'*unified*' { 'm365'; break }
32+
'*microsoft*' { 'm365'; break }
33+
'*m365*' { 'm365'; break }
34+
'*generic*' { 'generic'; break }
35+
'*security*' { 'security'; break }
36+
'*distribution*' { 'distribution'; break }
37+
'*mail*' { 'distribution'; break }
38+
default { $data.groupType }
39+
}
40+
2541
[PSCustomObject]@{
2642
displayName = $data.displayName
2743
description = $data.description
28-
groupType = $data.groupType
44+
groupType = $normalizedGroupType
2945
membershipRules = $data.membershipRules
3046
allowExternal = $data.allowExternal
3147
username = $data.username

0 commit comments

Comments
 (0)