359 lines
16 KiB
TypeScript
359 lines
16 KiB
TypeScript
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 <name> --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<User[] | null>(null)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [unavailable, setUnavailable] = useState(false)
|
|
const [busy, setBusy] = useState(false)
|
|
const [adding, setAdding] = useState(false)
|
|
const [password, setPassword] = useState<string | null>(null)
|
|
const [details, setDetails] = useState<User | null>(null)
|
|
const [enrolling, setEnrolling] = useState<string | null>(null)
|
|
const [confirm, setConfirm] = useState<ConfirmDialogProps | null>(null)
|
|
const [openvpn, setOpenvpn] = useState<boolean | null>(null)
|
|
const [p2s, setP2s] = useState<boolean | null>(null)
|
|
const [appleProfile, setAppleProfile] = useState<string | null>(null)
|
|
const [search, setSearch] = useState('')
|
|
const [kind, setKind] = useState<Kind>('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<RouterStatus>('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<unknown>) {
|
|
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 (
|
|
<Box component="section">
|
|
<Toolbar description="Users of remote access.">
|
|
<ToggleButtonGroup size="small" exclusive value={kind} onChange={(_event, next: Kind | null) => next && setKind(next)}>
|
|
<ToggleButton value="all">All</ToggleButton>
|
|
<ToggleButton value="vpn">VPN users</ToggleButton>
|
|
<ToggleButton value="manager">Manager-only</ToggleButton>
|
|
</ToggleButtonGroup>
|
|
<SearchField value={search} onChange={setSearch} />
|
|
<Button variant="contained" startIcon={<Add />} disabled={busy || users === null} onClick={() => setAdding(true)}>
|
|
Add
|
|
</Button>
|
|
<Button variant="outlined" startIcon={<Refresh />} disabled={busy} onClick={() => void refresh()}>
|
|
Refresh
|
|
</Button>
|
|
</Toolbar>
|
|
|
|
{error && <Alert severity={unavailable ? 'info' : 'error'} sx={{ mb: 2, whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
|
|
{users && (
|
|
<Section title="Users">
|
|
<Table size="small">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableCell>User name</TableCell>
|
|
<TableCell>Name</TableCell>
|
|
<TableCell>E-mail</TableCell>
|
|
<TableCell>State</TableCell>
|
|
<TableCell>Certificate</TableCell>
|
|
<TableCell>OTP</TableCell>
|
|
<TableCell>Passkeys</TableCell>
|
|
<TableCell>Console</TableCell>
|
|
<TableCell align="right">Actions</TableCell>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
{shown.length === 0 && (
|
|
<TableRow>
|
|
<TableCell colSpan={9} sx={{ color: 'text.secondary' }}>None.</TableCell>
|
|
</TableRow>
|
|
)}
|
|
{shown.map((user) => (
|
|
<TableRow key={user.name} hover>
|
|
<TableCell>{user.name}</TableCell>
|
|
<TableCell sx={user.manager_only ? { color: 'text.secondary', fontStyle: 'italic' } : undefined}>
|
|
{user.manager_only ? 'Manager-only account' : [user.first_name, user.last_name].filter(Boolean).join(' ') || '-'}
|
|
</TableCell>
|
|
<TableCell>{user.email ?? '-'}</TableCell>
|
|
<TableCell sx={{ color: user.enabled && !user.locked ? 'success.main' : 'warning.main' }}>
|
|
{!user.enabled ? 'disabled' : user.locked ? 'locked' : 'enabled'}
|
|
</TableCell>
|
|
<TableCell>{user.certificate ? 'issued' : 'missing'}</TableCell>
|
|
<TableCell>{user.otp ? 'enrolled' : '-'}</TableCell>
|
|
<TableCell>{user.passkeys.length || '-'}</TableCell>
|
|
<TableCell>
|
|
<Tooltip title={CONSOLE_HELP}>
|
|
<span>{consoleText(user.console, user.otp)}</span>
|
|
</Tooltip>
|
|
</TableCell>
|
|
<TableCell align="right" sx={{ whiteSpace: 'nowrap' }}>
|
|
<Tooltip title={user.manager_only ? 'Edit the e-mail' : 'Edit the name and e-mail'}>
|
|
<span>
|
|
<IconButton size="small" disabled={busy} onClick={() => setDetails(user)}><Edit fontSize="small" /></IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<Tooltip title="Set the password">
|
|
<span>
|
|
<IconButton size="small" disabled={busy} onClick={() => setPassword(user.name)}><Key fontSize="small" /></IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<Tooltip title={user.otp ? 'Enrol again for one-time passwords' : 'Enrol for one-time passwords'}>
|
|
<span>
|
|
<IconButton size="small" disabled={busy} onClick={() => setEnrolling(user.name)}><PhonelinkLock fontSize="small" /></IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<Tooltip title="Remove the one-time passwords">
|
|
<span>
|
|
<IconButton size="small" disabled={busy || !user.otp} onClick={() => removeOtp(user.name)}><PhonelinkErase fontSize="small" /></IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<Tooltip title="Remove the passkeys">
|
|
<span>
|
|
<IconButton size="small" disabled={busy || user.passkeys.length === 0} onClick={() => removePasskeys(user.name)}><KeyOff fontSize="small" /></IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<Tooltip title={user.enabled ? 'Disable' : 'Enable'}>
|
|
<span>
|
|
<IconButton
|
|
size="small"
|
|
disabled={busy}
|
|
onClick={() => void change(() => request('POST', `/users/${user.name}/${user.enabled ? 'disable' : 'enable'}`, USER_SHAPE))}
|
|
>
|
|
{user.enabled ? <Block fontSize="small" /> : <CheckCircle fontSize="small" />}
|
|
</IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<Tooltip title="Unlock after failed logins">
|
|
<span>
|
|
<IconButton
|
|
size="small"
|
|
disabled={busy || !user.locked}
|
|
onClick={() => void change(() => request('POST', `/users/${user.name}/unlock`, USER_SHAPE))}
|
|
>
|
|
<LockOpen fontSize="small" />
|
|
</IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<Tooltip title={openvpn === false ? 'OpenVPN is not enabled on the server' : 'Download the OpenVPN profile'}>
|
|
<span>
|
|
<IconButton size="small" disabled={busy || !user.certificate || openvpn === false} onClick={() => void download(user.name)}><Download fontSize="small" /></IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<Tooltip title={p2s === false ? 'IKEv2 is not enabled on the server' : 'Download the Apple VPN profile (IKEv2)'}>
|
|
<span>
|
|
<IconButton size="small" disabled={busy || !user.certificate || p2s === false} onClick={() => setAppleProfile(user.name)}><Apple fontSize="small" /></IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<Tooltip title="Remove">
|
|
<span>
|
|
<IconButton size="small" color="error" disabled={busy} onClick={() => remove(user.name)}><Delete fontSize="small" /></IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
</TableCell>
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
</Table>
|
|
</Section>
|
|
)}
|
|
|
|
{adding && (
|
|
<UserDialog
|
|
title="Add user"
|
|
saveLabel="Add"
|
|
onCancel={() => setAdding(false)}
|
|
onSave={async (name, secret, person) => {
|
|
await request('POST', '/users', USER_SHAPE, { name, password: secret, ...person })
|
|
setAdding(false)
|
|
await refresh()
|
|
}}
|
|
/>
|
|
)}
|
|
{details && (
|
|
<PersonDialog
|
|
user={details}
|
|
onCancel={() => 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 && (
|
|
<UserDialog
|
|
title={`Set the password of ${password}`}
|
|
name={password}
|
|
saveLabel="Set"
|
|
onCancel={() => setPassword(null)}
|
|
onSave={async (name, secret) => {
|
|
await request('PUT', `/users/${name}/password`, USER_SHAPE, { password: secret })
|
|
setPassword(null)
|
|
}}
|
|
/>
|
|
)}
|
|
{enrolling && (
|
|
<OtpDialog
|
|
name={enrolling}
|
|
path={`/users/${enrolling}/otp`}
|
|
onCancel={() => setEnrolling(null)}
|
|
onSave={async (secret, code) => {
|
|
await request('PUT', `/users/${enrolling}/otp`, USER_SHAPE, { secret, code })
|
|
setEnrolling(null)
|
|
await refresh()
|
|
}}
|
|
/>
|
|
)}
|
|
{appleProfile && (
|
|
<ProfileDialog kind="apple" name={appleProfile} path={`/users/${appleProfile}/apple-profile`} own={false} otp={false} onClose={() => setAppleProfile(null)} />
|
|
)}
|
|
{confirm && <ConfirmDialog {...confirm} />}
|
|
</Box>
|
|
)
|
|
}
|