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
248 lines
7.1 KiB
PHP
248 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\BulkOperationRun;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Intune\AuditLogger;
|
|
|
|
class BulkOperationService
|
|
{
|
|
public function __construct(
|
|
protected AuditLogger $auditLogger
|
|
) {}
|
|
|
|
public function sanitizeFailureReason(string $reason): string
|
|
{
|
|
$reason = trim($reason);
|
|
|
|
if ($reason === '') {
|
|
return 'error';
|
|
}
|
|
|
|
$lower = mb_strtolower($reason);
|
|
|
|
if (
|
|
str_contains($lower, 'bearer ') ||
|
|
str_contains($lower, 'access_token') ||
|
|
str_contains($lower, 'client_secret') ||
|
|
str_contains($lower, 'authorization')
|
|
) {
|
|
return 'redacted';
|
|
}
|
|
|
|
$reason = preg_replace("/\s+/u", ' ', $reason) ?? $reason;
|
|
|
|
return mb_substr($reason, 0, 200);
|
|
}
|
|
|
|
public function createRun(
|
|
Tenant $tenant,
|
|
User $user,
|
|
string $resource,
|
|
string $action,
|
|
array $itemIds,
|
|
int $totalItems
|
|
): BulkOperationRun {
|
|
$effectiveTotalItems = max($totalItems, count($itemIds));
|
|
|
|
$run = BulkOperationRun::create([
|
|
'tenant_id' => $tenant->id,
|
|
'user_id' => $user->id,
|
|
'resource' => $resource,
|
|
'action' => $action,
|
|
'status' => 'pending',
|
|
'item_ids' => $itemIds,
|
|
'total_items' => $effectiveTotalItems,
|
|
'processed_items' => 0,
|
|
'succeeded' => 0,
|
|
'failed' => 0,
|
|
'skipped' => 0,
|
|
'failures' => [],
|
|
]);
|
|
|
|
$auditLog = $this->auditLogger->log(
|
|
tenant: $tenant,
|
|
action: "bulk.{$resource}.{$action}.created",
|
|
context: [
|
|
'metadata' => [
|
|
'bulk_run_id' => $run->id,
|
|
'total_items' => $effectiveTotalItems,
|
|
],
|
|
],
|
|
actorId: $user->id,
|
|
actorEmail: $user->email,
|
|
actorName: $user->name,
|
|
resourceType: 'bulk_operation_run',
|
|
resourceId: (string) $run->id
|
|
);
|
|
|
|
$run->update(['audit_log_id' => $auditLog->id]);
|
|
|
|
return $run;
|
|
}
|
|
|
|
public function start(BulkOperationRun $run): void
|
|
{
|
|
$run->update(['status' => 'running']);
|
|
}
|
|
|
|
public function recordSuccess(BulkOperationRun $run): void
|
|
{
|
|
$run->increment('processed_items');
|
|
$run->increment('succeeded');
|
|
}
|
|
|
|
public function recordFailure(BulkOperationRun $run, string $itemId, string $reason): void
|
|
{
|
|
$reason = $this->sanitizeFailureReason($reason);
|
|
|
|
$failures = $run->failures ?? [];
|
|
$failures[] = [
|
|
'item_id' => $itemId,
|
|
'reason' => $reason,
|
|
'timestamp' => now()->toIso8601String(),
|
|
];
|
|
|
|
$run->update([
|
|
'failures' => $failures,
|
|
'processed_items' => $run->processed_items + 1,
|
|
'failed' => $run->failed + 1,
|
|
]);
|
|
}
|
|
|
|
public function recordSkipped(BulkOperationRun $run): void
|
|
{
|
|
$run->increment('processed_items');
|
|
$run->increment('skipped');
|
|
}
|
|
|
|
public function recordSkippedWithReason(BulkOperationRun $run, string $itemId, string $reason): void
|
|
{
|
|
$reason = $this->sanitizeFailureReason($reason);
|
|
|
|
$failures = $run->failures ?? [];
|
|
$failures[] = [
|
|
'item_id' => $itemId,
|
|
'reason' => $reason,
|
|
'type' => 'skipped',
|
|
'timestamp' => now()->toIso8601String(),
|
|
];
|
|
|
|
$run->update([
|
|
'failures' => $failures,
|
|
'processed_items' => $run->processed_items + 1,
|
|
'skipped' => $run->skipped + 1,
|
|
]);
|
|
}
|
|
|
|
public function complete(BulkOperationRun $run): void
|
|
{
|
|
$run->refresh();
|
|
|
|
if ($run->processed_items > $run->total_items) {
|
|
BulkOperationRun::query()
|
|
->whereKey($run->id)
|
|
->update(['total_items' => $run->processed_items]);
|
|
|
|
$run->refresh();
|
|
}
|
|
|
|
if (! in_array($run->status, ['pending', 'running'], true)) {
|
|
return;
|
|
}
|
|
|
|
$status = $run->failed > 0 ? 'completed_with_errors' : 'completed';
|
|
|
|
$updated = BulkOperationRun::query()
|
|
->whereKey($run->id)
|
|
->whereIn('status', ['pending', 'running'])
|
|
->update(['status' => $status]);
|
|
|
|
if ($updated === 0) {
|
|
return;
|
|
}
|
|
|
|
$run->refresh();
|
|
|
|
$failureEntries = collect($run->failures ?? []);
|
|
$failedReasons = $failureEntries
|
|
->filter(fn (array $entry) => ($entry['type'] ?? 'failed') !== 'skipped')
|
|
->groupBy('reason')
|
|
->map(fn ($group) => $group->count())
|
|
->all();
|
|
|
|
$skippedReasons = $failureEntries
|
|
->filter(fn (array $entry) => ($entry['type'] ?? null) === 'skipped')
|
|
->groupBy('reason')
|
|
->map(fn ($group) => $group->count())
|
|
->all();
|
|
|
|
$this->auditLogger->log(
|
|
tenant: $run->tenant,
|
|
action: "bulk.{$run->resource}.{$run->action}.{$status}",
|
|
context: [
|
|
'metadata' => [
|
|
'bulk_run_id' => $run->id,
|
|
'succeeded' => $run->succeeded,
|
|
'failed' => $run->failed,
|
|
'skipped' => $run->skipped,
|
|
'failed_reasons' => $failedReasons,
|
|
'skipped_reasons' => $skippedReasons,
|
|
],
|
|
],
|
|
actorId: $run->user_id,
|
|
resourceType: 'bulk_operation_run',
|
|
resourceId: (string) $run->id
|
|
);
|
|
}
|
|
|
|
public function fail(BulkOperationRun $run, string $reason): void
|
|
{
|
|
$run->update(['status' => 'failed']);
|
|
|
|
$reason = $this->sanitizeFailureReason($reason);
|
|
|
|
$this->auditLogger->log(
|
|
tenant: $run->tenant,
|
|
action: "bulk.{$run->resource}.{$run->action}.failed",
|
|
context: [
|
|
'reason' => $reason,
|
|
'metadata' => [
|
|
'bulk_run_id' => $run->id,
|
|
],
|
|
],
|
|
actorId: $run->user_id,
|
|
status: 'failure',
|
|
resourceType: 'bulk_operation_run',
|
|
resourceId: (string) $run->id
|
|
);
|
|
}
|
|
|
|
public function abort(BulkOperationRun $run, string $reason): void
|
|
{
|
|
$run->update(['status' => 'aborted']);
|
|
|
|
$reason = $this->sanitizeFailureReason($reason);
|
|
|
|
$this->auditLogger->log(
|
|
tenant: $run->tenant,
|
|
action: "bulk.{$run->resource}.{$run->action}.aborted",
|
|
context: [
|
|
'reason' => $reason,
|
|
'metadata' => [
|
|
'bulk_run_id' => $run->id,
|
|
'succeeded' => $run->succeeded,
|
|
'failed' => $run->failed,
|
|
'skipped' => $run->skipped,
|
|
],
|
|
],
|
|
actorId: $run->user_id,
|
|
status: 'failure',
|
|
resourceType: 'bulk_operation_run',
|
|
resourceId: (string) $run->id
|
|
);
|
|
}
|
|
}
|