From f3080e9468b0f156fc8766f1fb1b0bad274eea78 Mon Sep 17 00:00:00 2001 From: Aine <97398200+aine-etke@users.noreply.github.com> Date: Sun, 29 Sep 2024 00:16:44 +0300 Subject: [PATCH] Provide options to delete media and redact events on user erase (#49) * Provide options to delete media and redact events on user erase * update readme --- README.md | 3 +- src/components/DeleteUserButton.tsx | 111 ++++++++++++++++++++++++++++ src/i18n/de.ts | 5 +- src/i18n/en.ts | 5 +- src/i18n/fa.ts | 4 + src/i18n/fr.ts | 3 + src/i18n/index.d.ts | 3 + src/i18n/it.ts | 8 +- src/i18n/ru.ts | 3 + src/i18n/zh.ts | 3 + src/resources/users.tsx | 20 ++--- src/synapse/dataProvider.ts | 53 ++++++++++++- 12 files changed, 205 insertions(+), 16 deletions(-) create mode 100644 src/components/DeleteUserButton.tsx diff --git a/README.md b/README.md index 590d839..8011b54 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ The following changes are already implemented: * [Upgrade react-admin to v5](https://github.com/etkecc/synapse-admin/pull/40) * [Restrict actions on specific users](https://github.com/etkecc/synapse-admin/pull/42) * [Add `Contact support` menu item](https://github.com/etkecc/synapse-admin/pull/45) +* [Provide options to delete media and redact events on user erase](https://github.com/etkecc/synapse-admin/pull/49) _the list will be updated as new changes are added_ @@ -136,7 +137,7 @@ Synapse-Admin provides a support link in the main menu - `Contact support`. By d ### Supported Synapse -It needs at least [Synapse](https://github.com/element-hq/synapse) v1.93.0 for all functions to work as expected! +It needs at least [Synapse](https://github.com/element-hq/synapse) v1.116.0 for all functions to work as expected! You get your server version with the request `/_synapse/admin/v1/server_version`. See also [Synapse version API](https://element-hq.github.io/synapse/latest/admin_api/version_api.html). diff --git a/src/components/DeleteUserButton.tsx b/src/components/DeleteUserButton.tsx new file mode 100644 index 0000000..c9c34b7 --- /dev/null +++ b/src/components/DeleteUserButton.tsx @@ -0,0 +1,111 @@ +import { Button, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle } from "@mui/material"; +import { Fragment, useState } from "react"; +import { SimpleForm, BooleanInput, useTranslate, RaRecord, useNotify, useRedirect, useDelete, NotificationType, useDeleteMany, Identifier, useUnselectAll } from "react-admin"; +import ActionDelete from "@mui/icons-material/Delete"; +import ActionCheck from "@mui/icons-material/CheckCircle"; +import AlertError from "@mui/icons-material/ErrorOutline"; + +interface DeleteUserButtonProps { + selectedIds: Identifier[]; + confirmTitle: string; + confirmContent: string; +} + +const resourceName = "users"; + +const DeleteUserButton: React.FC = (props) => { + const translate = useTranslate(); + const [open, setOpen] = useState(false); + const [deleteMedia, setDeleteMedia] = useState(false); + const [redactEvents, setRedactEvents] = useState(false); + + const notify = useNotify(); + const redirect = useRedirect(); + + const [deleteMany, { isLoading }] = useDeleteMany(); + const unselectAll = useUnselectAll(resourceName); + const recordIds = props.selectedIds; + + const handleDialogOpen = () => setOpen(true); + const handleDialogClose = () => setOpen(false); + + const handleDelete = (values: {deleteMedia: boolean, redactEvents: boolean}) => { + deleteMany( + resourceName, + { ids: recordIds, meta: values }, + { + onSuccess: () => { + handleDialogClose(); + unselectAll(); + redirect("/users"); + }, + onError: (error) => + notify("ra.notification.data_provider_error", { type: 'error' as NotificationType }), + } + ); + }; + + const handleConfirm = () => { + setOpen(false); + handleDelete({ deleteMedia: deleteMedia, redactEvents: redactEvents }); + }; + + return ( + + + + {translate(props.confirmTitle)} + + {translate(props.confirmContent)} + + ) => setDeleteMedia(event.target.checked)} + label="resources.users.action.delete_media" + defaultValue={false} + /> + ) => setRedactEvents(event.target.checked)} + label="resources.users.action.redact_events" + defaultValue={false} + /> + + + + + + + + + ); +}; + +export default DeleteUserButton; diff --git a/src/i18n/de.ts b/src/i18n/de.ts index b8615f7..aef4bb6 100644 --- a/src/i18n/de.ts +++ b/src/i18n/de.ts @@ -151,12 +151,15 @@ const de: SynapseTranslationMessages = { password: "Durch die Änderung des Passworts wird der Benutzer von allen Sitzungen abgemeldet.", deactivate: "Sie müssen ein Passwort angeben, um ein Konto wieder zu aktivieren.", erase: "DSGVO konformes Löschen der Benutzerdaten.", + erase_text: "Das bedeutet, dass die von dem/den Benutzer(n) gesendeten Nachrichten für alle, die zum Zeitpunkt des Sendens im Raum waren, sichtbar bleiben, aber für Benutzer, die dem Raum später beitreten, nicht sichtbar sind.", erase_admin_error: "Das Löschen des eigenen Benutzers ist nicht erlaubt.", modify_managed_user_error: "Das Ändern eines vom System verwalteten Benutzers ist nicht zulässig.", }, action: { erase: "Lösche Benutzerdaten", - erase_avatar: "Avatar löschen" + erase_avatar: "Avatar löschen", + delete_media: "Alle von dem/den Benutzer(n) hochgeladenen Medien löschen", + redact_events: "Schwärzen aller vom Benutzer gesendeten Ereignisse (-s)", }, }, rooms: { diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 8893241..93ef69d 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -142,12 +142,15 @@ const en: SynapseTranslationMessages = { password: "Changing password will log user out of all sessions.", deactivate: "You must provide a password to re-activate an account.", erase: "Mark the user as GDPR-erased", + erase_text: "This means messages sent by the user(-s) will still be visible by anyone who was in the room when these messages were sent, but hidden from users joining the room afterward.", erase_admin_error: "Deleting own user is not allowed.", modify_managed_user_error: "Modifying a system-managed user is not allowed.", }, action: { erase: "Erase user data", - erase_avatar: "Erase avatar" + erase_avatar: "Erase avatar", + delete_media: "Delete all media uploaded by the user(-s)", + redact_events: "Redact all events sent by the user(-s)", }, }, rooms: { diff --git a/src/i18n/fa.ts b/src/i18n/fa.ts index e0d5a96..7d79569 100644 --- a/src/i18n/fa.ts +++ b/src/i18n/fa.ts @@ -138,11 +138,15 @@ const fa: SynapseTranslationMessages = { password: "با تغییر رمز عبور کاربر از تمام دستگاه ها خارج می شود.", deactivate: "برای فعالسازی مجدد حساب باید رمز عبور وارد کنید.", erase: "کاربر را به عنوان GDPR پاک شده علامت گذاری کنید", + erase_text: "وهذا يعني أن الرسائل المرسلة من قبل المستخدم (المستخدمين) ستظل مرئية من قبل أي شخص كان في الغرفة عند إرسال هذه الرسائل، ولكنها مخفية عن المستخدمين الذين ينضمون إلى الغرفة بعد ذلك.", erase_admin_error: "حذف المستخدم الخاص غير مسموح به.", modify_managed_user_error: "لا يُسمح بتغيير المستخدم الذي يديره النظام.", }, action: { erase: "پاک کردن اطلاعات کاربر", + erase_avatar: "محو الصورة الرمزية", + delete_media: "حذف جميع الوسائط التي تم تحميلها بواسطة المستخدم (المستخدمين)", + redact_events: "تنقيح جميع الأحداث المرسلة من قبل المستخدم (-s)", }, }, rooms: { diff --git a/src/i18n/fr.ts b/src/i18n/fr.ts index fdd62ca..c77422f 100644 --- a/src/i18n/fr.ts +++ b/src/i18n/fr.ts @@ -140,12 +140,15 @@ const fr: SynapseTranslationMessages = { helper: { deactivate: "Vous devrez fournir un mot de passe pour réactiver le compte.", erase: "Marquer l'utilisateur comme effacé conformément au RGPD", + erase_text: "Cela signifie que les messages envoyés par le(s) utilisateur(s) seront toujours visibles par toute personne qui se trouvait dans la salle au moment où ces messages ont été envoyés, mais qu'ils seront cachés aux utilisateurs qui rejoindront la salle par la suite.", erase_admin_error: "La suppression de son propre utilisateur n'est pas autorisée.", modify_managed_user_error: "La modification d'un utilisateur géré par le système n'est pas autorisée.", }, action: { erase: "Effacer les données de l'utilisateur", erase_avatar: "Effacer l'avatar", + delete_media: "Supprimer tous les médias téléchargés par le(s) utilisateur(s)", + redact_events: "Expurger tous les événements envoyés par l'utilisateur(-s)", }, }, rooms: { diff --git a/src/i18n/index.d.ts b/src/i18n/index.d.ts index bda6651..7d03720 100644 --- a/src/i18n/index.d.ts +++ b/src/i18n/index.d.ts @@ -138,12 +138,15 @@ interface SynapseTranslationMessages extends TranslationMessages { password?: string; deactivate: string; erase: string; + erase_text: string; erase_admin_error: string; modify_managed_user_error: string; }; action: { erase: string; erase_avatar: string; + delete_media: string; + redact_events: string; }; }; rooms: { diff --git a/src/i18n/it.ts b/src/i18n/it.ts index eb27b96..db188d9 100644 --- a/src/i18n/it.ts +++ b/src/i18n/it.ts @@ -139,11 +139,15 @@ const it: SynapseTranslationMessages = { password: "Cambiando la password l'utente verrà disconnesso da tutte le sessioni attive.", deactivate: "Devi fornire una password per riattivare l'account.", erase: "Constrassegna l'utente come cancellato dal GDPR", + erase_text: "Ciò significa che i messaggi inviati dall'utente (o dagli utenti) saranno ancora visibili da chiunque si trovasse nella stanza al momento dell'invio, ma saranno nascosti agli utenti che si uniranno alla stanza in seguito.", + erase_admin_error: "Non è consentito eliminare il proprio utente.", + modify_managed_user_error: "La modifica di un utente gestito dal sistema non è consentita.", }, action: { erase: "Cancella i dati dell'utente", - erase_admin_error: "Non è consentito eliminare il proprio utente.", - modify_managed_user_error: "La modifica di un utente gestito dal sistema non è consentita.", + erase_avatar: "Cancella l'avatar dell'utente", + delete_media: "Elimina tutti i media caricati dall'utente(-s)", + redact_events: "Ridurre tutti gli eventi inviati dall'utente(-s)", }, }, rooms: { diff --git a/src/i18n/ru.ts b/src/i18n/ru.ts index f4de1fa..2db8278 100644 --- a/src/i18n/ru.ts +++ b/src/i18n/ru.ts @@ -159,12 +159,15 @@ const ru: SynapseTranslationMessages = { password: "Смена пароля завершит все сессии пользователя.", deactivate: "Вы должны предоставить пароль для реактивации учётной записи.", erase: "Пометить пользователя как удалённого в соответствии с GDPR", + erase_text: "Это означает, что сообщения, отправленные пользователем (-ами), будут по-прежнему видны всем, кто находился в комнате в момент их отправки, но будут скрыты от пользователей, присоединившихся к комнате после этого.", erase_admin_error: "Удаление собственного пользователя запрещено.", modify_managed_user_error: "Изменение пользователя, управляемого системой, не допускается.", }, action: { erase: "Удалить данные пользователя", erase_avatar: "Удалить аватар", + delete_media: "Удаление всех медиафайлов, загруженных пользователем (-ами)", + redact_events: "Удаление всех событий, отправленных пользователем (-ами)", }, }, rooms: { diff --git a/src/i18n/zh.ts b/src/i18n/zh.ts index 27ff47a..0064a15 100644 --- a/src/i18n/zh.ts +++ b/src/i18n/zh.ts @@ -143,12 +143,15 @@ const zh: SynapseTranslationMessages = { helper: { deactivate: "您必须提供一串密码来激活账户。", erase: "将用户标记为根据 GDPR 的要求抹除了", + erase_text: "这意味着用户发送的信息对于发送信息时在房间内的任何人来说都是可见的,但对于之后加入房间的用户来说则是隐藏的。", erase_admin_error: "不允许删除自己的用户", modify_managed_user_error: "不允许修改系统管理的用户。", }, action: { erase: "抹除用户信息", erase_avatar: "抹掉头像", + delete_media: "删除用户上传的所有媒体", + redact_events: "重新编辑用户(-s)发送的所有事件", }, }, rooms: { diff --git a/src/resources/users.tsx b/src/resources/users.tsx index c16220c..7a3830e 100644 --- a/src/resources/users.tsx +++ b/src/resources/users.tsx @@ -36,7 +36,6 @@ import { ResourceProps, SearchInput, SelectInput, - BulkDeleteButton, DeleteButton, maxLength, regex, @@ -61,6 +60,7 @@ import { import { Link } from "react-router-dom"; import AvatarField from "../components/AvatarField"; +import DeleteUserButton from "../components/DeleteUserButton"; import { isASManaged } from "../components/mxid"; import { ServerNoticeButton, ServerNoticeBulkButton } from "../components/ServerNotices"; import { DATE_FORMAT } from "../components/date"; @@ -141,10 +141,10 @@ const UserBulkActionButtons = () => { <> - @@ -200,15 +200,15 @@ const UserEditActions = () => { return ( {!record?.deactivated && } + {record && record.id && ( - + )} ); }; diff --git a/src/synapse/dataProvider.ts b/src/synapse/dataProvider.ts index 6c66dc0..7d546b3 100644 --- a/src/synapse/dataProvider.ts +++ b/src/synapse/dataProvider.ts @@ -56,6 +56,12 @@ const filterUndefined = (obj: Record) => { return Object.fromEntries(Object.entries(obj).filter(([key, value]) => value !== undefined)); }; +interface Action { + endpoint: string; + method?: string; + body?: Record; +} + interface Room { room_id: string; name?: string; @@ -282,6 +288,24 @@ const resourceMap = { body: { erase: true }, method: "POST", }), + afterDelete: (params: DeleteParams) => { + let actions : Action[] = []; + if (params.meta?.deleteMedia) { + actions.push({ + endpoint: `/_synapse/admin/v1/users/${encodeURIComponent(returnMXID(params.id))}/media`, + method: "DELETE", + }); + } + + if (params.meta?.redactEvents) { + actions.push({ + endpoint: `/_synapse/admin/v1/users/${encodeURIComponent(returnMXID(params.id))}/redact`, + method: "POST", + }); + } + + return actions; + }, }, rooms: { path: "/_synapse/admin/v1/rooms", @@ -702,6 +726,17 @@ const baseDataProvider: SynapseDataProvider = { method: "method" in del ? del.method : "DELETE", body: "body" in del ? JSON.stringify(del.body) : null, }); + + if ("afterDelete" in res) { + const del = res.afterDelete(params); + del.map(async action => { + const endpoint_url = homeserver + action.endpoint; + return jsonClient(endpoint_url, { + method: "method" in action ? action.method : "DELETE", + body: "body" in action ? JSON.stringify(action.body) : null, + }); + }); + } return { data: json }; } else { const endpoint_url = homeserver + res.path; @@ -714,7 +749,7 @@ const baseDataProvider: SynapseDataProvider = { }, deleteMany: async (resource, params) => { - console.log("deleteMany " + resource); + console.log("deleteMany " + resource, "params", params); const homeserver = storage.getItem("base_url"); if (!homeserver || !(resource in resourceMap)) throw Error("Homeserver not set"); @@ -731,6 +766,22 @@ const baseDataProvider: SynapseDataProvider = { }); }) ); + + if ("afterDelete" in res) { + await Promise.all( + params.ids.map(id => { + const del = res.afterDelete({ ...params, id: id }); + del.map(async action => { + const endpoint_url = homeserver + action.endpoint; + return jsonClient(endpoint_url, { + method: "method" in action ? action.method : "DELETE", + body: "body" in action ? JSON.stringify(action.body) : null, + }); + }); + }) + ); + } + return { data: responses.map(({ json }) => json), };