- Staff: the 13 perm_* tick columns become one Permissions column (count, "changed from role") opening an editor grouped by module, from /api/waiters/permission-catalog: switches labelled per key, keys you don't hold are disabled with the reason, reset to role defaults (names what it can't give), save the whole set; access.cannot_grant explained by label. - "Signed in now" (/management/sessions): who, where (app or device), since, last active, your own session marked; end session; show ended + reason. - "Manager approvals" (/management/approvals): the override audit with action label, asked by, approved by, device, used/not used, order link. - Backend: sessions list adds device_name/device_app/current/expires_at; overrides list adds device_name/expires_at. - Permission labels for every key in modules.json (en/el), guarded by test_module_texts; manager unit tests for the editor logic; e2e 66 checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
6.7 KiB
React
142 lines
6.7 KiB
React
// Staff permission editor (plan step 2.6e, APPS_PLATFORM D-7): every permission of the
|
|
// licensed modules, grouped by module, from GET /api/waiters/permission-catalog.
|
|
// - Saved as the whole key set: PUT /api/waiters/{id}/permissions. Keys of modules that
|
|
// aren't licensed aren't shown and are kept as they are.
|
|
// - You can only give permissions you hold yourself (backend: access.cannot_grant). Those
|
|
// switches are disabled with the reason; removing is always allowed.
|
|
// - "Reset to role defaults" fills the draft with the role's template (minus what you
|
|
// can't give, which it names); nothing is saved until Save.
|
|
// - A superadmin always has everything: read-only.
|
|
import { useEffect, useMemo, useState } from 'react'
|
|
import toast from 'react-hot-toast'
|
|
import { useQueryClient } from '@tanstack/react-query'
|
|
import { errorMessage, parseError } from '@xenia/api'
|
|
import { useAccess } from '@xenia/access'
|
|
import { useTranslation } from '@xenia/i18n'
|
|
import { Button, InfoTip, Sheet, Spinner, Switch } from '@xenia/ui'
|
|
import client from '../../api/client'
|
|
import { canGive, diff, editorGroups, permLabelKey, resetToRole } from './permissionsLogic'
|
|
|
|
export default function PermissionEditor({ member, onClose }) {
|
|
const { t } = useTranslation('manager')
|
|
const access = useAccess()
|
|
const qc = useQueryClient()
|
|
const actorIsSuperadmin = access.user?.role === 'superadmin'
|
|
const [data, setData] = useState(null) // { catalog, modules, target, grantable }
|
|
const [draft, setDraft] = useState(null)
|
|
const [error, setError] = useState('')
|
|
const [note, setNote] = useState('')
|
|
const [busy, setBusy] = useState(false)
|
|
|
|
useEffect(() => {
|
|
let alive = true
|
|
Promise.all([
|
|
client.get('/api/waiters/permission-catalog'),
|
|
client.get('/api/platform/modules'),
|
|
client.get(`/api/waiters/${member.id}/permissions`),
|
|
actorIsSuperadmin ? null : client.get(`/api/waiters/${access.user.id}/permissions`),
|
|
]).then(([c, m, p, mine]) => {
|
|
if (!alive) return
|
|
setData({
|
|
catalog: c.data.modules, modules: m.data.modules, target: p.data,
|
|
original: new Set(p.data.keys), grantable: mine ? new Set(mine.data.keys) : null,
|
|
})
|
|
setDraft(new Set(p.data.keys))
|
|
}).catch(err => alive && setError(errorMessage(err, t)))
|
|
return () => { alive = false }
|
|
}, [member.id, actorIsSuperadmin, access.user.id, t])
|
|
|
|
const groups = useMemo(() => (data ? editorGroups(data.catalog, data.modules, { actorIsSuperadmin }) : []), [data, actorIsSuperadmin])
|
|
const name = member.full_name || member.username
|
|
const label = key => t(permLabelKey(key))
|
|
const changes = data && draft ? diff(draft, data.original) : { added: [], removed: [] }
|
|
const changed = changes.added.length + changes.removed.length
|
|
const readOnly = data?.target.superadmin
|
|
|
|
function toggle(key, on) {
|
|
setNote('')
|
|
setError('')
|
|
setDraft(prev => {
|
|
const next = new Set(prev)
|
|
if (on) next.add(key)
|
|
else next.delete(key)
|
|
return next
|
|
})
|
|
}
|
|
|
|
function reset() {
|
|
const { keys, skipped } = resetToRole(data.target.role_template, data.original, data.grantable)
|
|
setDraft(keys)
|
|
setError('')
|
|
setNote(skipped.length ? t('permissions.resetSkipped', { list: skipped.map(label).join(', ') }) : t('permissions.resetDone'))
|
|
}
|
|
|
|
async function save() {
|
|
setBusy(true)
|
|
setError('')
|
|
try {
|
|
await client.put(`/api/waiters/${member.id}/permissions`, { keys: [...draft].sort() })
|
|
toast.success(t('permissions.saved', { name }))
|
|
qc.invalidateQueries({ queryKey: ['staff'] })
|
|
if (member.id === access.user.id) access.refresh()
|
|
onClose()
|
|
} catch (err) {
|
|
const x = parseError(err)
|
|
setError(x.code === 'access.cannot_grant' ? t('permissions.cannotGrant', { label: label(x.params.key) }) : errorMessage(err, t))
|
|
} finally {
|
|
setBusy(false)
|
|
}
|
|
}
|
|
|
|
const footer = readOnly || !data ? null : (
|
|
<>
|
|
<Button variant="ghost" onClick={reset} className="mr-auto" data-testid="perm-reset">{t('permissions.reset')}</Button>
|
|
<Button variant="secondary" onClick={onClose}>{t('common:actions.cancel')}</Button>
|
|
<Button onClick={save} loading={busy} disabled={!changed}>
|
|
{changed ? t('permissions.saveCount', { count: changed }) : t('permissions.save')}
|
|
</Button>
|
|
</>
|
|
)
|
|
|
|
return (
|
|
<Sheet open side="right" onClose={onClose} title={t('permissions.title', { name })}
|
|
description={data ? t('permissions.subtitle', { role: t(`roles.${data.target.role}`, { defaultValue: data.target.role }) }) : undefined}
|
|
footer={footer}>
|
|
{!data && !error && <div className="py-10 flex justify-center"><Spinner /></div>}
|
|
{!data && error && <p className="text-label text-danger-ink" role="alert">{error}</p>}
|
|
{readOnly && <p className="text-body text-ink-2">{t('permissions.superadmin')}</p>}
|
|
{data && !readOnly && (
|
|
<div className="grid gap-6" data-testid="perm-editor">
|
|
{note && <p className="text-label text-ink-2 bg-surface-2 rounded-card px-3 py-2" data-testid="perm-note">{note}</p>}
|
|
{error && <p className="text-label text-danger-ink bg-danger-soft rounded-card px-3 py-2" role="alert" data-testid="perm-error">{error}</p>}
|
|
{groups.map(g => (
|
|
<section key={g.key} data-testid={`perm-group-${g.key}`}>
|
|
<h3 className="text-label font-semibold text-ink mb-1 flex items-center gap-2">
|
|
{t(`modules:${g.key}.name`, { defaultValue: g.key })}
|
|
{!g.on && <span className="rounded-full px-2 py-0.5 text-caption font-medium bg-surface-2 text-ink-3">{t('permissions.moduleOff')}</span>}
|
|
</h3>
|
|
<div className="grid gap-2">
|
|
{g.permissions.map(p => {
|
|
const on = draft.has(p.key)
|
|
const allowed = on || canGive(p.key, data.original, data.grantable)
|
|
const fromRole = data.target.role_template.includes(p.key)
|
|
const hints = []
|
|
if (!allowed) hints.push(t('permissions.notYours'))
|
|
if (on !== fromRole) hints.push(fromRole ? t('permissions.roleHas') : t('permissions.roleHasNot'))
|
|
return (
|
|
<div key={p.key} className="flex items-start gap-1" data-testid={`perm-${p.key}`} data-on={on ? 'true' : 'false'}>
|
|
<Switch className="flex-1" checked={on} disabled={!allowed} onChange={v => toggle(p.key, v)}
|
|
label={label(p.key)} description={hints.join(' · ') || undefined} />
|
|
{p.overridable && <InfoTip>{t('permissions.overridable')}</InfoTip>}
|
|
</div>
|
|
)
|
|
})}
|
|
</div>
|
|
</section>
|
|
))}
|
|
</div>
|
|
)}
|
|
</Sheet>
|
|
)
|
|
}
|