import Add from '@mui/icons-material/Add' import Apple from '@mui/icons-material/Apple' import Block from '@mui/icons-material/Block' import CheckCircle from '@mui/icons-material/CheckCircle' import Delete from '@mui/icons-material/Delete' import Download from '@mui/icons-material/Download' import Edit from '@mui/icons-material/Edit' import Key from '@mui/icons-material/Key' import KeyOff from '@mui/icons-material/KeyOff' import LockOpen from '@mui/icons-material/LockOpen' import PhonelinkLock from '@mui/icons-material/PhonelinkLock' import PhonelinkErase from '@mui/icons-material/PhonelinkErase' import Refresh from '@mui/icons-material/Refresh' import { Alert, Box, Button, IconButton, Table, TableBody, TableCell, TableHead, TableRow, ToggleButton, ToggleButtonGroup, Tooltip, } from '@mui/material' import { useCallback, useEffect, useState } from 'react' import ConfirmDialog, { type ConfirmDialogProps } from './ConfirmDialog.tsx' import OtpDialog from './OtpDialog.tsx' import PersonDialog from './PersonDialog.tsx' import ProfileDialog from './ProfileDialog.tsx' import { Section } from './ServerPanel.tsx' import SearchField, { matches } from './SearchField.tsx' import Toolbar from './Toolbar.tsx' import UserDialog from './UserDialog.tsx' import { ApiError, PROFILE_SHAPE, REMOVED_SHAPE, STATUS_SHAPE, USERS_SHAPE, USER_SHAPE, request, type Console, type Person, type RouterStatus, type User, } from './api.ts' import { saveFile } from './saveFile.ts' /** The kinds of users the list shows: all, VPN users or Manager-only accounts. */ type Kind = 'all' | 'vpn' | 'manager' /** How console login is changed, which the Manager cannot do. */ const CONSOLE_HELP = 'Console login is switched on the server: linux-manager-user console --posix | --account LOCAL | --off' /** The console login of a user as a cell: off, the user's own POSIX account with its uid, or the local account, refused without one-time passwords. */ function consoleText(login: Console | null, otp: boolean) { if (login === null || !login.enabled) { return login === null ? '-' : 'off' } const text = login.local ? `local ${login.account}` : `LDAP POSIX (uid ${login.uid ?? '-'})` return otp ? text : `${text}, refused: no OTP` } /** * The users of the server's directory, which /users answers without its service accounts (ServiceAccountsPanel): list with their names and * e-mail addresses, filtered by kind and by a search in the user name, the names and the e-mail address, add, edit the details, set the password, enrol for one-time passwords and remove the enrolment, * remove the passkeys, disable, enable, unlock after failed logins, remove, the OpenVPN profile, offered while the * server's status shows OpenVPN, and the Apple VPN profile of IKEv2, offered while it shows IKEv2 access; console * login is shown, not changed. */ export default function UsersPanel() { const [users, setUsers] = useState(null) const [error, setError] = useState(null) const [unavailable, setUnavailable] = useState(false) const [busy, setBusy] = useState(false) const [adding, setAdding] = useState(false) const [password, setPassword] = useState(null) const [details, setDetails] = useState(null) const [enrolling, setEnrolling] = useState(null) const [confirm, setConfirm] = useState(null) const [openvpn, setOpenvpn] = useState(null) const [p2s, setP2s] = useState(null) const [appleProfile, setAppleProfile] = useState(null) const [search, setSearch] = useState('') const [kind, setKind] = useState('all') const refresh = useCallback(async () => { setBusy(true) // The status has openvpn and p2s while the server runs OpenVPN and IKEv2, which the profiles need; null while it is // not known. request('GET', '/status/full', STATUS_SHAPE) .then((status) => { setOpenvpn(status.openvpn !== undefined) setP2s(status.p2s !== undefined) }) .catch(() => { setOpenvpn(null) setP2s(null) }) try { setUsers((await request<{ users: User[] }>('GET', '/users', USERS_SHAPE)).users) setError(null) setUnavailable(false) } catch (caught) { const failure = caught as ApiError setUsers(null) setUnavailable(failure.status === 404) setError(failure.message) } finally { setBusy(false) } }, []) useEffect(() => { void refresh() }, [refresh]) /** Runs a change of a user, then reads the list again; a failure is shown above the list after the reading. */ async function change(work: () => Promise) { setBusy(true) let failure: string | null = null try { await work() } catch (caught) { failure = (caught as Error).message } await refresh() if (failure !== null) { setError(failure) } } async function download(name: string) { await change(async () => { const { profile } = await request<{ profile: string }>('GET', `/users/${name}/profile`, PROFILE_SHAPE) saveFile(`${name}.ovpn`, profile, 'application/x-openvpn-profile') }) } function removeOtp(name: string) { setConfirm({ title: 'Remove the one-time passwords', message: `Remove the TOTP secret of ${name}? The user's authenticator app no longer matches the server.`, confirmLabel: 'Remove', destructive: true, onCancel: () => setConfirm(null), onConfirm: () => { setConfirm(null) void change(() => request('DELETE', `/users/${name}/otp`, USER_SHAPE)) }, }) } function removePasskeys(name: string) { setConfirm({ title: 'Remove the passkeys', message: `Remove all passkeys of ${name}? The user signs in with the password until adding a passkey again.`, confirmLabel: 'Remove', destructive: true, onCancel: () => setConfirm(null), onConfirm: () => { setConfirm(null) void change(() => request('DELETE', `/users/${name}/passkeys`, USER_SHAPE)) }, }) } function remove(name: string) { setConfirm({ title: 'Remove user', message: `Remove ${name} and revoke the user's certificate? The user can no longer connect.`, confirmLabel: 'Remove', destructive: true, onCancel: () => setConfirm(null), onConfirm: () => { setConfirm(null) void change(() => request('DELETE', `/users/${name}`, REMOVED_SHAPE)) }, }) } const shown = (users ?? []).filter((user) => (kind === 'all' || (kind === 'manager') === user.manager_only) && matches(search, [user.name, user.first_name, user.last_name, user.email])) return ( next && setKind(next)}> All VPN users Manager-only {error && {error}} {users && (
User name Name E-mail State Certificate OTP Passkeys Console Actions {shown.length === 0 && ( None. )} {shown.map((user) => ( {user.name} {user.manager_only ? 'Manager-only account' : [user.first_name, user.last_name].filter(Boolean).join(' ') || '-'} {user.email ?? '-'} {!user.enabled ? 'disabled' : user.locked ? 'locked' : 'enabled'} {user.certificate ? 'issued' : 'missing'} {user.otp ? 'enrolled' : '-'} {user.passkeys.length || '-'} {consoleText(user.console, user.otp)} setDetails(user)}> setPassword(user.name)}> setEnrolling(user.name)}> removeOtp(user.name)}> removePasskeys(user.name)}> void change(() => request('POST', `/users/${user.name}/${user.enabled ? 'disable' : 'enable'}`, USER_SHAPE))} > {user.enabled ? : } void change(() => request('POST', `/users/${user.name}/unlock`, USER_SHAPE))} > void download(user.name)}> setAppleProfile(user.name)}> remove(user.name)}> ))}
)} {adding && ( setAdding(false)} onSave={async (name, secret, person) => { await request('POST', '/users', USER_SHAPE, { name, password: secret, ...person }) setAdding(false) await refresh() }} /> )} {details && ( setDetails(null)} onSave={async (person) => { // Only the fields that changed are sent, and nothing when none did. const changed = Object.fromEntries((Object.keys(person) as (keyof Person)[]) .filter((key) => person[key] !== (details[key] ?? '')) .map((key) => [key, person[key]])) if (Object.keys(changed).length > 0) { await request('PUT', `/users/${details.name}`, USER_SHAPE, changed) } setDetails(null) if (Object.keys(changed).length > 0) { await refresh() } }} /> )} {password && ( setPassword(null)} onSave={async (name, secret) => { await request('PUT', `/users/${name}/password`, USER_SHAPE, { password: secret }) setPassword(null) }} /> )} {enrolling && ( setEnrolling(null)} onSave={async (secret, code) => { await request('PUT', `/users/${enrolling}/otp`, USER_SHAPE, { secret, code }) setEnrolling(null) await refresh() }} /> )} {appleProfile && ( setAppleProfile(null)} /> )} {confirm && }
) }