Skip to content

Commit c1c492f

Browse files
authored
Merge pull request #133 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 16d6be0 + d2f28eb commit c1c492f

File tree

9 files changed

+434
-7
lines changed

9 files changed

+434
-7
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
function Invoke-ExecCippReplacemap {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
CIPP.Extension.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
$Table = Get-CippTable -tablename 'CippReplacemap'
12+
$Action = $Request.Query.Action ?? $Request.Body.Action
13+
$customerId = $Request.Query.customerId ?? $Request.Body.customerId
14+
15+
if (!$customerId) {
16+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
17+
StatusCode = [HttpStatusCode]::BadRequest
18+
Body = 'customerId is required'
19+
})
20+
return
21+
}
22+
23+
switch ($Action) {
24+
'List' {
25+
$Variables = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq '$customerId'"
26+
if (!$Variables) {
27+
$Variables = @()
28+
}
29+
$Body = @{ Results = @($Variables) }
30+
}
31+
'AddEdit' {
32+
$VariableName = $Request.Body.RowKey
33+
$VariableValue = $Request.Body.Value
34+
35+
$VariableEntity = @{
36+
PartitionKey = $customerId
37+
RowKey = $VariableName
38+
Value = $VariableValue
39+
}
40+
41+
Add-CIPPAzDataTableEntity @Table -Entity $VariableEntity -Force
42+
$Body = @{ Results = "Variable '$VariableName' saved successfully" }
43+
}
44+
'Delete' {
45+
$VariableName = $Request.Body.RowKey
46+
47+
$VariableEntity = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq '$customerId' and RowKey eq '$VariableName'"
48+
if ($VariableEntity) {
49+
Remove-AzDataTableEntity @Table -Entity $VariableEntity -Force
50+
$Body = @{ Results = "Variable '$VariableName' deleted successfully" }
51+
} else {
52+
$Body = @{ Results = "Variable '$VariableName' not found" }
53+
}
54+
}
55+
default {
56+
$Body = @{ Results = 'Invalid action' }
57+
}
58+
}
59+
60+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
61+
StatusCode = [HttpStatusCode]::OK
62+
Body = $Body
63+
})
64+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
function Invoke-ExecTenantGroup {
2+
<#
3+
.SYNOPSIS
4+
Entrypoint for tenant group management
5+
.DESCRIPTION
6+
This function is used to manage tenant groups in CIPP
7+
.FUNCTIONALITY
8+
Entrypoint,AnyTenant
9+
.ROLE
10+
Tenant.Config.ReadWrite
11+
#>
12+
[CmdletBinding()]
13+
param($Request, $TriggerMetadata)
14+
15+
$Table = Get-CippTable -tablename 'TenantGroups'
16+
$MembersTable = Get-CippTable -tablename 'TenantGroupMembers'
17+
$Action = $Request.Body.Action
18+
$groupId = $Request.Body.groupId ?? [guid]::NewGuid().ToString()
19+
$groupName = $Request.Body.groupName
20+
$groupDescription = $Request.Body.groupDescription
21+
$members = $Request.Body.members
22+
23+
switch ($Action) {
24+
'AddEdit' {
25+
$Results = [System.Collections.Generic.List[object]]::new()
26+
# Update group details
27+
$GroupEntity = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'TenantGroup' and RowKey eq '$groupId'"
28+
if ($GroupEntity) {
29+
if ($groupName) {
30+
$GroupEntity.Name = $groupName
31+
}
32+
if ($groupDescription) {
33+
$GroupEntity.Description = $groupDescription
34+
}
35+
Add-CIPPAzDataTableEntity @Table -Entity $GroupEntity -Force
36+
} else {
37+
$GroupEntity = @{
38+
PartitionKey = 'TenantGroup'
39+
RowKey = $groupId
40+
Name = $groupName
41+
Description = $groupDescription
42+
}
43+
Add-CIPPAzDataTableEntity @Table -Entity $GroupEntity -Force
44+
}
45+
46+
$CurrentMembers = Get-CIPPAzDataTableEntity @MembersTable -Filter "GroupId eq '$groupId'"
47+
48+
$Adds = [System.Collections.Generic.List[string]]::new()
49+
$Removes = [System.Collections.Generic.List[string]]::new()
50+
# Add members
51+
foreach ($member in $members) {
52+
if ($CurrentMembers) {
53+
$CurrentMember = $CurrentMembers | Where-Object { $_.customerId -eq $member.value }
54+
if ($CurrentMember) {
55+
continue
56+
}
57+
}
58+
$MemberEntity = @{
59+
PartitionKey = 'Member'
60+
RowKey = '{0}-{1}' -f $groupId, $member.value
61+
GroupId = $groupId
62+
customerId = $member.value
63+
}
64+
Add-CIPPAzDataTableEntity @MembersTable -Entity $MemberEntity -Force
65+
$Adds.Add('Added member {0}' -f $member.label)
66+
}
67+
68+
if ($CurrentMembers) {
69+
foreach ($CurrentMember in $CurrentMembers) {
70+
if ($members.value -notcontains $CurrentMember.customerId) {
71+
Remove-AzDataTableEntity @MembersTable -Entity $CurrentMember -Force
72+
$Removes.Add('Removed member {0}' -f $CurrentMember.customerId)
73+
}
74+
}
75+
}
76+
$Results.Add(@{
77+
resultText = "Group '$groupName' saved successfully"
78+
state = 'success'
79+
})
80+
foreach ($Add in $Adds) {
81+
$Results.Add(@{
82+
resultText = $Add
83+
state = 'success'
84+
})
85+
}
86+
foreach ($Remove in $Removes) {
87+
$Results.Add(@{
88+
resultText = $Remove
89+
state = 'success'
90+
})
91+
}
92+
93+
$Body = @{ Results = $Results }
94+
}
95+
'Delete' {
96+
# Delete group
97+
$GroupEntity = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'TenantGroup' and RowKey eq '$groupId'"
98+
if ($GroupEntity) {
99+
Remove-AzDataTableEntity @Table -Entity $GroupEntity -Force
100+
$Body = @{ Results = "Group '$($GroupEntity.Name)' deleted successfully" }
101+
} else {
102+
$Body = @{ Results = "Group '$groupId' not found" }
103+
}
104+
}
105+
default {
106+
$Body = @{ Results = 'Invalid action' }
107+
}
108+
}
109+
110+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
111+
StatusCode = [HttpStatusCode]::OK
112+
Body = $Body
113+
})
114+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function Invoke-ListTenantGroups {
2+
<#
3+
.SYNOPSIS
4+
Entrypoint for listing tenant groups
5+
.FUNCTIONALITY
6+
Entrypoint,AnyTenant
7+
.ROLE
8+
CIPP.Core.Read
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$groupFilter = $Request.Query.groupId ?? $Request.Body.groupId
14+
$TenantGroups = (Get-TenantGroups -GroupId $groupFilter) ?? @()
15+
$Body = @{ Results = @($TenantGroups) }
16+
17+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
18+
StatusCode = [HttpStatusCode]::OK
19+
Body = $Body
20+
})
21+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using namespace System.Net
2+
3+
Function Invoke-EditTenant {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint,AnyTenant
7+
.ROLE
8+
Tenant.Config.ReadWrite
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $Request.Params.CIPPEndpoint
14+
15+
Write-LogMessage -headers $Request.Headers -API $APINAME -message 'Accessed this API' -Sev 'Debug'
16+
17+
$customerId = $Request.Body.customerId
18+
$tenantAlias = $Request.Body.tenantAlias
19+
$tenantGroups = $Request.Body.tenantGroups
20+
21+
$PropertiesTable = Get-CippTable -TableName 'TenantProperties'
22+
$Existing = Get-CIPPAzDataTableEntity @PropertiesTable -Filter "PartitionKey eq '$customerId'"
23+
$Tenant = Get-Tenants -TenantFilter $customerId
24+
$TenantTable = Get-CippTable -TableName 'Tenants'
25+
$GroupMembersTable = Get-CippTable -TableName 'TenantGroupMembers'
26+
27+
try {
28+
$AliasEntity = $Existing | Where-Object { $_.RowKey -eq 'Alias' }
29+
if (!$tenantAlias) {
30+
if ($AliasEntity) {
31+
Write-Host 'Removing alias'
32+
Remove-AzDataTableEntity @PropertiesTable -Entity $AliasEntity
33+
$null = Get-Tenants -TenantFilter $customerId -TriggerRefresh
34+
}
35+
} else {
36+
$aliasEntity = @{
37+
PartitionKey = $customerId
38+
RowKey = 'Alias'
39+
Value = $tenantAlias
40+
}
41+
$null = Add-CIPPAzDataTableEntity @PropertiesTable -Entity $aliasEntity -Force
42+
Write-Host "Setting alias to $tenantAlias"
43+
$Tenant.displayName = $tenantAlias
44+
$null = Add-CIPPAzDataTableEntity @TenantTable -Entity $Tenant -Force
45+
}
46+
47+
# Update tenant groups
48+
$CurrentMembers = Get-CIPPAzDataTableEntity @GroupMembersTable -Filter "customerId eq '$customerId'"
49+
foreach ($Group in $tenantGroups) {
50+
$GroupEntity = $CurrentMembers | Where-Object { $_.GroupId -eq $Group.groupId }
51+
if (!$GroupEntity) {
52+
$GroupEntity = @{
53+
PartitionKey = 'Member'
54+
RowKey = '{0}-{1}' -f $Group.groupId, $customerId
55+
GroupId = $Group.groupId
56+
customerId = $customerId
57+
}
58+
Add-CIPPAzDataTableEntity @GroupMembersTable -Entity $GroupEntity -Force
59+
}
60+
}
61+
62+
# Remove any groups that are no longer selected
63+
foreach ($Group in $CurrentMembers) {
64+
if ($tenantGroups -notcontains $Group.GroupId) {
65+
Remove-AzDataTableEntity @GroupMembersTable -Entity $Group
66+
}
67+
}
68+
69+
$response = @{
70+
state = 'success'
71+
resultText = 'Tenant details updated successfully'
72+
}
73+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
74+
StatusCode = [HttpStatusCode]::OK
75+
Body = $response
76+
})
77+
} catch {
78+
Write-LogMessage -headers $Request.Headers -tenant $customerId -API $APINAME -message "Edit Tenant failed. The error is: $($_.Exception.Message)" -Sev 'Error'
79+
$response = @{
80+
state = 'error'
81+
resultText = $_.Exception.Message
82+
}
83+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
84+
StatusCode = [HttpStatusCode]::InternalServerError
85+
Body = $response
86+
})
87+
}
88+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ Function Invoke-ListTenantDetails {
2222
@{ Name = 'technicalNotificationMails'; Expression = { $_.technicalNotificationMails -join ', ' } },
2323
tenantType, createdDateTime, onPremisesLastPasswordSyncDateTime, onPremisesLastSyncDateTime, onPremisesSyncEnabled, assignedPlans
2424

25+
$customProperties = Get-TenantProperties -customerId $tenantfilter
26+
$org | Add-Member -MemberType NoteProperty -Name 'customProperties' -Value $customProperties
27+
28+
$Groups = (Get-TenantGroups -TenantFilter $tenantfilter) ?? @()
29+
$org | Add-Member -MemberType NoteProperty -Name 'Groups' -Value @($Groups)
30+
31+
2532
# Respond with the successful output
2633
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
2734
StatusCode = [HttpStatusCode]::OK
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
function Get-TenantGroups {
2+
<#
3+
.SYNOPSIS
4+
Get tenant groups
5+
.DESCRIPTION
6+
Get tenant groups from Azure Table Storage
7+
.PARAMETER GroupId
8+
The group id to filter on
9+
.PARAMETER TenantFilter
10+
The tenant filter to apply to get the groups for a specific tenant
11+
#>
12+
[CmdletBinding()]
13+
param(
14+
$GroupId,
15+
$TenantFilter
16+
)
17+
18+
$GroupTable = Get-CippTable -tablename 'TenantGroups'
19+
$MembersTable = Get-CippTable -tablename 'TenantGroupMembers'
20+
21+
if ($TenantFilter) {
22+
$TenantParams = @{
23+
TenantFilter = $TenantFilter
24+
IncludeErrors = $true
25+
}
26+
} else {
27+
$TenantParams = @{
28+
IncludeErrors = $true
29+
}
30+
}
31+
$Tenants = Get-Tenants @TenantParams
32+
33+
if ($GroupFilter) {
34+
$Groups = Get-CIPPAzDataTableEntity @GroupTable -Filter "RowKey eq '$GroupFilter'"
35+
$AllMembers = Get-CIPPAzDataTableEntity @MembersTable -Filter "GroupId eq '$GroupFilter'"
36+
} else {
37+
$Groups = Get-CIPPAzDataTableEntity @GroupTable
38+
$AllMembers = Get-CIPPAzDataTableEntity @MembersTable
39+
}
40+
41+
if (!$Groups) {
42+
return @()
43+
}
44+
45+
if ($TenantFilter) {
46+
$Memberships = $AllMembers | Where-Object { $_.customerId -eq $Tenants.customerId }
47+
foreach ($Group in $Memberships) {
48+
$Group = $Groups | Where-Object { $_.RowKey -eq $Group.GroupId }
49+
if ($Group) {
50+
[PSCustomObject]@{
51+
Id = $Group.RowKey
52+
Name = $Group.Name
53+
Description = $Group.Description
54+
}
55+
}
56+
}
57+
} else {
58+
$Groups | ForEach-Object {
59+
$Group = $_
60+
$Members = $AllMembers | Where-Object { $_.GroupId -eq $Group.RowKey }
61+
if (!$Members) {
62+
$Members = @()
63+
}
64+
65+
$Members = $Members | ForEach-Object {
66+
$Member = $_
67+
$Tenant = $Tenants | Where-Object { $Member.customerId -eq $_.customerId }
68+
if ($Tenant) {
69+
@{
70+
customerId = $Tenant.customerId
71+
displayName = $Tenant.displayName
72+
defaultDomainName = $Tenant.defaultDomainName
73+
}
74+
}
75+
}
76+
if (!$Members) {
77+
$Members = @()
78+
}
79+
80+
[PSCustomObject]@{
81+
Id = $Group.RowKey
82+
Name = $Group.Name
83+
Description = $Group.Description
84+
Members = @($Members)
85+
}
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)