|
| 1 | +function Invoke-ListCustomDataMappings { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint |
| 5 | + .ROLE |
| 6 | + CIPP.Core.Read |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
| 10 | + |
| 11 | + $CustomDataMappingsTable = Get-CippTable -TableName 'CustomDataMappings' |
| 12 | + $TenantFilter = $Request.Query.tenantFilter |
| 13 | + $SourceTypeFilter = $Request.Query.sourceType |
| 14 | + $DirectoryObjectFilter = $Request.Query.directoryObject |
| 15 | + |
| 16 | + Write-Information "Listing custom data mappings with filters - sourceType: $SourceTypeFilter, directoryObject: $DirectoryObjectFilter, tenant: $TenantFilter" |
| 17 | + |
| 18 | + try { |
| 19 | + $Mappings = Get-CIPPAzDataTableEntity @CustomDataMappingsTable | ForEach-Object { |
| 20 | + $Mapping = $_.JSON | ConvertFrom-Json -AsHashtable |
| 21 | + |
| 22 | + # Filter by tenant |
| 23 | + $TenantList = Expand-CIPPTenantGroups -TenantFilter $Mapping.tenantFilter |
| 24 | + if ($TenantFilter -and ($TenantList -contains $TenantFilter -or $TenantList -eq 'AllTenants')) { |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + $MappingObject = [PSCustomObject]@{ |
| 29 | + id = $_.RowKey |
| 30 | + tenant = $Mapping.tenantFilter.label |
| 31 | + dataset = $Mapping.extensionSyncDataset.label |
| 32 | + sourceType = $Mapping.sourceType.label |
| 33 | + directoryObject = $Mapping.directoryObjectType.label |
| 34 | + syncProperty = $Mapping.extensionSyncProperty.label ?? @($Mapping.extensionSyncDataset.addedFields.select -split ',') |
| 35 | + customDataAttribute = $Mapping.customDataAttribute |
| 36 | + manualEntryFieldLabel = $Mapping.manualEntryFieldLabel |
| 37 | + } |
| 38 | + |
| 39 | + # Apply safe filtering |
| 40 | + $Include = $true |
| 41 | + if ($SourceTypeFilter -and $MappingObject.sourceType -ne $SourceTypeFilter) { |
| 42 | + $Include = $false |
| 43 | + } |
| 44 | + if ($DirectoryObjectFilter -and $MappingObject.directoryObject -ne $DirectoryObjectFilter) { |
| 45 | + $Include = $false |
| 46 | + } |
| 47 | + |
| 48 | + if ($Include) { |
| 49 | + return $MappingObject |
| 50 | + } |
| 51 | + } | Where-Object { $_ -ne $null } |
| 52 | + |
| 53 | + $Body = @{ |
| 54 | + Results = @($Mappings) |
| 55 | + } |
| 56 | + } catch { |
| 57 | + $Body = @{ |
| 58 | + Results = @( |
| 59 | + @{ |
| 60 | + state = 'error' |
| 61 | + resultText = "Failed to retrieve mappings: $($_.Exception.Message)" |
| 62 | + } |
| 63 | + ) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return ([HttpResponseContext]@{ |
| 68 | + StatusCode = [HttpStatusCode]::OK |
| 69 | + Body = $Body |
| 70 | + }) |
| 71 | +} |
0 commit comments