From 7493e9b8514f04d98904b89043335759bee6f9ee Mon Sep 17 00:00:00 2001 From: carolineBda Date: Thu, 26 Sep 2024 12:24:50 +0200 Subject: [PATCH 1/6] feat(anonymisation): anonymiser les utiliseurs quand on le supprime en mettant leur initiales --- targets/frontend/src/components/user/List.tsx | 4 ++-- .../src/modules/authentification/deleteUser.ts | 17 +++++++++++++++-- targets/frontend/src/pages/api/users/delete.ts | 3 ++- targets/frontend/src/pages/users/index.tsx | 4 ++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/targets/frontend/src/components/user/List.tsx b/targets/frontend/src/components/user/List.tsx index d6508f743..f5757e2d0 100644 --- a/targets/frontend/src/components/user/List.tsx +++ b/targets/frontend/src/components/user/List.tsx @@ -31,7 +31,7 @@ query getUsers { `; type Props = { - onDeleteUser: (userId: string) => Promise; + onDeleteUser: (userId: string, name: string) => Promise; refresh?: boolean; }; @@ -57,7 +57,7 @@ export function UserList({ onDeleteUser }: Props) { return; } close(); - const result = await onDeleteUser(selectedUser.id); + const result = await onDeleteUser(selectedUser.id, selectedUser.name); if (result) { executeQuery({ requestPolicy: "network-only" }); } diff --git a/targets/frontend/src/modules/authentification/deleteUser.ts b/targets/frontend/src/modules/authentification/deleteUser.ts index 93fa936b5..ce400a28f 100644 --- a/targets/frontend/src/modules/authentification/deleteUser.ts +++ b/targets/frontend/src/modules/authentification/deleteUser.ts @@ -23,12 +23,25 @@ interface DeleteUserHasuraResult { }; } -export const deleteUser = async (userId: string): Promise => { +const anonymizeUser = (userName: string, userId: string): string => { + if (!userName?.length) return userId.slice(4); + let anonymous = userName[0].toUpperCase(); + const spaceIndex = userName.indexOf(" "); + if (spaceIndex && userName.length > spaceIndex) { + anonymous += userName[spaceIndex + 1].toUpperCase(); + } + return anonymous; +}; + +export const deleteUser = async ( + userId: string, + userName: string +): Promise => { const deleteResult = await gqlClient() .mutation(deleteQuery, { email: `${userId}@gouv.fr`, id: userId, - name: userId, + name: anonymizeUser(userName, userId), }) .toPromise(); diff --git a/targets/frontend/src/pages/api/users/delete.ts b/targets/frontend/src/pages/api/users/delete.ts index d1f77ca9c..a8e2f469b 100644 --- a/targets/frontend/src/pages/api/users/delete.ts +++ b/targets/frontend/src/pages/api/users/delete.ts @@ -14,6 +14,7 @@ export default async function handler( } const userId = req.body.userId; + const userName = req.body.name; if (!userId) { res.status(400).json({ message: "Missing user id" }); @@ -36,7 +37,7 @@ export default async function handler( return; } - const result = await deleteUser(userId); + const result = await deleteUser(userId, userName); if (!result) { res.status(500).json({ message: "Error deleting user" }); diff --git a/targets/frontend/src/pages/users/index.tsx b/targets/frontend/src/pages/users/index.tsx index fe4674868..b273c528f 100644 --- a/targets/frontend/src/pages/users/index.tsx +++ b/targets/frontend/src/pages/users/index.tsx @@ -9,13 +9,13 @@ import { useRouter } from "next/router"; export function UserPage() { const router = useRouter(); - const onDeleteUser = async (userId: string) => { + const onDeleteUser = async (userId: string, name: string) => { const result = await fetch(`/api/users/delete`, { method: "DELETE", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ userId }), + body: JSON.stringify({ userId, name }), }); const resultJson = await result.json(); From cc09eb8f6e73c14abb8f679414bd882cee9171dd Mon Sep 17 00:00:00 2001 From: carolineBda Date: Thu, 26 Sep 2024 13:39:47 +0200 Subject: [PATCH 2/6] =?UTF-8?q?ajout=20des=20migrations=20pour=20mettre=20?= =?UTF-8?q?=C3=A0=20jour=20les=20anciens=20utilisateurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../down.sql | 1 + .../1727349866123_put_back_users_initials/up.sql | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 targets/hasura/migrations/default/1727349866123_put_back_users_initials/down.sql create mode 100644 targets/hasura/migrations/default/1727349866123_put_back_users_initials/up.sql diff --git a/targets/hasura/migrations/default/1727349866123_put_back_users_initials/down.sql b/targets/hasura/migrations/default/1727349866123_put_back_users_initials/down.sql new file mode 100644 index 000000000..8389d76a5 --- /dev/null +++ b/targets/hasura/migrations/default/1727349866123_put_back_users_initials/down.sql @@ -0,0 +1 @@ +-- alter table "contribution"."answers" alter column "display_date" drop not null; diff --git a/targets/hasura/migrations/default/1727349866123_put_back_users_initials/up.sql b/targets/hasura/migrations/default/1727349866123_put_back_users_initials/up.sql new file mode 100644 index 000000000..6c05673f0 --- /dev/null +++ b/targets/hasura/migrations/default/1727349866123_put_back_users_initials/up.sql @@ -0,0 +1,15 @@ +UPDATE auth.users +SET name = 'CL' +WHERE id = '62a6e4c1-2624-4dfb-b984-0d206ec8a43e'; + +UPDATE auth.users +SET name = 'FD' +WHERE id = '474702f8-7f7e-4f05-92b2-45796a075e0f'; + +UPDATE auth.users +SET name = 'AB' +WHERE id = '1baef8d6-e871-46b4-8150-1f587b9f56cd'; + +UPDATE auth.users +SET name = 'CL' +WHERE id = 'aa6d1721-71e5-42a8-bf26-98f453d1fab5'; From 815ce43463be89ec98fd525a435b2e9da7ed6682 Mon Sep 17 00:00:00 2001 From: carolineBda Date: Thu, 26 Sep 2024 14:47:12 +0200 Subject: [PATCH 3/6] update maj --- targets/frontend/src/components/user/List.tsx | 12 ++++++------ .../src/modules/authentification/deleteUser.ts | 6 +++--- targets/frontend/src/pages/api/users/delete.ts | 2 +- targets/frontend/src/pages/users/index.tsx | 5 ++--- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/targets/frontend/src/components/user/List.tsx b/targets/frontend/src/components/user/List.tsx index f5757e2d0..223c7a3d8 100644 --- a/targets/frontend/src/components/user/List.tsx +++ b/targets/frontend/src/components/user/List.tsx @@ -31,7 +31,7 @@ query getUsers { `; type Props = { - onDeleteUser: (userId: string, name: string) => Promise; + onDeleteUser: (userId: string, userName: string) => Promise; refresh?: boolean; }; @@ -47,8 +47,8 @@ export function UserList({ onDeleteUser }: Props) { const { data, fetching, error } = result; const users = data?.users || []; - function confirmDeleteUser(id: string, email: string) { - setSelectedUser({ email, id }); + function confirmDeleteUser(id: string, email: string, userName:string) { + setSelectedUser({ email, id, userName }); open(); } @@ -57,7 +57,7 @@ export function UserList({ onDeleteUser }: Props) { return; } close(); - const result = await onDeleteUser(selectedUser.id, selectedUser.name); + const result = await onDeleteUser(selectedUser.id, selectedUser.userName); if (result) { executeQuery({ requestPolicy: "network-only" }); } @@ -78,7 +78,7 @@ export function UserList({ onDeleteUser }: Props) { ariaLabel="Supprimer l'utilisateur" >

Etes vous sur de vouloir supprimer l’utilisateur

- {selectedUser?.email} + {selectedUser?.userName} ({selectedUser?.email})