From 198b2abe4b00fad2761a131ac6b73ea9d375f2f9 Mon Sep 17 00:00:00 2001 From: Alma Date: Wed, 9 Apr 2025 21:12:54 +0200 Subject: [PATCH] update api users and groups and users 8 --- app/api/users/[userId]/password/route.ts | 11 ++- components/users/users-table.tsx | 101 +++++++++++------------ 2 files changed, 56 insertions(+), 56 deletions(-) diff --git a/app/api/users/[userId]/password/route.ts b/app/api/users/[userId]/password/route.ts index 31ade16c..2fe422eb 100644 --- a/app/api/users/[userId]/password/route.ts +++ b/app/api/users/[userId]/password/route.ts @@ -13,7 +13,7 @@ export async function PUT( } try { - const { password } = await req.json(); + const { password, temporary = true } = await req.json(); // Get client credentials token const tokenResponse = await fetch( @@ -38,7 +38,7 @@ export async function PUT( return NextResponse.json({ error: "Failed to get token" }, { status: 500 }); } - // Reset password + // Reset password with temporary flag const passwordResponse = await fetch( `${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/users/${params.userId}/reset-password`, { @@ -50,7 +50,7 @@ export async function PUT( body: JSON.stringify({ type: "password", value: password, - temporary: false, + temporary: temporary, }), } ); @@ -61,7 +61,10 @@ export async function PUT( return NextResponse.json({ error: "Failed to reset password" }, { status: passwordResponse.status }); } - return NextResponse.json({ success: true }); + return NextResponse.json({ + success: true, + temporary: temporary + }); } catch (error) { console.error("Error in reset password:", error); return NextResponse.json({ error: "Internal server error" }, { status: 500 }); diff --git a/components/users/users-table.tsx b/components/users/users-table.tsx index c4625012..803a11bd 100644 --- a/components/users/users-table.tsx +++ b/components/users/users-table.tsx @@ -179,7 +179,7 @@ export function UsersTable({ userRole = [] }: UsersTableProps) { lastName: user.lastName || "", email: user.email || "", password: "", - roles: user.roles || [], + roles: [], enabled: user.enabled, }); setEditUserDialog(true); @@ -269,6 +269,15 @@ export function UsersTable({ userRole = [] }: UsersTableProps) { await fetchUsers(); + setFormData({ + username: "", + lastName: "", + firstName: "", + email: "", + password: "", + roles: [], + enabled: true, + }); setEditUserDialog(false); setSelectedUser(null); @@ -313,7 +322,7 @@ export function UsersTable({ userRole = [] }: UsersTableProps) { }; const handleChangePassword = async (userId: string) => { - const newPassword = prompt("Entrez le nouveau mot de passe:"); + const newPassword = prompt("Entrez le nouveau mot de passe temporaire:"); if (!newPassword) return; try { @@ -322,7 +331,10 @@ export function UsersTable({ userRole = [] }: UsersTableProps) { headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ password: newPassword }), + body: JSON.stringify({ + password: newPassword, + temporary: true + }), }); if (!response.ok) { @@ -331,7 +343,7 @@ export function UsersTable({ userRole = [] }: UsersTableProps) { toast({ title: "Succès", - description: "Le mot de passe a été modifié avec succès", + description: "Le mot de passe temporaire a été défini avec succès. L'utilisateur devra le changer à sa première connexion.", }); } catch (error) { toast({ @@ -583,7 +595,21 @@ export function UsersTable({ userRole = [] }: UsersTableProps) { - + { + if (!open) { + setFormData({ + username: "", + lastName: "", + firstName: "", + email: "", + password: "", + roles: [], + enabled: true, + }); + setSelectedUser(null); + } + setEditUserDialog(open); + }}> Modifier l'utilisateur @@ -614,57 +640,28 @@ export function UsersTable({ userRole = [] }: UsersTableProps) { onChange={(e) => setFormData(prev => ({ ...prev, email: e.target.value }))} /> -
- -
- {roles.map((role) => ( -
- { - setFormData(prev => ({ - ...prev, - roles: e.target.checked - ? [...prev.roles, role.name] - : prev.roles.filter(r => r !== role.name) - })); - }} - /> - -
- ))} -
-
- {formData.roles.map(role => ( - - {role} - - - ))} -
-
-
- +
+