UserContext.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Auth;
  4. /**
  5. * The minimal projection of a logged-in user the UI keeps in the session.
  6. *
  7. * `source` is `oidc` for users who logged in via Microsoft Entra and
  8. * `local` for the local-admin path. The api is the source of truth for
  9. * the user record; this struct is only what the UI needs to render
  10. * top-nav, sidebar visibility, and the `/app/me` page.
  11. */
  12. final class UserContext
  13. {
  14. public const SOURCE_OIDC = 'oidc';
  15. public const SOURCE_LOCAL = 'local';
  16. public function __construct(
  17. public readonly int $userId,
  18. public readonly string $displayName,
  19. public readonly string $role,
  20. public readonly ?string $email,
  21. public readonly string $source,
  22. ) {
  23. }
  24. /**
  25. * @param array<string, mixed> $row
  26. */
  27. public static function fromArray(array $row): self
  28. {
  29. return new self(
  30. userId: (int) $row['user_id'],
  31. displayName: (string) ($row['display_name'] ?? ''),
  32. role: (string) ($row['role'] ?? 'viewer'),
  33. email: isset($row['email']) ? (string) $row['email'] : null,
  34. source: (string) ($row['source'] ?? self::SOURCE_OIDC),
  35. );
  36. }
  37. /**
  38. * @return array<string, mixed>
  39. */
  40. public function toArray(): array
  41. {
  42. return [
  43. 'user_id' => $this->userId,
  44. 'display_name' => $this->displayName,
  45. 'role' => $this->role,
  46. 'email' => $this->email,
  47. 'source' => $this->source,
  48. ];
  49. }
  50. }