fix: allow nested compliance action expand

This commit is contained in:
Ahmed Darrazi 2025-12-28 23:08:09 +01:00
parent 89b1dfb78e
commit 59c74246d6
4 changed files with 32 additions and 9 deletions

View File

@ -25,22 +25,41 @@ public function sanitizeQuery(string $policyType, array $query): array
$allowedExpand = $contract['allowed_expand'] ?? []; $allowedExpand = $contract['allowed_expand'] ?? [];
$warnings = []; $warnings = [];
if (! empty($query['$select']) && is_array($query['$select'])) { if (! empty($query['$select'])) {
$original = $query['$select']; $original = $query['$select'];
$query['$select'] = array_values(array_intersect($original, $allowedSelect)); $select = is_array($original)
? $original
: array_map('trim', explode(',', (string) $original));
$filtered = array_values(array_intersect($select, $allowedSelect));
if (count($query['$select']) !== count($original)) { if (count($filtered) !== count($select)) {
$warnings[] = 'Trimmed unsupported $select fields for capability safety.'; $warnings[] = 'Trimmed unsupported $select fields for capability safety.';
} }
if ($filtered === []) {
unset($query['$select']);
} else {
$query['$select'] = implode(',', $filtered);
}
} }
if (! empty($query['$expand']) && is_array($query['$expand'])) { if (! empty($query['$expand'])) {
$original = $query['$expand']; $original = $query['$expand'];
$query['$expand'] = array_values(array_intersect($original, $allowedExpand)); $expand = is_array($original)
? $original
: [trim((string) $original)];
$expand = array_values(array_filter($expand, static fn ($value) => $value !== ''));
$filtered = array_values(array_intersect($expand, $allowedExpand));
if (count($query['$expand']) !== count($original)) { if (count($filtered) !== count($expand)) {
$warnings[] = 'Trimmed unsupported $expand fields for capability safety.'; $warnings[] = 'Trimmed unsupported $expand fields for capability safety.';
} }
if ($filtered === []) {
unset($query['$expand']);
} else {
$query['$expand'] = implode(',', $filtered);
}
} }
return [ return [

View File

@ -47,7 +47,7 @@ public function fetch(Tenant $tenant, Policy $policy, ?string $actorEmail = null
]; ];
if ($policy->policy_type === 'deviceCompliancePolicy') { if ($policy->policy_type === 'deviceCompliancePolicy') {
$options['expand'] = ['scheduledActionsForRule']; $options['expand'] = 'scheduledActionsForRule($expand=scheduledActionConfigurations)';
} }
$response = $this->graphClient->getPolicy($policy->policy_type, $policy->external_id, $options); $response = $this->graphClient->getPolicy($policy->policy_type, $policy->external_id, $options);

View File

@ -15,7 +15,10 @@
'deviceConfiguration' => [ 'deviceConfiguration' => [
'resource' => 'deviceManagement/deviceConfigurations', 'resource' => 'deviceManagement/deviceConfigurations',
'allowed_select' => ['id', 'displayName', 'description', '@odata.type', 'version', 'lastModifiedDateTime'], 'allowed_select' => ['id', 'displayName', 'description', '@odata.type', 'version', 'lastModifiedDateTime'],
'allowed_expand' => ['scheduledActionsForRule'], 'allowed_expand' => [
'scheduledActionsForRule',
'scheduledActionsForRule($expand=scheduledActionConfigurations)',
],
'type_family' => [ 'type_family' => [
'#microsoft.graph.deviceConfiguration', '#microsoft.graph.deviceConfiguration',
'#microsoft.graph.windows10CustomConfiguration', '#microsoft.graph.windows10CustomConfiguration',

View File

@ -104,5 +104,6 @@ public function request(string $method, string $path, array $options = []): Grap
expect($client->requests[0][0])->toBe('getPolicy'); expect($client->requests[0][0])->toBe('getPolicy');
expect($client->requests[0][1])->toBe('deviceCompliancePolicy'); expect($client->requests[0][1])->toBe('deviceCompliancePolicy');
expect($client->requests[0][2])->toBe('compliance-123'); expect($client->requests[0][2])->toBe('compliance-123');
expect($client->requests[0][3]['expand'] ?? [])->toBe(['scheduledActionsForRule']); expect($client->requests[0][3]['expand'] ?? null)
->toBe('scheduledActionsForRule($expand=scheduledActionConfigurations)');
}); });