From c5342968466c654d381620b3c7c0844dd8f6c01b Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 20 Jul 2020 08:15:56 -0400 Subject: [PATCH 1/5] Mark users as not erased. --- synapse/handlers/deactivate_account.py | 5 +++- .../data_stores/main/user_erasure_store.py | 24 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/synapse/handlers/deactivate_account.py b/synapse/handlers/deactivate_account.py index 696d85b5f945..fae58799a74e 100644 --- a/synapse/handlers/deactivate_account.py +++ b/synapse/handlers/deactivate_account.py @@ -230,5 +230,8 @@ async def activate_account(self, user_id: str) -> None: Args: user_id: ID of user to be deactivated """ - # Mark the user as activate. + # Ensure the user is not marked as erased. + await self.store.mark_user_not_erased(user_id) + + # Mark the user as active. await self.store.set_user_deactivated_status(user_id, False) diff --git a/synapse/storage/data_stores/main/user_erasure_store.py b/synapse/storage/data_stores/main/user_erasure_store.py index ec6b8a4ffd10..29048d7749a0 100644 --- a/synapse/storage/data_stores/main/user_erasure_store.py +++ b/synapse/storage/data_stores/main/user_erasure_store.py @@ -70,11 +70,11 @@ def are_users_erased(self, user_ids): class UserErasureStore(UserErasureWorkerStore): - def mark_user_erased(self, user_id): + def mark_user_erased(self, user_id: str) -> None: """Indicate that user_id wishes their message history to be erased. Args: - user_id (str): full user_id to be erased + user_id: full user_id to be erased """ def f(txn): @@ -89,3 +89,23 @@ def f(txn): self._invalidate_cache_and_stream(txn, self.is_user_erased, (user_id,)) return self.db.runInteraction("mark_user_erased", f) + + def mark_user_not_erased(self, user_id: str) -> None: + """Indicate that user_id is no longer erased. + + Args: + user_id: full user_id to be un-erased + """ + + def f(txn): + # first check if they are already in the list + txn.execute("SELECT 1 FROM erased_users WHERE user_id = ?", (user_id,)) + if not txn.fetchone(): + return + + # They are there, delete them. + self.simple_delete_one_txn(txn, "erased_users", keyvalues={"user_id": user_id}) + + self._invalidate_cache_and_stream(txn, self.is_user_erased, (user_id,)) + + return self.db.runInteraction("mark_user_not_erased", f) From dc585c2170fcb5fc085468e8e5189fb2f605b985 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 20 Jul 2020 08:21:27 -0400 Subject: [PATCH 2/5] Also re-add the user to the directory. --- synapse/handlers/deactivate_account.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/deactivate_account.py b/synapse/handlers/deactivate_account.py index fae58799a74e..da9dedf8f820 100644 --- a/synapse/handlers/deactivate_account.py +++ b/synapse/handlers/deactivate_account.py @@ -30,6 +30,7 @@ class DeactivateAccountHandler(BaseHandler): def __init__(self, hs): super(DeactivateAccountHandler, self).__init__(hs) + self.hs = hs self._auth_handler = hs.get_auth_handler() self._device_handler = hs.get_device_handler() self._room_member_handler = hs.get_room_member_handler() @@ -222,14 +223,22 @@ async def activate_account(self, user_id: str) -> None: """ Activate an account that was previously deactivated. - This simply marks the user as activate in the database and does not + This marks the user as activate in the database, but does not attempt to rejoin rooms, re-add threepids, etc. The user will also need a password hash set to actually login. Args: - user_id: ID of user to be deactivated + user_id: ID of user to be re-activated """ + # Add the user to the directory, if necessary. + user = UserID.from_string(user_id) + if self.hs.config.user_directory_search_all_users: + profile = await self.store.get_profileinfo(user.localpart) + await self.user_directory_handler.handle_local_profile_change( + user_id, profile + ) + # Ensure the user is not marked as erased. await self.store.mark_user_not_erased(user_id) From b6c62b04f4e3fc0d60637e0026f217103c70ed77 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 20 Jul 2020 08:27:19 -0400 Subject: [PATCH 3/5] Add a newsfragment. --- changelog.d/7908.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/7908.feature diff --git a/changelog.d/7908.feature b/changelog.d/7908.feature new file mode 100644 index 000000000000..4b9a8d85691b --- /dev/null +++ b/changelog.d/7908.feature @@ -0,0 +1 @@ +Add the ability to re-activate an account from the admin API. From e969a3f56f4d2c58f4167b5f974b0d2966eed59e Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Mon, 20 Jul 2020 08:29:19 -0400 Subject: [PATCH 4/5] Lint --- synapse/storage/data_stores/main/user_erasure_store.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/synapse/storage/data_stores/main/user_erasure_store.py b/synapse/storage/data_stores/main/user_erasure_store.py index 29048d7749a0..d3038ff06d1b 100644 --- a/synapse/storage/data_stores/main/user_erasure_store.py +++ b/synapse/storage/data_stores/main/user_erasure_store.py @@ -104,7 +104,9 @@ def f(txn): return # They are there, delete them. - self.simple_delete_one_txn(txn, "erased_users", keyvalues={"user_id": user_id}) + self.simple_delete_one_txn( + txn, "erased_users", keyvalues={"user_id": user_id} + ) self._invalidate_cache_and_stream(txn, self.is_user_erased, (user_id,)) From d06fd486f43f144af029edc6bcf991dadfb5006a Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 21 Jul 2020 07:25:09 -0400 Subject: [PATCH 5/5] Update a docstring. --- synapse/handlers/deactivate_account.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/deactivate_account.py b/synapse/handlers/deactivate_account.py index da9dedf8f820..25169157c1ca 100644 --- a/synapse/handlers/deactivate_account.py +++ b/synapse/handlers/deactivate_account.py @@ -223,8 +223,10 @@ async def activate_account(self, user_id: str) -> None: """ Activate an account that was previously deactivated. - This marks the user as activate in the database, but does not - attempt to rejoin rooms, re-add threepids, etc. + This marks the user as active and not erased in the database, but does + not attempt to rejoin rooms, re-add threepids, etc. + + If enabled, the user will be re-added to the user directory. The user will also need a password hash set to actually login.