// 管理员重置用户密码弹窗 // Logan.Li import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Key, Loader2 } from 'lucide-react'; import { useToast } from '@/hooks/use-toast'; import { userApi } from '@/api'; import { useTranslation } from 'react-i18next'; interface ResetPasswordDialogProps { user: { id: number | string; username?: string; nickname?: string; authSource?: string } | null; onClose: () => void; } export function ResetPasswordDialog({ user, onClose }: ResetPasswordDialogProps) { const { t } = useTranslation(); const { toast } = useToast(); const [password, setPassword] = useState(''); const [confirm, setConfirm] = useState(''); const [saving, setSaving] = useState(false); const isLdap = user?.authSource === 'ldap'; const canSubmit = password.length >= 6 && password === confirm && !isLdap; const handleReset = async () => { if (!user || !canSubmit) return; setSaving(true); try { await userApi.resetUserPassword(user.id, password); toast({ title: t('system.passwordReset'), description: `用户 ${user.nickname || user.username} 的新密码已生效` }); setPassword(''); setConfirm(''); onClose(); } catch (e: any) { toast({ title: t('system.resetFailed'), description: e?.message || String(e), variant: 'destructive' }); } finally { setSaving(false); } }; return ( { if (!open) { setPassword(''); setConfirm(''); onClose(); } }}> {t('system.resetPassword')} {user ? `为「${user.nickname || user.username}」设置新密码` : ''} {isLdap ? (

{t('system.ldapResetHint')}

) : (
setPassword(e.target.value)} placeholder={t('system.passwordMinLen')} className="mt-1 h-9 bg-[var(--dg-bg-tertiary)] border-[var(--dg-border)]" />
setConfirm(e.target.value)} placeholder={t('system.confirmNewPassword')} className="mt-1 h-9 bg-[var(--dg-bg-tertiary)] border-[var(--dg-border)]" />
)} {!isLdap && ( )}
); }