Skip to content

Commit

Permalink
Allow admins to create aliases when they are not in the room (matrix-…
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep authored and phil-flex committed Jun 16, 2020
1 parent 654a716 commit ddcbe88
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/7191.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Admin users are no longer required to be in a room to create an alias for it.
6 changes: 5 additions & 1 deletion synapse/handlers/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ def create_association(
errcode=Codes.EXCLUSIVE,
)
else:
if self.require_membership and check_membership:
# Server admins are not subject to the same constraints as normal
# users when creating an alias (e.g. being in the room).
is_admin = yield self.auth.is_server_admin(requester.user)

if (self.require_membership and check_membership) and not is_admin:
rooms_for_user = yield self.store.get_rooms_for_user(user_id)
if room_id not in rooms_for_user:
raise AuthError(
Expand Down
62 changes: 62 additions & 0 deletions tests/handlers/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,68 @@ def test_incoming_fed_query(self):
self.assertEquals({"room_id": "!8765asdf:test", "servers": ["test"]}, response)


class TestCreateAlias(unittest.HomeserverTestCase):
servlets = [
synapse.rest.admin.register_servlets,
login.register_servlets,
room.register_servlets,
directory.register_servlets,
]

def prepare(self, reactor, clock, hs):
self.handler = hs.get_handlers().directory_handler

# Create user
self.admin_user = self.register_user("admin", "pass", admin=True)
self.admin_user_tok = self.login("admin", "pass")

# Create a test room
self.room_id = self.helper.create_room_as(
self.admin_user, tok=self.admin_user_tok
)

self.test_alias = "#test:test"
self.room_alias = RoomAlias.from_string(self.test_alias)

# Create a test user.
self.test_user = self.register_user("user", "pass", admin=False)
self.test_user_tok = self.login("user", "pass")
self.helper.join(room=self.room_id, user=self.test_user, tok=self.test_user_tok)

def test_create_alias_joined_room(self):
"""A user can create an alias for a room they're in."""
self.get_success(
self.handler.create_association(
create_requester(self.test_user), self.room_alias, self.room_id,
)
)

def test_create_alias_other_room(self):
"""A user cannot create an alias for a room they're NOT in."""
other_room_id = self.helper.create_room_as(
self.admin_user, tok=self.admin_user_tok
)

self.get_failure(
self.handler.create_association(
create_requester(self.test_user), self.room_alias, other_room_id,
),
synapse.api.errors.SynapseError,
)

def test_create_alias_admin(self):
"""An admin can create an alias for a room they're NOT in."""
other_room_id = self.helper.create_room_as(
self.test_user, tok=self.test_user_tok
)

self.get_success(
self.handler.create_association(
create_requester(self.admin_user), self.room_alias, other_room_id,
)
)


class TestDeleteAlias(unittest.HomeserverTestCase):
servlets = [
synapse.rest.admin.register_servlets,
Expand Down

0 comments on commit ddcbe88

Please sign in to comment.