Summary Adds a tenant-scoped Entra Groups “Directory Cache” to enable DB-only group name resolution across the app (no render-time Graph calls), plus sync runs + observability. What’s included • Entra Groups cache • New entra_groups storage (tenant-scoped) for group metadata (no memberships). • Retention semantics: groups become stale / retained per spec (no hard delete on first miss). • Group Sync Runs • New “Group Sync Runs” UI (list + detail) with tenant isolation (403 on cross-tenant access). • Manual “Sync Groups” action: creates/reuses a run, dispatches job, DB notification with “View run” link. • Scheduled dispatcher command wired in console.php. • DB-only label resolution (US3) • Shared EntraGroupLabelResolver with safe fallback Unresolved (…last8) and UUID guarding. • Refactors to prefer cached names (no typeahead / no live Graph) in: • Tenant RBAC group selects • Policy version assignments widget • Restore results + restore wizard group mapping labels Safety / Guardrails • No render-time Graph calls: fail-hard guard test verifies UI paths don’t call GraphClientInterface during page render. • Tenant isolation & authorization: policies + scoped queries enforced (cross-tenant access returns 403, not 404). • Data minimization: only group metadata is cached (no membership/owners). Tests / Verification • Added/updated tests under tests/Feature/DirectoryGroups and tests/Unit/DirectoryGroups: • Start sync → run record + job dispatch + upserts • Retention purge semantics • Scheduled dispatch wiring • Render-time Graph guard • UI/resource access isolation • Ran: • ./vendor/bin/pint --dirty • ./vendor/bin/sail artisan test tests/Feature/DirectoryGroups • ./vendor/bin/sail artisan test tests/Unit/DirectoryGroups Notes / Follow-ups • UI polish remains (picker/lookup UX, consistent progress widget/toasts across modules, navigation grouping). • pr-gate checklist still has non-blocking open items (mostly UX/ops polish); requirements gate is green. Co-authored-by: Ahmed Darrazi <ahmeddarrazi@adsmac.local> Reviewed-on: #57
140 lines
4.8 KiB
PHP
140 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\EntraGroupSyncRun;
|
|
use App\Models\Tenant;
|
|
use App\Services\Directory\EntraGroupSyncService;
|
|
use App\Services\Intune\AuditLogger;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use RuntimeException;
|
|
|
|
class EntraGroupSyncJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public function __construct(
|
|
public int $tenantId,
|
|
public string $selectionKey,
|
|
public ?string $slotKey = null,
|
|
public ?int $runId = null,
|
|
) {}
|
|
|
|
public function handle(EntraGroupSyncService $syncService, AuditLogger $auditLogger): void
|
|
{
|
|
$tenant = Tenant::query()->find($this->tenantId);
|
|
if (! $tenant instanceof Tenant) {
|
|
throw new RuntimeException('Tenant not found.');
|
|
}
|
|
|
|
$run = $this->resolveRun($tenant);
|
|
|
|
if ($run->status !== EntraGroupSyncRun::STATUS_PENDING) {
|
|
return;
|
|
}
|
|
|
|
$run->update([
|
|
'status' => EntraGroupSyncRun::STATUS_RUNNING,
|
|
'started_at' => CarbonImmutable::now('UTC'),
|
|
]);
|
|
|
|
$auditLogger->log(
|
|
tenant: $tenant,
|
|
action: 'directory_groups.sync.started',
|
|
context: [
|
|
'selection_key' => $run->selection_key,
|
|
'run_id' => $run->getKey(),
|
|
'slot_key' => $run->slot_key,
|
|
],
|
|
actorId: $run->initiator_user_id,
|
|
status: 'success',
|
|
resourceType: 'entra_group_sync_run',
|
|
resourceId: (string) $run->getKey(),
|
|
);
|
|
|
|
$result = $syncService->sync($tenant, $run);
|
|
|
|
$terminalStatus = EntraGroupSyncRun::STATUS_SUCCEEDED;
|
|
|
|
if ($result['error_code'] !== null) {
|
|
$terminalStatus = EntraGroupSyncRun::STATUS_FAILED;
|
|
} elseif ($result['safety_stop_triggered'] === true) {
|
|
$terminalStatus = EntraGroupSyncRun::STATUS_PARTIAL;
|
|
}
|
|
|
|
$run->update([
|
|
'status' => $terminalStatus,
|
|
'pages_fetched' => $result['pages_fetched'],
|
|
'items_observed_count' => $result['items_observed_count'],
|
|
'items_upserted_count' => $result['items_upserted_count'],
|
|
'error_count' => $result['error_count'],
|
|
'safety_stop_triggered' => $result['safety_stop_triggered'],
|
|
'safety_stop_reason' => $result['safety_stop_reason'],
|
|
'error_code' => $result['error_code'],
|
|
'error_category' => $result['error_category'],
|
|
'error_summary' => $result['error_summary'],
|
|
'finished_at' => CarbonImmutable::now('UTC'),
|
|
]);
|
|
|
|
$auditLogger->log(
|
|
tenant: $tenant,
|
|
action: $terminalStatus === EntraGroupSyncRun::STATUS_SUCCEEDED
|
|
? 'directory_groups.sync.succeeded'
|
|
: ($terminalStatus === EntraGroupSyncRun::STATUS_PARTIAL
|
|
? 'directory_groups.sync.partial'
|
|
: 'directory_groups.sync.failed'),
|
|
context: [
|
|
'selection_key' => $run->selection_key,
|
|
'run_id' => $run->getKey(),
|
|
'slot_key' => $run->slot_key,
|
|
'pages_fetched' => $run->pages_fetched,
|
|
'items_observed_count' => $run->items_observed_count,
|
|
'items_upserted_count' => $run->items_upserted_count,
|
|
'error_code' => $run->error_code,
|
|
'error_category' => $run->error_category,
|
|
],
|
|
actorId: $run->initiator_user_id,
|
|
status: $terminalStatus === EntraGroupSyncRun::STATUS_FAILED ? 'failed' : 'success',
|
|
resourceType: 'entra_group_sync_run',
|
|
resourceId: (string) $run->getKey(),
|
|
);
|
|
}
|
|
|
|
private function resolveRun(Tenant $tenant): EntraGroupSyncRun
|
|
{
|
|
if ($this->runId !== null) {
|
|
$run = EntraGroupSyncRun::query()
|
|
->whereKey($this->runId)
|
|
->where('tenant_id', $tenant->getKey())
|
|
->first();
|
|
|
|
if ($run instanceof EntraGroupSyncRun) {
|
|
return $run;
|
|
}
|
|
|
|
throw new RuntimeException('EntraGroupSyncRun not found.');
|
|
}
|
|
|
|
if ($this->slotKey !== null) {
|
|
$run = EntraGroupSyncRun::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('selection_key', $this->selectionKey)
|
|
->where('slot_key', $this->slotKey)
|
|
->first();
|
|
|
|
if ($run instanceof EntraGroupSyncRun) {
|
|
return $run;
|
|
}
|
|
|
|
throw new RuntimeException('EntraGroupSyncRun not found for slot.');
|
|
}
|
|
|
|
throw new RuntimeException('Job missing runId/slotKey.');
|
|
}
|
|
}
|