Skip to content

Commit 253ffa2

Browse files
authored
Merge pull request #624 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 8b49e52 + 5c4fe80 commit 253ffa2

File tree

3 files changed

+143
-4
lines changed

3 files changed

+143
-4
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function Invoke-AddDomain {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
Tenant.Administration.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
$APIName = $Request.Params.CIPPEndpoint
12+
$TenantFilter = $Request.Body.tenantFilter
13+
$DomainName = $Request.Body.domain
14+
15+
# Interact with query parameters or the body of the request.
16+
try {
17+
if ([string]::IsNullOrWhiteSpace($DomainName)) {
18+
throw 'Domain name is required'
19+
}
20+
21+
# Validate domain name format
22+
if ($DomainName -notmatch '^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$') {
23+
throw 'Invalid domain name format'
24+
}
25+
26+
Write-Information "Adding domain $DomainName to tenant $TenantFilter"
27+
28+
$Body = @{
29+
id = $DomainName
30+
} | ConvertTo-Json -Compress
31+
32+
$GraphRequest = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/beta/domains' -tenantid $TenantFilter -type POST -body $Body -AsApp $true
33+
34+
$Result = "Successfully added domain $DomainName to tenant $TenantFilter. Please verify the domain to complete setup."
35+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Added domain $DomainName" -Sev 'Info'
36+
$StatusCode = [HttpStatusCode]::OK
37+
} catch {
38+
$ErrorMessage = Get-CippException -Exception $_
39+
$Result = "Failed to add domain $DomainName`: $($ErrorMessage.NormalizedError)"
40+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Failed to add domain $DomainName`: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
41+
$StatusCode = [HttpStatusCode]::Forbidden
42+
}
43+
44+
return ([HttpResponseContext]@{
45+
StatusCode = $StatusCode
46+
Body = @{'Results' = $Result }
47+
})
48+
49+
}
50+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
function Invoke-ExecDomainAction {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
Tenant.Administration.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
$APIName = $Request.Params.CIPPEndpoint
12+
$TenantFilter = $Request.Body.tenantFilter
13+
$DomainName = $Request.Body.domain
14+
$Action = $Request.Body.Action
15+
16+
try {
17+
if ([string]::IsNullOrWhiteSpace($DomainName)) {
18+
throw 'Domain name is required'
19+
}
20+
21+
if ([string]::IsNullOrWhiteSpace($Action)) {
22+
throw 'Action is required'
23+
}
24+
25+
switch ($Action) {
26+
'verify' {
27+
Write-Information "Verifying domain $DomainName for tenant $TenantFilter"
28+
29+
$Body = @{
30+
verificationDnsRecordCollection = @()
31+
} | ConvertTo-Json -Compress
32+
33+
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/domains/$DomainName/verify" -tenantid $TenantFilter -type POST -body $Body -AsApp $true
34+
35+
$Result = @{
36+
resultText = "Domain $DomainName has been verified successfully."
37+
state = 'success'
38+
}
39+
40+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Verified domain $DomainName" -Sev 'Info'
41+
$StatusCode = [HttpStatusCode]::OK
42+
}
43+
'delete' {
44+
Write-Information "Deleting domain $DomainName from tenant $TenantFilter"
45+
46+
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/domains/$DomainName" -tenantid $TenantFilter -type DELETE -AsApp $true
47+
48+
$Result = @{
49+
resultText = "Domain $DomainName has been deleted successfully."
50+
state = 'success'
51+
}
52+
53+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Deleted domain $DomainName" -Sev 'Info'
54+
$StatusCode = [HttpStatusCode]::OK
55+
}
56+
'setDefault' {
57+
Write-Information "Setting domain $DomainName as default for tenant $TenantFilter"
58+
59+
$Body = @{
60+
isDefault = $true
61+
} | ConvertTo-Json -Compress
62+
63+
$GraphRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/domains/$DomainName" -tenantid $TenantFilter -type PATCH -body $Body -AsApp $true
64+
65+
$Result = @{
66+
resultText = "Domain $DomainName has been set as the default domain successfully."
67+
state = 'success'
68+
}
69+
70+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Set domain $DomainName as default" -Sev 'Info'
71+
$StatusCode = [HttpStatusCode]::OK
72+
}
73+
default {
74+
throw "Invalid action: $Action"
75+
}
76+
}
77+
} catch {
78+
$ErrorMessage = Get-CippException -Exception $_
79+
$Result = @{
80+
resultText = "Failed to perform action on domain $DomainName`: $($ErrorMessage.NormalizedError)"
81+
state = 'error'
82+
}
83+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Failed to perform action on domain $DomainName`: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
84+
$StatusCode = [HttpStatusCode]::Forbidden
85+
}
86+
87+
return ([HttpResponseContext]@{
88+
StatusCode = $StatusCode
89+
Body = @{'Results' = $Result }
90+
})
91+
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/GDAP/Invoke-ExecGDAPInvite.ps1

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ function Invoke-ExecGDAPInvite {
1111
$APIName = $Request.Params.CIPPEndpoint
1212
$Headers = $Request.Headers
1313

14-
15-
1614
$Action = $Request.Body.Action ?? $Request.Query.Action ?? 'Create'
1715
$InviteId = $Request.Body.InviteId
1816
$Reference = $Request.Body.Reference
@@ -23,8 +21,8 @@ function Invoke-ExecGDAPInvite {
2321
$user = $headers.'x-ms-client-principal'
2422
$Technician = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($user)) | ConvertFrom-Json).userDetails
2523
} elseif ($Headers.'x-ms-client-principal-idp' -eq 'aad') {
26-
$Table = Get-CIPPTable -TableName 'ApiClients'
27-
$Client = Get-CIPPAzDataTableEntity @Table -Filter "RowKey eq '$($headers.'x-ms-client-principal-name')'"
24+
$ApiClientTable = Get-CIPPTable -TableName 'ApiClients'
25+
$Client = Get-CIPPAzDataTableEntity @ApiClientTable -Filter "RowKey eq '$($headers.'x-ms-client-principal-name')'"
2826
$Technician = $Client.AppName ?? 'CIPP-API'
2927
} else {
3028
try {

0 commit comments

Comments
 (0)