Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 6c439bb

Browse files
committed
Update check_can_deactivate_user to not take a Requester
1 parent e66329f commit 6c439bb

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

docs/modules/third_party_rules_callbacks.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ async def check_can_shutdown_room(
161161

162162
Called when an admin user requests the shutdown of a room. The module must return a
163163
boolean indicating whether the shutdown can go through. If the callback returns `False`,
164-
the shutdown will not proceed and the caller will see a `M_FOBIDDEN` error.
164+
the shutdown will not proceed and the caller will see a `M_FORBIDDEN` error.
165165

166166
If multiple modules implement this callback, they will be considered in order. If a
167167
callback returns `True`, Synapse falls through to the next one. The value of the first
@@ -174,16 +174,16 @@ _First introduced in Synapse v1.5X.0_
174174

175175
```python
176176
async def check_can_deactivate_user(
177-
requester: "synapse.types.Requester",
178177
user_id: str,
178+
admin_user_id: str|None,
179179
) -> bool:
180180
```
181181

182182
Called when the deactivation of a user is requested. User deactivation can be
183183
performed by an admin or the user themselves, so developers are encouraged to check the
184184
requester when implementing this callback. The module must return a
185185
boolean indicating whether the deactivation can go through. If the callback returns `False`,
186-
the deactivation will not proceed and the caller will see a `M_FOBIDDEN` error.
186+
the deactivation will not proceed and the caller will see a `M_FORBIDDEN` error.
187187

188188
If multiple modules implement this callback, they will be considered in order. If a
189189
callback returns `True`, Synapse falls through to the next one. The value of the first

synapse/events/third_party_rules.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
]
4040
ON_NEW_EVENT_CALLBACK = Callable[[EventBase, StateMap[EventBase]], Awaitable]
4141
CHECK_CAN_SHUTDOWN_ROOM_CALLBACK = Callable[[str, str], Awaitable[bool]]
42-
CHECK_CAN_DEACTIVATE_USER_CALLBACK = Callable[[Requester, str], Awaitable[bool]]
42+
CHECK_CAN_DEACTIVATE_USER_CALLBACK = Callable[[str, str | None], Awaitable[bool]]
4343
ON_PROFILE_UPDATE_CALLBACK = Callable[[str, ProfileInfo, bool, bool], Awaitable]
4444
ON_USER_DEACTIVATION_STATUS_CHANGED_CALLBACK = Callable[[str, bool, bool], Awaitable]
4545

@@ -403,7 +403,9 @@ async def check_can_shutdown_room(self, user_id: str, room_id: str) -> bool:
403403
return True
404404

405405
async def check_can_deactivate_user(
406-
self, requester: Requester, user_id: str
406+
self,
407+
user_id: str,
408+
admin_user_id: str | None,
407409
) -> bool:
408410
"""Intercept requests to deactivate a user. If `False` is returned, the
409411
user should not be deactivated.
@@ -414,7 +416,7 @@ async def check_can_deactivate_user(
414416
"""
415417
for callback in self._check_can_deactivate_user_callbacks:
416418
try:
417-
if await callback(requester, user_id) is False:
419+
if await callback(user_id, admin_user_id) is False:
418420
return False
419421
except Exception as e:
420422
logger.exception(

synapse/handlers/deactivate_account.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async def deactivate_account(
7878

7979
# Check if this user can be deactivated
8080
if not await self._third_party_rules.check_can_deactivate_user(
81-
requester, user_id
81+
user_id, requester.user.to_string() if by_admin else None
8282
):
8383
raise SynapseError(
8484
403, "Deactivation of this user is forbidden", Codes.FORBIDDEN

tests/rest/client/test_third_party_rules.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,11 @@ def test_check_can_deactivate_user(self) -> None:
816816
deactivation_mock.assert_called_once()
817817
args = deactivation_mock.call_args[0]
818818

819-
# Check that the mock was called with the right requester
820-
self.assertEqual(args[0].user.to_string(), user_id)
821-
822819
# Check that the mock was called with the right user ID
823-
self.assertEqual(args[1], user_id)
820+
self.assertEqual(args[0], user_id)
821+
822+
# Check that the admin user ID was not provided
823+
self.assertEqual(args[1], None)
824824

825825
def test_check_can_deactivate_user_admin(self) -> None:
826826
"""Tests that the on_user_deactivation_status_changed module callback is called
@@ -855,11 +855,11 @@ def test_check_can_deactivate_user_admin(self) -> None:
855855
deactivation_mock.assert_called_once()
856856
args = deactivation_mock.call_args[0]
857857

858-
# Check that the mock was called with the right requester
859-
self.assertEqual(args[0].user.to_string(), admin_user_id)
860-
861858
# Check that the mock was called with the right user ID
862-
self.assertEqual(args[1], user_id)
859+
self.assertEqual(args[0], user_id)
860+
861+
# Check that the mock was called with the right admin user id
862+
self.assertEqual(args[1], admin_user_id)
863863

864864
def test_check_can_shutdown_room(self) -> None:
865865
"""Tests that the check_can_shutdown_room module callback is called

0 commit comments

Comments
 (0)