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

Commit

Permalink
switch to use handler instead of store
Browse files Browse the repository at this point in the history
  • Loading branch information
dklimpel committed Dec 12, 2020
1 parent 46373ae commit 9298d3b
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 13 deletions.
14 changes: 10 additions & 4 deletions synapse/handlers/deactivate_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from synapse.api.errors import SynapseError
from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.types import UserID, create_requester
from synapse.types import Requester, UserID, create_requester

from ._base import BaseHandler

Expand All @@ -38,6 +38,7 @@ def __init__(self, hs: "HomeServer"):
self._device_handler = hs.get_device_handler()
self._room_member_handler = hs.get_room_member_handler()
self._identity_handler = hs.get_identity_handler()
self._profile_handler = hs.get_profile_handler()
self.user_directory_handler = hs.get_user_directory_handler()
self._server_name = hs.hostname

Expand All @@ -52,13 +53,18 @@ def __init__(self, hs: "HomeServer"):
self._account_validity_enabled = hs.config.account_validity.enabled

async def deactivate_account(
self, user_id: str, erase_data: bool, id_server: Optional[str] = None
self,
user_id: str,
erase_data: bool,
requester: Requester,
id_server: Optional[str] = None,
) -> bool:
"""Deactivate a user's account
Args:
user_id: ID of user to be deactivated
erase_data: whether to GDPR-erase the user's data
requester: The user attempting to make this change.
id_server: Use the given identity server when unbinding
any threepids. If None then will attempt to unbind using the
identity server specified when binding (if known).
Expand Down Expand Up @@ -104,8 +110,8 @@ async def deactivate_account(
await self.store.user_delete_threepids(user_id)

# Remove avatar URL from this user
await self.store.set_profile_avatar_url(
UserID.from_string(user_id).localpart, None
await self._profile_handler.set_avatar_url(
UserID.from_string(user_id), requester, "", by_admin=True
)

# delete any devices belonging to the user, which will also
Expand Down
8 changes: 7 additions & 1 deletion synapse/handlers/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,19 @@ async def set_avatar_url(
400, "Avatar URL is too long (max %i)" % (MAX_AVATAR_URL_LEN,)
)

avatar_url_to_set = new_avatar_url # type: Optional[str]
if new_avatar_url == "":
avatar_url_to_set = None

# Same like set_displayname
if by_admin:
requester = create_requester(
target_user, authenticated_entity=requester.authenticated_entity
)

await self.store.set_profile_avatar_url(target_user.localpart, new_avatar_url)
await self.store.set_profile_avatar_url(
target_user.localpart, avatar_url_to_set
)

if self.hs.config.user_directory_search_all_users:
profile = await self.store.get_profileinfo(target_user.localpart)
Expand Down
5 changes: 3 additions & 2 deletions synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ async def on_PUT(self, request, user_id):

if deactivate and not user["deactivated"]:
await self.deactivate_account_handler.deactivate_account(
target_user.to_string(), False
target_user.to_string(), False, requester
)
elif not deactivate and user["deactivated"]:
if "password" not in body:
Expand Down Expand Up @@ -503,6 +503,7 @@ def __init__(self, hs):

async def on_POST(self, request, target_user_id):
await assert_requester_is_admin(self.auth, request)
requester = await self.auth.get_user_by_req(request)
body = parse_json_object_from_request(request, allow_empty_body=True)
erase = body.get("erase", False)
if not isinstance(erase, bool):
Expand All @@ -515,7 +516,7 @@ async def on_POST(self, request, target_user_id):
UserID.from_string(target_user_id)

result = await self._deactivate_account_handler.deactivate_account(
target_user_id, erase
target_user_id, erase, requester
)
if result:
id_server_unbind_result = "success"
Expand Down
7 changes: 5 additions & 2 deletions synapse/rest/client/v2_alpha/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ async def on_POST(self, request):
# allow ASes to deactivate their own users
if requester.app_service:
await self._deactivate_account_handler.deactivate_account(
requester.user.to_string(), erase
requester.user.to_string(), erase, requester
)
return 200, {}

Expand All @@ -312,7 +312,10 @@ async def on_POST(self, request):
"deactivate your account",
)
result = await self._deactivate_account_handler.deactivate_account(
requester.user.to_string(), erase, id_server=body.get("id_server")
requester.user.to_string(),
erase,
requester,
id_server=body.get("id_server"),
)
if result:
id_server_unbind_result = "success"
Expand Down
2 changes: 1 addition & 1 deletion synapse/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def get_initial_sync_handler(self) -> InitialSyncHandler:
return InitialSyncHandler(self)

@cache_in_self
def get_profile_handler(self):
def get_profile_handler(self) -> ProfileHandler:
return ProfileHandler(self)

@cache_in_self
Expand Down
32 changes: 32 additions & 0 deletions tests/handlers/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ def test_set_my_name(self):
"Frank",
)

# Set displayname to None
yield defer.ensureDeferred(
self.handler.set_displayname(
self.frank, synapse.types.create_requester(self.frank), ""
)
)

self.assertEquals(
(
yield defer.ensureDeferred(
self.store.get_profile_displayname(self.frank.localpart)
)
),
None,
)

@defer.inlineCallbacks
def test_set_my_name_if_disabled(self):
self.hs.config.enable_set_displayname = False
Expand Down Expand Up @@ -223,6 +239,22 @@ def test_set_my_avatar(self):
"http://my.server/me.png",
)

# Set avatar to None
yield defer.ensureDeferred(
self.handler.set_avatar_url(
self.frank, synapse.types.create_requester(self.frank), "",
)
)

self.assertEquals(
(
yield defer.ensureDeferred(
self.store.get_profile_avatar_url(self.frank.localpart)
)
),
None,
)

@defer.inlineCallbacks
def test_set_my_avatar_if_disabled(self):
self.hs.config.enable_set_avatar_url = False
Expand Down
5 changes: 4 additions & 1 deletion tests/rest/client/v1/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from synapse.rest.client.v1 import login, logout
from synapse.rest.client.v2_alpha import devices, register
from synapse.rest.client.v2_alpha.account import WhoamiRestServlet
from synapse.types import create_requester

from tests import unittest
from tests.unittest import override_config
Expand Down Expand Up @@ -462,7 +463,9 @@ def test_deactivated_user(self):

# Deactivate the account.
self.get_success(
self.deactivate_account_handler.deactivate_account(self.user_id, False)
self.deactivate_account_handler.deactivate_account(
self.user_id, False, create_requester(self.user_id)
)
)

# Request the CAS ticket.
Expand Down
6 changes: 4 additions & 2 deletions tests/rest/client/v1/test_rooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from synapse.handlers.pagination import PurgeStatus
from synapse.rest.client.v1 import directory, login, profile, room
from synapse.rest.client.v2_alpha import account
from synapse.types import JsonDict, RoomAlias, UserID
from synapse.types import JsonDict, RoomAlias, UserID, create_requester
from synapse.util.stringutils import random_string

from tests import unittest
Expand Down Expand Up @@ -1681,7 +1681,9 @@ def test_erased_sender(self):

deactivate_account_handler = self.hs.get_deactivate_account_handler()
self.get_success(
deactivate_account_handler.deactivate_account(self.user_id, erase_data=True)
deactivate_account_handler.deactivate_account(
self.user_id, True, create_requester(self.user_id)
)
)

# Invite another user in the room. This is needed because messages will be
Expand Down
28 changes: 28 additions & 0 deletions tests/storage/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ def test_displayname(self):
),
)

# test set to None
yield defer.ensureDeferred(
self.store.set_profile_displayname(self.u_frank.localpart, None)
)

self.assertEquals(
None,
(
yield defer.ensureDeferred(
self.store.get_profile_displayname(self.u_frank.localpart)
)
),
)

@defer.inlineCallbacks
def test_avatar_url(self):
yield defer.ensureDeferred(self.store.create_profile(self.u_frank.localpart))
Expand All @@ -66,3 +80,17 @@ def test_avatar_url(self):
)
),
)

# test set to None
yield defer.ensureDeferred(
self.store.set_profile_avatar_url(self.u_frank.localpart, None)
)

self.assertEquals(
None,
(
yield defer.ensureDeferred(
self.store.get_profile_avatar_url(self.u_frank.localpart)
)
),
)

0 comments on commit 9298d3b

Please sign in to comment.