Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(anonymisation): anonymiser les utiliseurs quand on le supprime en mettant leur initiales #1480

Merged
merged 6 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions targets/frontend/src/components/user/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ query getUsers {
`;

type Props = {
onDeleteUser: (userId: string) => Promise<boolean>;
onDeleteUser: (userId: string, userName: string) => Promise<boolean>;
refresh?: boolean;
};

Expand All @@ -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();
}

Expand All @@ -57,7 +57,7 @@ export function UserList({ onDeleteUser }: Props) {
return;
}
close();
const result = await onDeleteUser(selectedUser.id);
const result = await onDeleteUser(selectedUser.id, selectedUser.userName);
if (result) {
executeQuery({ requestPolicy: "network-only" });
}
Expand All @@ -78,7 +78,7 @@ export function UserList({ onDeleteUser }: Props) {
ariaLabel="Supprimer l'utilisateur"
>
<p>Etes vous sur de vouloir supprimer l’utilisateur</p>
<strong>{selectedUser?.email}</strong>
<strong>{selectedUser?.userName} ({selectedUser?.email})</strong>
<Stack direction="row" spacing={2} mt={4} justifyContent="end">
<Button variant="outlined" onClick={close}>
Annuler
Expand Down Expand Up @@ -117,7 +117,7 @@ export function UserList({ onDeleteUser }: Props) {
</TableCell>
<TableCell align="center">
<MenuButton variant="contained">
<MenuItem onClick={() => confirmDeleteUser(id, email)}>
<MenuItem onClick={() => confirmDeleteUser(id, email, name)}>
Supprimer
</MenuItem>
</MenuButton>
Expand Down
23 changes: 18 additions & 5 deletions targets/frontend/src/modules/authentification/deleteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,41 @@ const deleteQuery = gql`
isDeleted: true
}
) {
name
id
}
}
`;

interface DeleteUserHasuraResult {
update_auth_users_by_pk: {
name: string;
id: string;
};
}

export const deleteUser = async (userId: string): Promise<boolean> => {
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 > 0 && userName.length > spaceIndex) {
anonymous += userName[spaceIndex + 1].toUpperCase();
}
return anonymous;
carolineBda marked this conversation as resolved.
Show resolved Hide resolved
};

export const deleteUser = async (
userId: string,
userName: string
): Promise<boolean> => {
const deleteResult = await gqlClient()
.mutation<DeleteUserHasuraResult>(deleteQuery, {
email: `${userId}@gouv.fr`,
id: userId,
name: userId,
name: anonymizeUser(userName, userId),
})
.toPromise();

if (
deleteResult.data?.update_auth_users_by_pk.name !== userId ||
deleteResult.data?.update_auth_users_by_pk.id !== userId ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Du coup on avait un bug ici ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non avant comme le nom était l'id on checkait le nom et ça marchait mais maintenant il faut bien checker sur l'id

deleteResult.error
) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion targets/frontend/src/pages/api/users/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default async function handler(
}

const userId = req.body.userId;
const userName = req.body.userName;

if (!userId) {
res.status(400).json({ message: "Missing user id" });
Expand All @@ -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" });
Expand Down
5 changes: 2 additions & 3 deletions targets/frontend/src/pages/users/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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, userName: string) => {
const result = await fetch(`/api/users/delete`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ userId }),
body: JSON.stringify({ userId, userName }),
});

const resultJson = await result.json();
Expand All @@ -26,7 +26,6 @@ export function UserPage() {
}

alert("L'utilisateur a été supprimé avec succès");

return true;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- alter table "contribution"."answers" alter column "display_date" drop not null;
Original file line number Diff line number Diff line change
@@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ca a du être sympa de retrouver les personnes 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

j'ai regardé dans le fichier audit ^^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pratique cette table audit :)

Loading