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

Commit c070223

Browse files
Fix a bug that deactivated users appear in the directory (#8933)
Fixes a bug that deactivated users appear in the directory when their profile information was updated. To change profile information of deactivated users is neccesary for example you will remove displayname or avatar. But they should not appear in directory. They are deactivated. Co-authored-by: Erik Johnston <[email protected]>
1 parent 0600605 commit c070223

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

changelog.d/8933.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug where deactivated users appeared in the user directory when their profile information was updated.

synapse/handlers/user_directory.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,13 @@ async def handle_local_profile_change(self, user_id, profile):
113113
"""
114114
# FIXME(#3714): We should probably do this in the same worker as all
115115
# the other changes.
116-
is_support = await self.store.is_support_user(user_id)
116+
117117
# Support users are for diagnostics and should not appear in the user directory.
118-
if not is_support:
118+
is_support = await self.store.is_support_user(user_id)
119+
# When change profile information of deactivated user it should not appear in the user directory.
120+
is_deactivated = await self.store.get_user_deactivated_status(user_id)
121+
122+
if not (is_support or is_deactivated):
119123
await self.store.update_profile_in_user_dir(
120124
user_id, profile.display_name, profile.avatar_url
121125
)

tests/handlers/test_user_directory.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def test_handle_local_profile_change_with_support_user(self):
5454
user_id=support_user_id, password_hash=None, user_type=UserTypes.SUPPORT
5555
)
5656
)
57+
regular_user_id = "@regular:test"
58+
self.get_success(
59+
self.store.register_user(user_id=regular_user_id, password_hash=None)
60+
)
5761

5862
self.get_success(
5963
self.handler.handle_local_profile_change(support_user_id, None)
@@ -63,13 +67,47 @@ def test_handle_local_profile_change_with_support_user(self):
6367
display_name = "display_name"
6468

6569
profile_info = ProfileInfo(avatar_url="avatar_url", display_name=display_name)
66-
regular_user_id = "@regular:test"
6770
self.get_success(
6871
self.handler.handle_local_profile_change(regular_user_id, profile_info)
6972
)
7073
profile = self.get_success(self.store.get_user_in_directory(regular_user_id))
7174
self.assertTrue(profile["display_name"] == display_name)
7275

76+
def test_handle_local_profile_change_with_deactivated_user(self):
77+
# create user
78+
r_user_id = "@regular:test"
79+
self.get_success(
80+
self.store.register_user(user_id=r_user_id, password_hash=None)
81+
)
82+
83+
# update profile
84+
display_name = "Regular User"
85+
profile_info = ProfileInfo(avatar_url="avatar_url", display_name=display_name)
86+
self.get_success(
87+
self.handler.handle_local_profile_change(r_user_id, profile_info)
88+
)
89+
90+
# profile is in directory
91+
profile = self.get_success(self.store.get_user_in_directory(r_user_id))
92+
self.assertTrue(profile["display_name"] == display_name)
93+
94+
# deactivate user
95+
self.get_success(self.store.set_user_deactivated_status(r_user_id, True))
96+
self.get_success(self.handler.handle_user_deactivated(r_user_id))
97+
98+
# profile is not in directory
99+
profile = self.get_success(self.store.get_user_in_directory(r_user_id))
100+
self.assertTrue(profile is None)
101+
102+
# update profile after deactivation
103+
self.get_success(
104+
self.handler.handle_local_profile_change(r_user_id, profile_info)
105+
)
106+
107+
# profile is furthermore not in directory
108+
profile = self.get_success(self.store.get_user_in_directory(r_user_id))
109+
self.assertTrue(profile is None)
110+
73111
def test_handle_user_deactivated_support_user(self):
74112
s_user_id = "@support:test"
75113
self.get_success(

tests/rest/admin/test_user.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ def prepare(self, reactor, clock, hs):
603603
self.admin_user = self.register_user("admin", "pass", admin=True)
604604
self.admin_user_tok = self.login("admin", "pass")
605605

606-
self.other_user = self.register_user("user", "pass")
606+
self.other_user = self.register_user("user", "pass", displayname="User")
607607
self.other_user_token = self.login("user", "pass")
608608
self.url_other_user = "/_synapse/admin/v2/users/%s" % urllib.parse.quote(
609609
self.other_user
@@ -1012,6 +1012,54 @@ def test_deactivate_user(self):
10121012
self.assertEqual("@user:test", channel.json_body["name"])
10131013
self.assertEqual(True, channel.json_body["deactivated"])
10141014

1015+
@override_config({"user_directory": {"enabled": True, "search_all_users": True}})
1016+
def test_change_name_deactivate_user_user_directory(self):
1017+
"""
1018+
Test change profile information of a deactivated user and
1019+
check that it does not appear in user directory
1020+
"""
1021+
1022+
# is in user directory
1023+
profile = self.get_success(self.store.get_user_in_directory(self.other_user))
1024+
self.assertTrue(profile["display_name"] == "User")
1025+
1026+
# Deactivate user
1027+
body = json.dumps({"deactivated": True})
1028+
1029+
request, channel = self.make_request(
1030+
"PUT",
1031+
self.url_other_user,
1032+
access_token=self.admin_user_tok,
1033+
content=body.encode(encoding="utf_8"),
1034+
)
1035+
1036+
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
1037+
self.assertEqual("@user:test", channel.json_body["name"])
1038+
self.assertEqual(True, channel.json_body["deactivated"])
1039+
1040+
# is not in user directory
1041+
profile = self.get_success(self.store.get_user_in_directory(self.other_user))
1042+
self.assertTrue(profile is None)
1043+
1044+
# Set new displayname user
1045+
body = json.dumps({"displayname": "Foobar"})
1046+
1047+
request, channel = self.make_request(
1048+
"PUT",
1049+
self.url_other_user,
1050+
access_token=self.admin_user_tok,
1051+
content=body.encode(encoding="utf_8"),
1052+
)
1053+
1054+
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"])
1055+
self.assertEqual("@user:test", channel.json_body["name"])
1056+
self.assertEqual(True, channel.json_body["deactivated"])
1057+
self.assertEqual("Foobar", channel.json_body["displayname"])
1058+
1059+
# is not in user directory
1060+
profile = self.get_success(self.store.get_user_in_directory(self.other_user))
1061+
self.assertTrue(profile is None)
1062+
10151063
def test_reactivate_user(self):
10161064
"""
10171065
Test reactivating another user.

0 commit comments

Comments
 (0)