forked from fahrPool/fahrPool
feat: Admin setzt Passwörter, Passwortänderung auch beim Anlegen erzwungen
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
be021f803a
commit
835ce182c8
|
|
@ -10,6 +10,7 @@ import {
|
|||
validateNutzerBearbeiten,
|
||||
istSelbstschutzVerletzt,
|
||||
} from "@/lib/nutzerverwaltung/validate-nutzer";
|
||||
import { validatePasswort } from "@/lib/auth/passwort";
|
||||
import { validateNutzerDetails, leseDetailsAusFormData } from "@/lib/nutzerverwaltung/details";
|
||||
import { speichereDetails } from "@/lib/nutzerverwaltung/details-queries";
|
||||
|
||||
|
|
@ -40,6 +41,7 @@ export async function erstelleNutzer(formData: FormData): Promise<void> {
|
|||
password: validation.passwort,
|
||||
email_confirm: true,
|
||||
user_metadata: { name: validation.name },
|
||||
app_metadata: { must_change_password: true },
|
||||
});
|
||||
|
||||
if (erstellFehler || !erstellt.user) {
|
||||
|
|
@ -167,3 +169,30 @@ export async function entsperreNutzer(nutzerId: string): Promise<ActionResult> {
|
|||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function setzePasswort(nutzerId: string, formData: FormData): Promise<ActionResult> {
|
||||
const profileResult = await loadProfile();
|
||||
if (profileResult.status !== "ok" || !canManageUsers(profileResult.profile.role)) {
|
||||
return { success: false, error: "Nur Admin darf Passwörter setzen." };
|
||||
}
|
||||
|
||||
if (istSelbstschutzVerletzt(profileResult.profile.id, nutzerId)) {
|
||||
return { success: false, error: "Das eigene Passwort kann hier nicht gesetzt werden." };
|
||||
}
|
||||
|
||||
const validation = validatePasswort(formData.get("passwort"));
|
||||
if (!validation.valid) {
|
||||
return { success: false, error: validation.error };
|
||||
}
|
||||
|
||||
const adminClient = createAdminClient();
|
||||
const { error } = await adminClient.auth.admin.updateUserById(nutzerId, {
|
||||
password: validation.passwort,
|
||||
app_metadata: { must_change_password: true },
|
||||
});
|
||||
if (error) {
|
||||
return { success: false, error: `Passwort setzen fehlgeschlagen: ${error.message}` };
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import { Alert } from "@/components/ui/Alert";
|
|||
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
|
||||
import { NutzerDetailsFelder } from "./NutzerDetailsFelder";
|
||||
import { DetailsZusammenfassung } from "./DetailsZusammenfassung";
|
||||
import { PasswortSetzen } from "./PasswortSetzen";
|
||||
|
||||
export function NutzerZeile({ nutzer, eigeneId }: { nutzer: Nutzer; eigeneId: string }) {
|
||||
const router = useRouter();
|
||||
|
|
@ -101,6 +102,9 @@ export function NutzerZeile({ nutzer, eigeneId }: { nutzer: Nutzer; eigeneId: st
|
|||
<div role="cell" className="mt-3 md:col-span-6 md:mt-0">
|
||||
<DetailsZusammenfassung details={nutzer.details} />
|
||||
</div>
|
||||
<div role="cell" className="mt-3 md:col-span-6 md:mt-0">
|
||||
<PasswortSetzen nutzerId={nutzer.id} istEigenerAccount={istEigenerAccount} />
|
||||
</div>
|
||||
{sperrenFehler && (
|
||||
<div className="md:col-span-6">
|
||||
<Alert>{sperrenFehler}</Alert>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,90 @@
|
|||
"use client";
|
||||
|
||||
import { useState, useTransition } from "react";
|
||||
import { setzePasswort } from "@/app/nutzerverwaltung/actions";
|
||||
import { MIN_PASSWORT_LAENGE } from "@/lib/auth/passwort";
|
||||
import { Alert } from "@/components/ui/Alert";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { FormField, FIELD_CLASSES } from "@/components/ui/FormField";
|
||||
|
||||
export function PasswortSetzen({ nutzerId, istEigenerAccount }: { nutzerId: string; istEigenerAccount: boolean }) {
|
||||
const [offen, setOffen] = useState(false);
|
||||
const [fehler, setFehler] = useState<string | null>(null);
|
||||
const [gesetzt, setGesetzt] = useState(false);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
const formData = new FormData(event.currentTarget);
|
||||
startTransition(async () => {
|
||||
const ergebnis = await setzePasswort(nutzerId, formData);
|
||||
if (!ergebnis.success) {
|
||||
setFehler(ergebnis.error);
|
||||
return;
|
||||
}
|
||||
setFehler(null);
|
||||
setGesetzt(true);
|
||||
setOffen(false);
|
||||
});
|
||||
}
|
||||
|
||||
if (!offen) {
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
<div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
fullWidth={false}
|
||||
disabled={istEigenerAccount}
|
||||
onClick={() => {
|
||||
setGesetzt(false);
|
||||
setFehler(null);
|
||||
setOffen(true);
|
||||
}}
|
||||
>
|
||||
Passwort setzen
|
||||
</Button>
|
||||
</div>
|
||||
{gesetzt && (
|
||||
<p role="status" className="text-status-success">
|
||||
Passwort gesetzt. Der Nutzer muss es beim nächsten Login ändern.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit} className="flex flex-col gap-3">
|
||||
{fehler && <Alert>{fehler}</Alert>}
|
||||
<FormField label="Neues Passwort" htmlFor={`passwort-${nutzerId}`}>
|
||||
<input
|
||||
id={`passwort-${nutzerId}`}
|
||||
name="passwort"
|
||||
type="password"
|
||||
required
|
||||
minLength={MIN_PASSWORT_LAENGE}
|
||||
autoComplete="new-password"
|
||||
className={FIELD_CLASSES}
|
||||
/>
|
||||
</FormField>
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<Button type="submit" fullWidth={false} disabled={isPending}>
|
||||
Passwort speichern
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="secondary"
|
||||
fullWidth={false}
|
||||
onClick={() => {
|
||||
setOffen(false);
|
||||
setFehler(null);
|
||||
}}
|
||||
>
|
||||
Abbrechen
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in New Issue