Files
device-management/resources/js/pages/devices/settings.tsx
T
Wian Drs 1e80be180e
tests / ci (push) Has been cancelled
Initial commit device-management
2026-08-25 09:42:00 +07:00

361 lines
18 KiB
TypeScript

import { Form, Head } from '@inertiajs/react';
import { useEffect, useRef, useState } from 'react';
import InputError from '@/components/input-error';
import { Button } from '@/components/ui/button';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
type Props = {
setting: {
base_username: string;
use_tls: boolean;
verify_tls: boolean;
connection_timeout: number;
} | null;
hasPassword: boolean;
encryptionKey: {
version: number;
source: string;
created_at: string;
} | null;
devices: {
id: number;
name: string;
base_sync_status: string;
base_sync_error_code?: string;
base_sync_message?: string;
base_synced_at?: string;
vendor: { name: string };
}[];
};
export default function DeviceSettings({
setting,
hasPassword,
encryptionKey,
devices,
}: Props) {
const [keyMode, setKeyMode] = useState('generated');
const [loginPassword, setLoginPassword] = useState('');
const [revealedPassword, setRevealedPassword] = useState('');
const [revealError, setRevealError] = useState('');
const [revealing, setRevealing] = useState(false);
const hideTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
useEffect(() => () => {
if (hideTimer.current) {
clearTimeout(hideTimer.current);
}
}, []);
const revealBasePassword = async () => {
setRevealing(true);
setRevealError('');
setRevealedPassword('');
try {
const csrf = document.querySelector<HTMLMetaElement>('meta[name="csrf-token"]')?.content ?? '';
const response = await fetch('/device-settings/reveal-password', {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'X-CSRF-TOKEN': csrf },
body: JSON.stringify({ current_password: loginPassword }),
});
const result = await response.json();
if (!response.ok) {
throw new Error(result.errors?.current_password?.[0] ?? result.message ?? 'Password tidak dapat ditampilkan.');
}
setRevealedPassword(result.password);
setLoginPassword('');
if (hideTimer.current) {
clearTimeout(hideTimer.current);
}
hideTimer.current = setTimeout(() => setRevealedPassword(''), 30000);
} catch (error) {
setRevealError(error instanceof Error ? error.message : 'Password tidak dapat ditampilkan.');
} finally {
setRevealing(false);
}
};
return (
<>
<Head title="Pengaturan Perangkat" />
<div className="mx-auto flex w-full max-w-4xl flex-1 flex-col gap-6 p-4 md:p-6">
<div>
<h1 className="text-2xl font-semibold">
Base User Perangkat
</h1>
<p className="text-sm text-muted-foreground">
Akun management terpusat untuk seluruh perangkat tenant.
Perubahan username atau password akan disinkronkan ke
semua perangkat yang aktif.
</p>
</div>
<Form action="/device-settings" method="put">
{({ processing, errors }) => (
<Card>
<CardHeader>
<CardTitle>
Template Base User Tenant
</CardTitle>
<CardDescription>
Password disimpan terenkripsi dan tidak
pernah ditampilkan kembali.
</CardDescription>
</CardHeader>
<CardContent className="grid gap-5 md:grid-cols-2">
<div className="grid gap-2">
<Label>Base Username</Label>
<Input
name="base_username"
defaultValue={
setting?.base_username ??
'radiq-manager'
}
required
/>
<InputError
message={errors.base_username}
/>
</div>
<div className="grid gap-2">
<Label>Connection Timeout (detik)</Label>
<Input
name="connection_timeout"
type="number"
min="3"
max="30"
defaultValue={
setting?.connection_timeout ?? 10
}
required
/>
</div>
<div className="grid gap-2">
<Label>
{hasPassword
? 'Password Baru (kosongkan jika tetap)'
: 'Base Password'}
</Label>
<Input
name="base_password"
type="password"
required={!hasPassword}
autoComplete="new-password"
/>
<InputError
message={errors.base_password}
/>
</div>
<div className="grid gap-2">
<Label>Konfirmasi Password</Label>
<Input
name="base_password_confirmation"
type="password"
required={!hasPassword}
autoComplete="new-password"
/>
</div>
<label className="flex items-center gap-2">
<input
type="hidden"
name="use_tls"
value="0"
/>
<input
type="checkbox"
name="use_tls"
value="1"
defaultChecked={setting?.use_tls}
/>
Gunakan API-SSL/TLS (umumnya port 8729)
</label>
<label className="flex items-center gap-2">
<input
type="hidden"
name="verify_tls"
value="0"
/>
<input
type="checkbox"
name="verify_tls"
value="1"
defaultChecked={
setting?.verify_tls ?? true
}
/>
Verifikasi sertifikat TLS
</label>
<div className="rounded-lg border bg-muted/40 p-4 text-sm md:col-span-2">
<strong>Group yang dibuat:</strong>{' '}
RADIQ-READ, RADIQ-WRITE, dan RADIQ-NOC tanpa
permission <code>policy</code>, serta
group bawaan full untuk akun provisioning
terpusat.
</div>
{hasPassword && (
<div className="grid gap-3 rounded-lg border border-amber-200 bg-amber-50 p-4 text-sm text-amber-950 md:col-span-2">
<div>
<strong>Lihat Base Password</strong>
<p>Masukkan password login Tenant Admin. Password perangkat akan disembunyikan otomatis setelah 30 detik.</p>
</div>
<div className="flex flex-col gap-2 md:flex-row">
<Input
type="password"
value={loginPassword}
onChange={(event) => setLoginPassword(event.target.value)}
placeholder="Password login Tenant Admin"
autoComplete="current-password"
/>
<Button type="button" variant="outline" disabled={revealing || !loginPassword} onClick={revealBasePassword}>
{revealing ? 'Memverifikasi...' : 'Lihat Password'}
</Button>
</div>
{revealError && <p className="text-destructive">{revealError}</p>}
{revealedPassword && (
<Input value={revealedPassword} readOnly className="font-mono" aria-label="Base Password" />
)}
</div>
)}
<div className="flex justify-end md:col-span-2">
<Button disabled={processing}>
Simpan Pengaturan
</Button>
</div>
</CardContent>
</Card>
)}
</Form>
<Card>
<CardHeader>
<CardTitle>Status Rotasi Base User</CardTitle>
<CardDescription>
Perangkat offline akan dicoba kembali oleh scheduler.
Vendor tanpa driver ditandai unsupported.
</CardDescription>
</CardHeader>
<CardContent className="space-y-2">
{devices.map((device) => (
<div key={device.id} className="flex flex-wrap items-center justify-between gap-3 rounded-lg border p-3 text-sm">
<div>
<div className="font-medium">{device.name}</div>
<div className="text-xs text-muted-foreground">{device.vendor.name} · {device.base_sync_message ?? 'Belum pernah disinkronkan'}</div>
</div>
<span className="rounded-full bg-muted px-2 py-1 text-xs uppercase">{device.base_sync_status}</span>
</div>
))}
{devices.length === 0 && <p className="py-4 text-center text-sm text-muted-foreground">Belum ada perangkat.</p>}
</CardContent>
</Card>
<Form
action="/device-settings/rotate-key"
method="post"
resetOnSuccess
>
{({ processing, errors }) => (
<Card>
<CardHeader>
<CardTitle>Encryption Key Tenant</CardTitle>
<CardDescription>
Key aktif versi{' '}
{encryptionKey?.version ?? 1} (
{encryptionKey?.source ?? 'generated'}).
Rotasi mengenkripsi ulang seluruh password
perangkat dan Base Password pada database.
</CardDescription>
</CardHeader>
<CardContent className="grid gap-5 md:grid-cols-2">
<div className="grid gap-2">
<Label>Metode Key Baru</Label>
<select
name="mode"
value={keyMode}
onChange={(event) =>
setKeyMode(event.target.value)
}
className="h-10 rounded-md border bg-background px-3"
>
<option value="generated">
Generate otomatis (direkomendasikan)
</option>
<option value="manual">
Masukkan manual
</option>
</select>
</div>
<div className="grid gap-2">
<Label>Password Tenant Admin</Label>
<Input
name="current_password"
type="password"
required
autoComplete="current-password"
/>
<InputError
message={errors.current_password}
/>
</div>
{keyMode === 'manual' && (
<>
<div className="grid gap-2">
<Label>
Key Manual (minimal 32 karakter)
</Label>
<Input
name="manual_key"
type="password"
minLength={32}
required
autoComplete="new-password"
/>
<InputError
message={errors.manual_key}
/>
</div>
<div className="grid gap-2">
<Label>Konfirmasi Key Manual</Label>
<Input
name="manual_key_confirmation"
type="password"
minLength={32}
required
autoComplete="new-password"
/>
</div>
</>
)}
<div className="rounded-lg border border-amber-200 bg-amber-50 p-4 text-sm text-amber-900 md:col-span-2">
Rotasi key tidak mengubah password yang
berada di router/OLT. Perubahan password
perangkat dilakukan melalui operasi rotasi
credential terpisah dan harus diverifikasi
login ulang.
</div>
<div className="flex justify-end md:col-span-2">
<Button
disabled={processing}
variant="destructive"
>
Rotasi Encryption Key
</Button>
</div>
</CardContent>
</Card>
)}
</Form>
</div>
</>
);
}