@@ -12,51 +12,90 @@ function Invoke-ExecAddMultiTenantApp {
1212 $APIName = $Request.Params.CIPPEndpoint
1313 $Headers = $Request.Headers
1414 Write-LogMessage - headers $Headers - API $APIName - message ' Accessed this API' - Sev ' Debug'
15- $DelegateResources = $request.body.permissions | Where-Object - Property origin -EQ ' Delegated' | ForEach-Object { @ { id = $_.id ; type = ' Scope' } }
16- $DelegateResourceAccess = @ { ResourceAppId = ' 00000003-0000-0000-c000-000000000000' ; resourceAccess = $DelegateResources }
17- $ApplicationResources = $request.body.permissions | Where-Object - Property origin -EQ ' Application' | ForEach-Object { @ { id = $_.id ; type = ' Role' } }
18- $ApplicationResourceAccess = @ { ResourceAppId = ' 00000003-0000-0000-c000-000000000000' ; resourceAccess = $ApplicationResources }
1915
20- $Results = try {
21- if ($Request.Body.CopyPermissions -eq $true ) {
22- $Command = ' ExecApplicationCopy'
23- } else {
24- $Command = ' ExecAddMultiTenantApp'
25- }
26- if (' allTenants' -in $Request.Body.tenantFilter.value ) {
27- $TenantFilter = (Get-Tenants ).defaultDomainName
28- } else {
29- $TenantFilter = $Request.Body.tenantFilter.value
30- }
16+ if ($Request.Body.configMode -eq ' manual' ) {
17+ $DelegateResources = $request.body.permissions | Where-Object - Property origin -EQ ' Delegated' | ForEach-Object { @ { id = $_.id ; type = ' Scope' } }
18+ $DelegateResourceAccess = @ { ResourceAppId = ' 00000003-0000-0000-c000-000000000000' ; resourceAccess = $DelegateResources }
19+ $ApplicationResources = $request.body.permissions | Where-Object - Property origin -EQ ' Application' | ForEach-Object { @ { id = $_.id ; type = ' Role' } }
20+ $ApplicationResourceAccess = @ { ResourceAppId = ' 00000003-0000-0000-c000-000000000000' ; resourceAccess = $ApplicationResources }
21+
22+ $Results = try {
23+ if ($Request.Body.CopyPermissions -eq $true ) {
24+ $Command = ' ExecApplicationCopy'
25+ } else {
26+ $Command = ' ExecAddMultiTenantApp'
27+ }
28+ if (' allTenants' -in $Request.Body.tenantFilter.value ) {
29+ $TenantFilter = (Get-Tenants ).defaultDomainName
30+ } else {
31+ $TenantFilter = $Request.Body.tenantFilter.value
32+ }
33+
34+ $TenantCount = ($TenantFilter | Measure-Object ).Count
35+ $Queue = New-CippQueueEntry - Name ' Application Approval' - TotalTasks $TenantCount
36+ $Batch = foreach ($Tenant in $TenantFilter ) {
37+ [pscustomobject ]@ {
38+ FunctionName = $Command
39+ Tenant = $tenant
40+ AppId = $Request.Body.AppId
41+ applicationResourceAccess = $ApplicationResourceAccess
42+ delegateResourceAccess = $DelegateResourceAccess
43+ QueueId = $Queue.RowKey
44+ }
45+ }
3146
32- $TenantCount = ($TenantFilter | Measure-Object ).Count
33- $Queue = New-CippQueueEntry - Name ' Application Approval' - TotalTasks $TenantCount
34- foreach ($Tenant in $TenantFilter ) {
3547 try {
3648 $InputObject = @ {
3749 OrchestratorName = ' ExecMultiTenantAppOrchestrator'
38- Batch = @ ([pscustomobject ]@ {
39- FunctionName = $Command
40- Tenant = $tenant
41- AppId = $Request.Body.AppId
42- applicationResourceAccess = $ApplicationResourceAccess
43- delegateResourceAccess = $DelegateResourceAccess
44- QueueId = $Queue.RowKey
45- })
50+ Batch = @ ($Batch )
4651 SkipLog = $true
4752 }
4853 $null = Start-NewOrchestration - FunctionName ' CIPPOrchestrator' - InputObject ($InputObject | ConvertTo-Json - Depth 5 - Compress)
49- " Queued application to tenant $Tenant . See the logbook for deployment details"
54+ $Results = ' Deploying {0} to {1}, see the logbook for details' -f $Request .Body.AppId , ( $Request .Body.tenantFilter.label -join ' , ' )
5055 } catch {
51- " Error queuing application to tenant $Tenant - $ ( $_.Exception.Message ) "
56+ $ErrorMsg = Get-NormalizedError - message $ ($_.Exception.Message )
57+ $Results = " Function Error: $ErrorMsg "
5258 }
59+
60+ $StatusCode = [HttpStatusCode ]::OK
61+ } catch {
62+ $ErrorMsg = Get-NormalizedError - message $ ($_.Exception.Message )
63+ $Results = " Function Error: $ErrorMsg "
64+ $StatusCode = [HttpStatusCode ]::BadRequest
65+ }
66+ } elseif ($Request.Body.configMode -eq ' template' ) {
67+ Write-Information ' Application Approval - Template Mode'
68+ if (' allTenants' -in $Request.Body.tenantFilter.value ) {
69+ $TenantFilter = (Get-Tenants ).defaultDomainName
70+ } else {
71+ $TenantFilter = $Request.Body.tenantFilter.value
72+ }
73+ $TenantCount = ($TenantFilter | Measure-Object ).Count
74+ $Queue = New-CippQueueEntry - Name ' Application Approval (Template)' - TotalTasks $TenantCount
75+
76+ $Batch = foreach ($Tenant in $TenantFilter ) {
77+ [pscustomobject ]@ {
78+ FunctionName = ' ExecAppApprovalTemplate'
79+ Tenant = $tenant
80+ TemplateId = $Request.Body.selectedTemplate.value
81+ AppId = $Request.Body.selectedTemplate.addedFields.AppId
82+ QueueId = $Queue.RowKey
83+ }
84+ }
85+ try {
86+ $InputObject = @ {
87+ OrchestratorName = ' ExecMultiTenantAppOrchestrator'
88+ Batch = @ ($Batch )
89+ SkipLog = $true
90+ }
91+ $null = Start-NewOrchestration - FunctionName ' CIPPOrchestrator' - InputObject ($InputObject | ConvertTo-Json - Depth 5 - Compress)
92+ $Results = ' Deploying {0} to {1}, see the logbook for details' -f $Request.Body.selectedTemplate.label , ($Request.Body.tenantFilter.label -join ' , ' )
93+ } catch {
94+ $Results = " Error queuing application - $ ( $_.Exception.Message ) "
5395 }
5496 $StatusCode = [HttpStatusCode ]::OK
55- } catch {
56- $ErrorMsg = Get-NormalizedError - message $ ($_.Exception.Message )
57- $Results = " Function Error: $ErrorMsg "
58- $StatusCode = [HttpStatusCode ]::BadRequest
5997 }
98+
6099 # Associate values to output bindings by calling 'Push-OutputBinding'.
61100 Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
62101 StatusCode = $StatusCode
0 commit comments