1
0

index.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /** @var list<\App\Domain\User> $users */
  3. /** @var \App\Domain\User $currentUser */
  4. /** @var string $csrfToken */
  5. /** @var string $flash */
  6. /** @var string $error */
  7. use function App\Http\e;
  8. $errorMessages = [
  9. 'self_demote' => 'You cannot demote yourself — ask another admin.',
  10. 'last_admin' => 'Cannot demote the last remaining admin.',
  11. 'not_found' => 'User not found.',
  12. 'db_error' => 'Could not save. Try again.',
  13. ];
  14. $flashMessages = [
  15. 'promoted' => 'Admin granted.',
  16. 'demoted' => 'Admin revoked.',
  17. 'noop' => 'Nothing changed.',
  18. ];
  19. ?>
  20. <section class="space-y-6">
  21. <div>
  22. <h1 class="text-2xl font-semibold tracking-tight">Users</h1>
  23. <p class="text-slate-600 text-sm mt-1 max-w-prose dark:text-slate-400">
  24. Everyone who has ever signed in. Toggle admin status here; you
  25. cannot demote yourself or the last admin. Users are never deleted
  26. — inactive accounts simply stop signing in.
  27. </p>
  28. </div>
  29. <?php if ($error !== '' && isset($errorMessages[$error])): ?>
  30. <div class="rounded-md border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-800 dark:bg-red-900 dark:border-red-800 dark:text-red-200">
  31. <?= e($errorMessages[$error]) ?>
  32. </div>
  33. <?php endif; ?>
  34. <?php if ($flash !== '' && isset($flashMessages[$flash])): ?>
  35. <div class="rounded-md border border-green-200 bg-green-50 px-4 py-3 text-sm text-green-800 dark:bg-green-900 dark:border-green-800 dark:text-green-200">
  36. <?= e($flashMessages[$flash]) ?>
  37. </div>
  38. <?php endif; ?>
  39. <div class="rounded-lg border bg-white overflow-hidden dark:bg-slate-800 dark:border-slate-700">
  40. <?php if ($users === []): ?>
  41. <div class="p-8 text-center text-slate-500 text-sm dark:text-slate-400">No users yet.</div>
  42. <?php else: ?>
  43. <table class="min-w-full text-sm">
  44. <thead class="bg-slate-50 text-slate-600 text-xs uppercase tracking-wider dark:bg-slate-700 dark:text-slate-300">
  45. <tr>
  46. <th class="text-left px-4 py-2 font-semibold">Email</th>
  47. <th class="text-left px-4 py-2 font-semibold">Display name</th>
  48. <th class="text-left px-4 py-2 font-semibold">Last login (UTC)</th>
  49. <th class="text-left px-4 py-2 font-semibold">Admin</th>
  50. <th class="text-right px-4 py-2 font-semibold">&nbsp;</th>
  51. </tr>
  52. </thead>
  53. <tbody class="divide-y divide-slate-100 dark:divide-slate-700">
  54. <?php foreach ($users as $u): $isSelf = $u->id === $currentUser->id; ?>
  55. <tr>
  56. <form method="post" action="/users/<?= (int) $u->id ?>" class="contents">
  57. <input type="hidden" name="_csrf" value="<?= e($csrfToken) ?>">
  58. <td class="px-4 py-2 font-mono text-xs">
  59. <?= e($u->email) ?>
  60. <?php if ($isSelf): ?>
  61. <span class="ml-1 inline-block px-1.5 py-0.5 text-[10px] font-semibold uppercase tracking-wider bg-slate-100 text-slate-700 rounded dark:bg-slate-700 dark:text-slate-200">you</span>
  62. <?php endif; ?>
  63. </td>
  64. <td class="px-4 py-2"><?= e($u->displayName) ?></td>
  65. <td class="px-4 py-2 text-slate-500 font-mono text-xs dark:text-slate-400">
  66. <?= $u->lastLoginAt !== null ? e($u->lastLoginAt) : '<span class="text-slate-400 dark:text-slate-500">—</span>' ?>
  67. </td>
  68. <td class="px-4 py-2">
  69. <label class="inline-flex items-center gap-2">
  70. <input name="is_admin" type="checkbox" value="1"
  71. <?= $u->isAdmin ? 'checked' : '' ?>
  72. <?= $isSelf && $u->isAdmin ? 'disabled title="You cannot demote yourself"' : '' ?>
  73. class="rounded border-slate-300 dark:border-slate-600">
  74. <span class="text-slate-600 dark:text-slate-400">admin</span>
  75. </label>
  76. </td>
  77. <td class="px-4 py-2 text-right">
  78. <button type="submit"
  79. class="rounded-md border border-slate-300 bg-white text-slate-700 px-3 py-1 text-sm hover:bg-slate-100 dark:bg-slate-800 dark:border-slate-600 dark:text-slate-200 dark:hover:bg-slate-700">
  80. Save
  81. </button>
  82. </td>
  83. </form>
  84. </tr>
  85. <?php endforeach; ?>
  86. </tbody>
  87. </table>
  88. <?php endif; ?>
  89. </div>
  90. </section>