TenantAtlas/app/Services/Graph/GroupResolver.php
Ahmed Darrazi 86bb4cdbd6 feat: Phase 1+2 - Assignments & Scope Tags foundation
Phase 1: Setup & Database (13 tasks completed)
- Add assignments JSONB column to backup_items table
- Add group_mapping JSONB column to restore_runs table
- Extend BackupItem model with 7 assignment accessor methods
- Extend RestoreRun model with 8 group mapping helper methods
- Add scopeWithAssignments() query scope to BackupItem
- Update graph_contracts.php with assignments endpoints
- Create 5 factories: BackupItem, RestoreRun, Tenant, BackupSet, Policy
- Add 30 unit tests (15 BackupItem, 15 RestoreRun) - all passing

Phase 2: Graph API Integration (16 tasks completed)
- Create AssignmentFetcher service with fallback strategy
- Create GroupResolver service with orphaned ID handling
- Create ScopeTagResolver service with 1-hour caching
- Implement fail-soft error handling for all services
- Add 17 unit tests (5 AssignmentFetcher, 6 GroupResolver, 6 ScopeTagResolver) - all passing
- Total: 71 assertions across all Phase 2 tests

Test Results:
- Phase 1: 30/30 tests passing (45 assertions)
- Phase 2: 17/17 tests passing (71 assertions)
- Total: 47/47 tests passing (116 assertions)
- Code formatted with Pint (PSR-12 compliant)

Next: Phase 3 - US1 Backup with Assignments (12 tasks)
2025-12-22 02:10:35 +01:00

121 lines
3.6 KiB
PHP

<?php
namespace App\Services\Graph;
use Illuminate\Support\Facades\Cache;
class GroupResolver
{
public function __construct(
private readonly MicrosoftGraphClient $graphClient,
private readonly GraphLogger $logger,
) {}
/**
* Resolve group IDs to group objects with display names.
*
* Uses POST /directoryObjects/getByIds endpoint.
* Missing IDs are marked as orphaned.
*
* @param array $groupIds Array of group IDs to resolve
* @param string $tenantId Target tenant ID
* @return array Keyed array: ['group-id' => ['id' => ..., 'displayName' => ..., 'orphaned' => bool]]
*/
public function resolveGroupIds(array $groupIds, string $tenantId): array
{
if (empty($groupIds)) {
return [];
}
// Create cache key
$cacheKey = $this->getCacheKey($groupIds, $tenantId);
return Cache::remember($cacheKey, 300, function () use ($groupIds, $tenantId) {
return $this->fetchAndResolveGroups($groupIds, $tenantId);
});
}
/**
* Fetch groups from Graph API and resolve orphaned IDs.
*/
private function fetchAndResolveGroups(array $groupIds, string $tenantId): array
{
try {
$response = $this->graphClient->post(
'/directoryObjects/getByIds',
[
'ids' => array_values($groupIds),
'types' => ['group'],
],
$tenantId
);
$resolvedGroups = $response['value'] ?? [];
// Create result map
$result = [];
$resolvedIds = [];
// Add resolved groups
foreach ($resolvedGroups as $group) {
$groupId = $group['id'];
$resolvedIds[] = $groupId;
$result[$groupId] = [
'id' => $groupId,
'displayName' => $group['displayName'] ?? null,
'orphaned' => false,
];
}
// Add orphaned groups (not in response)
foreach ($groupIds as $groupId) {
if (! in_array($groupId, $resolvedIds)) {
$result[$groupId] = [
'id' => $groupId,
'displayName' => null,
'orphaned' => true,
];
}
}
$this->logger->logDebug('Resolved group IDs', [
'tenant_id' => $tenantId,
'requested' => count($groupIds),
'resolved' => count($resolvedIds),
'orphaned' => count($groupIds) - count($resolvedIds),
]);
return $result;
} catch (GraphException $e) {
$this->logger->logWarning('Failed to resolve group IDs', [
'tenant_id' => $tenantId,
'group_ids' => $groupIds,
'error' => $e->getMessage(),
'context' => $e->context,
]);
// Return all as orphaned on failure
$result = [];
foreach ($groupIds as $groupId) {
$result[$groupId] = [
'id' => $groupId,
'displayName' => null,
'orphaned' => true,
];
}
return $result;
}
}
/**
* Generate cache key for group resolution.
*/
private function getCacheKey(array $groupIds, string $tenantId): string
{
sort($groupIds);
return "groups:{$tenantId}:".md5(implode(',', $groupIds));
}
}