mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-24 07:53:09 +00:00
2c2d008dc2
* fix(security): mint idempotency keys properly, and resolve the extractor by path Three findings out of a sweep of every open Sonar security issue. The other 49 turned out to be false positives and are documented in sonar-project.properties rather than changed — each entry scoped to a file, never a whole rule, so a future Math.random() on an auth path still fails. The idempotency key is the one that can lose data. Both producers fell back to Math.random when crypto.randomUUID was missing, and randomUUID needs a secure context — TREK's own quickstart is http://localhost:3000 and LAN installs run on http://192.168.x.x, so on the deployments the README documents the fallback is what actually runs. The axios one was `Math.random().toString(36).slice(2)`, which is not length-stable: 0.5 yields a single character and 0 yields the empty string, which the interceptor reads as "no key" and skips deduplication for entirely. Two writes sharing a key collapse into one — the second gets the first one's cached response and the edit is gone with no error anywhere. Both now come from utils/randomId, which has the getRandomValues rung that works in insecure contexts too. The extractor probe stored the bare binary name, so every extraction re-resolved it through whatever PATH held at that moment. The official image sets KITINERARY_EXTRACTOR_PATH and never reaches that branch, but a bare-metal install without it ran an unqualified name from a PATH it does not control. It now walks the search path itself and keeps the absolute path of the file it verified; execFileSync also drops the /bin/sh hop the old probe needed. PATH comes through app-config because process.env is walled off in src/. * fix: four tests and sorts that were not doing what they claimed Sonar reported 43 high-impact reliability issues. Reading all of them, four are real; the rest are cosmetic or would be made worse by the suggested change. budget.test.ts hand-built `paid: true`, but both emitters send `paid: body.paid ? 1 : 0` and the handler stores the value verbatim — the test asserted a payload the server never sends. The line below it already used a number. admin.controller.test.ts had a `.resolves` assertion with no await. The promise was dropped, so that line could not fail the test. The two to-do category lists sorted with a bare .sort(), which is byte order: every accented name files behind the whole ASCII range, so in a German trip "Übernachtung" lands after "Zelt". TREK ships 23 locales, so this is real data. One side effect worth knowing: katColor picks a category's dot colour by its index, so re-sorting can move which category shows which colour — it already moves whenever a category is added, and the colour carries no meaning. Deliberately not changed: the two `| 0` truncations (Math.trunc is not equivalent — `| 0` wraps at 32 bits and these carry millisecond timestamps), the two .map() callbacks whose second parameter is declared and used, and the sha1 in thumbnail.service (its own comment says a swap would orphan every existing thumbnail). * refactor: give every button a type, and prefer the Number statics Two mechanical sweeps, no behaviour change intended in either. A <button> with no type attribute is type="submit" per the HTML spec. Outside a form that does nothing today, but it turns into a surprise the moment the markup is wrapped in one, which is what Sonar's S9011 is warning about. 870 buttons get an explicit type="button". Two things were left alone on purpose: buttons that take a {...spread}, because the spread may carry a type and the merge order decides which wins, and anything inside a <form> — there the answer is a judgement call, and checking all eleven form files found every real submit button already carrying type="submit". The Number sweep covers only the three globals that are exactly equivalent to their Number twin: parseInt, parseFloat and NaN. isNaN is NOT in it — the global coerces its argument, so isNaN('abc') is true where Number.isNaN('abc') is false, and renaming it would change what the code does. It reaches server and plugin-sdk as well as the client; both copies of egress-policy.ts got the identical edit, so the mirror the SDK's parity test guards stays intact.
250 lines
13 KiB
TypeScript
250 lines
13 KiB
TypeScript
import React from 'react'
|
||
import CustomSelect from '../../components/shared/CustomSelect'
|
||
import { Shield, Trash2, Edit2, UserPlus, Link2, Copy, Plus } from 'lucide-react'
|
||
import Modal from '../../components/shared/Modal'
|
||
import PermissionsPanel from '../../components/Admin/PermissionsPanel'
|
||
import type { TranslationFn } from '../../types'
|
||
import type { useAdmin } from './useAdmin'
|
||
|
||
interface AdminUsersTabProps {
|
||
admin: ReturnType<typeof useAdmin>
|
||
t: TranslationFn
|
||
locale: string
|
||
}
|
||
|
||
// "Users" admin tab: user table, invite links, permissions panel + the
|
||
// create-invite modal. Pure layout around the useAdmin hook — no logic of its own.
|
||
export default function AdminUsersTab({ admin, t, locale }: AdminUsersTabProps): React.ReactElement {
|
||
const {
|
||
hour12, currentUser,
|
||
users, isLoading,
|
||
setShowCreateUser,
|
||
invites, inviteTrips, showCreateInvite, setShowCreateInvite, inviteForm, setInviteForm,
|
||
copyInviteLink, handleCreateInvite, handleDeleteInvite,
|
||
handleEditUser, handleDeleteUser,
|
||
} = admin
|
||
|
||
return (
|
||
<>
|
||
<div className="bg-white rounded-xl border border-slate-200 overflow-hidden">
|
||
<div className="p-5 border-b border-slate-100 flex items-center justify-between">
|
||
<div>
|
||
<h2 className="font-semibold text-slate-900">{t('admin.tabs.users')}</h2>
|
||
<p className="text-xs text-slate-400 mt-1">{users.length} {t('admin.stats.users')}</p>
|
||
</div>
|
||
<button type="button"
|
||
onClick={() => setShowCreateUser(true)}
|
||
className="flex items-center gap-1.5 px-3 py-1.5 text-sm bg-slate-900 text-white rounded-lg hover:bg-slate-700 transition-colors"
|
||
>
|
||
<UserPlus className="w-4 h-4" />
|
||
{t('admin.createUser')}
|
||
</button>
|
||
</div>
|
||
|
||
{isLoading ? (
|
||
<div className="p-8 text-center">
|
||
<div className="w-8 h-8 border-2 border-slate-200 border-t-slate-900 rounded-full animate-spin mx-auto"></div>
|
||
</div>
|
||
) : (
|
||
<div className="overflow-x-auto">
|
||
<table className="w-full">
|
||
<thead>
|
||
<tr className="text-left text-xs font-medium text-slate-500 uppercase tracking-wider border-b border-slate-100 bg-slate-50">
|
||
<th className="px-5 py-3">{t('admin.table.user')}</th>
|
||
<th className="px-5 py-3">{t('admin.table.email')}</th>
|
||
<th className="px-5 py-3">{t('admin.table.role')}</th>
|
||
<th className="px-5 py-3">{t('admin.table.created')}</th>
|
||
<th className="px-5 py-3">{t('admin.table.lastLogin')}</th>
|
||
<th className="px-5 py-3 text-right">{t('admin.table.actions')}</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody className="divide-y divide-slate-100 trek-stagger">
|
||
{users.map(u => (
|
||
<tr key={u.id} className={`hover:bg-slate-50 transition-colors ${u.id === currentUser?.id ? 'bg-slate-50/60' : ''}`}>
|
||
<td className="px-5 py-3">
|
||
<div className="flex items-center gap-2">
|
||
<div className="relative">
|
||
{u.avatar_url ? (
|
||
<img src={u.avatar_url} alt={u.username} className="w-8 h-8 rounded-full object-cover" />
|
||
) : (
|
||
<div className="w-8 h-8 rounded-full bg-slate-100 flex items-center justify-center text-sm font-medium text-slate-700">
|
||
{u.username.charAt(0).toUpperCase()}
|
||
</div>
|
||
)}
|
||
<span className={`absolute -bottom-0.5 -right-0.5 w-3 h-3 rounded-full border-2 border-surface-card ${u.online ? 'bg-[#22c55e]' : 'bg-[#94a3b8]'}`} />
|
||
</div>
|
||
<div>
|
||
<p className="text-sm font-medium text-slate-900">{u.username}</p>
|
||
{u.id === currentUser?.id && (
|
||
<span className="text-xs text-slate-500">{t('admin.you')}</span>
|
||
)}
|
||
</div>
|
||
</div>
|
||
</td>
|
||
<td className="px-5 py-3 text-sm text-slate-600">{u.email}</td>
|
||
<td className="px-5 py-3">
|
||
<span className={`inline-flex items-center gap-1 text-xs font-medium px-2.5 py-0.5 rounded-full ${
|
||
u.role === 'admin'
|
||
? 'bg-slate-900 text-white'
|
||
: 'bg-slate-100 text-slate-600'
|
||
}`}>
|
||
{u.role === 'admin' && <Shield className="w-3 h-3" />}
|
||
{u.role === 'admin' ? t('settings.roleAdmin') : t('settings.roleUser')}
|
||
</span>
|
||
</td>
|
||
<td className="px-5 py-3 text-sm text-slate-500">
|
||
{new Date(u.created_at).toLocaleDateString(locale)}
|
||
</td>
|
||
<td className="px-5 py-3 text-sm text-slate-500">
|
||
{u.last_login ? new Date(u.last_login).toLocaleDateString(locale, { day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit', hour12 }) : '—'}
|
||
</td>
|
||
<td className="px-5 py-3">
|
||
<div className="flex items-center gap-2 justify-end">
|
||
<button type="button"
|
||
onClick={() => handleEditUser(u)}
|
||
className="p-1.5 text-slate-400 hover:text-slate-900 hover:bg-slate-100 rounded-lg transition-colors"
|
||
title={t('admin.editUser')}
|
||
>
|
||
<Edit2 className="w-4 h-4" />
|
||
</button>
|
||
<button type="button"
|
||
onClick={() => handleDeleteUser(u)}
|
||
disabled={u.id === currentUser?.id}
|
||
className="p-1.5 text-slate-400 hover:text-red-600 hover:bg-red-50 rounded-lg transition-colors disabled:opacity-30 disabled:cursor-not-allowed"
|
||
title={t('admin.deleteUserTitle')}
|
||
>
|
||
<Trash2 className="w-4 h-4" />
|
||
</button>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
{/* Invite Links (inside users tab) */}
|
||
<div className="bg-white rounded-xl border border-slate-200 overflow-hidden mt-6">
|
||
<div className="p-5 border-b border-slate-100 flex items-center justify-between">
|
||
<div>
|
||
<h2 className="font-semibold text-slate-900">{t('admin.invite.title')}</h2>
|
||
<p className="text-xs text-slate-400 mt-1">{t('admin.invite.subtitle')}</p>
|
||
</div>
|
||
<button type="button"
|
||
onClick={() => setShowCreateInvite(true)}
|
||
className="flex items-center gap-1.5 px-3 py-1.5 text-sm bg-slate-900 text-white rounded-lg hover:bg-slate-700 transition-colors"
|
||
>
|
||
<Plus className="w-4 h-4" />
|
||
{t('admin.invite.create')}
|
||
</button>
|
||
</div>
|
||
|
||
{invites.length === 0 ? (
|
||
<div className="p-8 text-center text-sm text-slate-400">{t('admin.invite.empty')}</div>
|
||
) : (
|
||
<div className="divide-y divide-slate-100">
|
||
{invites.map(inv => {
|
||
const isExpired = inv.expires_at && new Date(inv.expires_at) < new Date()
|
||
const isUsedUp = inv.max_uses > 0 && inv.used_count >= inv.max_uses
|
||
const isActive = !isExpired && !isUsedUp
|
||
return (
|
||
<div key={inv.id} className="px-5 py-3 flex items-center gap-4">
|
||
<Link2 className={`w-4 h-4 flex-shrink-0 ${isActive ? 'text-content' : 'text-[#d1d5db]'}`} />
|
||
<div className="flex-1 min-w-0">
|
||
<div className="flex items-center gap-2">
|
||
<code className="text-xs font-mono text-slate-600 truncate">{inv.token.slice(0, 12)}...</code>
|
||
<span className={`text-xs px-1.5 py-0.5 rounded-full font-medium ${
|
||
isActive ? 'bg-green-50 text-green-700' : 'bg-slate-100 text-slate-400'
|
||
}`}>
|
||
{isUsedUp ? t('admin.invite.usedUp') : isExpired ? t('admin.invite.expired') : t('admin.invite.active')}
|
||
</span>
|
||
</div>
|
||
<div className="text-xs text-slate-400 mt-0.5">
|
||
{inv.used_count}/{inv.max_uses === 0 ? '∞' : inv.max_uses} {t('admin.invite.uses')}
|
||
{inv.expires_at && ` · ${t('admin.invite.expiresAt')} ${new Date(inv.expires_at).toLocaleDateString(locale)}`}
|
||
{inv.trip_title && ` · ${t('admin.invite.boundTo', { trip: inv.trip_title })}`}
|
||
{` · ${t('admin.invite.createdBy')} ${inv.created_by_name}`}
|
||
</div>
|
||
</div>
|
||
{isActive && (
|
||
<button type="button" onClick={() => copyInviteLink(inv.token)} title={t('admin.invite.copyLink')}
|
||
className="p-1.5 rounded-lg hover:bg-slate-100 text-slate-400 hover:text-slate-700 transition-colors">
|
||
<Copy className="w-3.5 h-3.5" />
|
||
</button>
|
||
)}
|
||
<button type="button" onClick={() => handleDeleteInvite(inv.id)} title={t('common.delete')}
|
||
className="p-1.5 rounded-lg hover:bg-red-50 text-slate-400 hover:text-red-500 transition-colors">
|
||
<Trash2 className="w-3.5 h-3.5" />
|
||
</button>
|
||
</div>
|
||
)
|
||
})}
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
<div className="mt-6"><PermissionsPanel /></div>
|
||
|
||
{/* Create Invite Modal */}
|
||
<Modal isOpen={showCreateInvite} onClose={() => setShowCreateInvite(false)} title={t('admin.invite.create')} size="sm">
|
||
<div className="space-y-4">
|
||
<div>
|
||
<label className="block text-xs font-semibold text-slate-500 uppercase tracking-wider mb-1.5">{t('admin.invite.maxUses')}</label>
|
||
<div className="flex gap-2">
|
||
{[1, 2, 3, 4, 5, 0].map(n => (
|
||
<button key={n} type="button" onClick={() => setInviteForm(f => ({ ...f, max_uses: n }))}
|
||
className={`flex-1 py-2 rounded-lg text-sm font-semibold border transition-colors ${
|
||
inviteForm.max_uses === n ? 'bg-slate-900 text-white border-slate-900' : 'bg-white text-slate-600 border-slate-200 hover:border-slate-400'
|
||
}`}>
|
||
{n === 0 ? '∞' : `${n}×`}
|
||
</button>
|
||
))}
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<label className="block text-xs font-semibold text-slate-500 uppercase tracking-wider mb-1.5">{t('admin.invite.expiry')}</label>
|
||
<div className="flex gap-2">
|
||
{[
|
||
{ value: 1, label: '1d' },
|
||
{ value: 3, label: '3d' },
|
||
{ value: 7, label: '7d' },
|
||
{ value: 14, label: '14d' },
|
||
{ value: '', label: '∞' },
|
||
].map(opt => (
|
||
<button key={String(opt.value)} type="button" onClick={() => setInviteForm(f => ({ ...f, expires_in_days: opt.value as number | '' }))}
|
||
className={`flex-1 py-2 rounded-lg text-sm font-semibold border transition-colors ${
|
||
inviteForm.expires_in_days === opt.value ? 'bg-slate-900 text-white border-slate-900' : 'bg-white text-slate-600 border-slate-200 hover:border-slate-400'
|
||
}`}>
|
||
{opt.label}
|
||
</button>
|
||
))}
|
||
</div>
|
||
</div>
|
||
{inviteTrips.length > 0 && (
|
||
<div>
|
||
<label className="block text-xs font-semibold text-slate-500 uppercase tracking-wider mb-1.5">{t('admin.invite.tripLabel')}</label>
|
||
<CustomSelect
|
||
value={inviteForm.trip_id}
|
||
onChange={v => setInviteForm(f => ({ ...f, trip_id: v === '' ? '' : Number(v) }))}
|
||
options={[
|
||
{ value: '', label: t('admin.invite.tripNone') },
|
||
...inviteTrips.map(tr => ({ value: tr.id, label: tr.title })),
|
||
]}
|
||
searchable={inviteTrips.length > 8}
|
||
placeholder={t('admin.invite.tripNone')}
|
||
/>
|
||
<p className="text-xs text-slate-400 mt-1.5 leading-relaxed">{t('admin.invite.tripHint')}</p>
|
||
</div>
|
||
)}
|
||
<div className="flex justify-end gap-2 pt-2 border-t border-slate-100">
|
||
<button type="button" onClick={() => setShowCreateInvite(false)} className="px-4 py-2 text-sm text-slate-500 hover:text-slate-700">{t('common.cancel')}</button>
|
||
<button type="button" onClick={handleCreateInvite} className="px-4 py-2 text-sm bg-slate-900 text-white rounded-lg hover:bg-slate-700">{t('admin.invite.createAndCopy')}</button>
|
||
</div>
|
||
</div>
|
||
</Modal>
|
||
</>
|
||
)
|
||
}
|