Simplify checking if admin

This commit is contained in:
Erik Johnston 2017-07-12 11:43:39 +01:00
parent a62406aaa5
commit 8d55877c9e

View file

@ -50,7 +50,7 @@ class GroupsServerHandler(object):
hs.get_groups_attestation_renewer() hs.get_groups_attestation_renewer()
@defer.inlineCallbacks @defer.inlineCallbacks
def check_group_is_ours(self, group_id, and_exists=False): def check_group_is_ours(self, group_id, and_exists=False, and_is_admin=None):
"""Check that the group is ours, and optionally if it exists. """Check that the group is ours, and optionally if it exists.
If group does exist then return group. If group does exist then return group.
@ -62,6 +62,11 @@ class GroupsServerHandler(object):
if and_exists and not group: if and_exists and not group:
raise SynapseError(404, "Unknown group") raise SynapseError(404, "Unknown group")
if and_is_admin:
is_admin = yield self.store.is_user_admin_in_group(group_id, and_is_admin)
if not is_admin:
raise SynapseError(403, "User is not admin in group")
defer.returnValue(group) defer.returnValue(group)
@defer.inlineCallbacks @defer.inlineCallbacks
@ -128,11 +133,7 @@ class GroupsServerHandler(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def update_group_summary_room(self, group_id, user_id, room_id, category_id, content): def update_group_summary_room(self, group_id, user_id, room_id, category_id, content):
yield self.check_group_is_ours(group_id, and_exists=True) yield self.check_group_is_ours(group_id, and_exists=True, and_is_admin=user_id)
is_admin = yield self.store.is_user_admin_in_group(group_id, user_id)
if not is_admin:
raise SynapseError(403, "User is not admin in group")
order = content.get("order", None) order = content.get("order", None)
@ -150,11 +151,7 @@ class GroupsServerHandler(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def delete_group_summary_room(self, group_id, user_id, room_id, category_id): def delete_group_summary_room(self, group_id, user_id, room_id, category_id):
yield self.check_group_is_ours(group_id, and_exists=True) yield self.check_group_is_ours(group_id, and_exists=True, and_is_admin=user_id)
is_admin = yield self.store.is_user_admin_in_group(group_id, user_id)
if not is_admin:
raise SynapseError(403, "User is not admin in group")
yield self.store.remove_room_from_summary( yield self.store.remove_room_from_summary(
group_id=group_id, group_id=group_id,
@ -186,11 +183,7 @@ class GroupsServerHandler(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def update_group_category(self, group_id, user_id, category_id, content): def update_group_category(self, group_id, user_id, category_id, content):
yield self.check_group_is_ours(group_id, and_exists=True) yield self.check_group_is_ours(group_id, and_exists=True, and_is_admin=user_id)
is_admin = yield self.store.is_user_admin_in_group(group_id, user_id)
if not is_admin:
raise SynapseError(403, "User is not admin in group")
is_public = _parse_visibility_from_contents(content) is_public = _parse_visibility_from_contents(content)
profile = content.get("profile") profile = content.get("profile")
@ -206,11 +199,7 @@ class GroupsServerHandler(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def delete_group_category(self, group_id, user_id, category_id): def delete_group_category(self, group_id, user_id, category_id):
yield self.check_group_is_ours(group_id, and_exists=True) yield self.check_group_is_ours(group_id, and_exists=True, and_is_admin=user_id)
is_admin = yield self.store.is_user_admin_in_group(group_id, user_id)
if not is_admin:
raise SynapseError(403, "User is not admin in group")
yield self.store.remove_group_category( yield self.store.remove_group_category(
group_id=group_id, group_id=group_id,
@ -240,11 +229,7 @@ class GroupsServerHandler(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def update_group_role(self, group_id, user_id, role_id, content): def update_group_role(self, group_id, user_id, role_id, content):
yield self.check_group_is_ours(group_id, and_exists=True) yield self.check_group_is_ours(group_id, and_exists=True, and_is_admin=user_id)
is_admin = yield self.store.is_user_admin_in_group(group_id, user_id)
if not is_admin:
raise SynapseError(403, "User is not admin in group")
is_public = _parse_visibility_from_contents(content) is_public = _parse_visibility_from_contents(content)
@ -261,11 +246,7 @@ class GroupsServerHandler(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def delete_group_role(self, group_id, user_id, role_id): def delete_group_role(self, group_id, user_id, role_id):
yield self.check_group_is_ours(group_id, and_exists=True) yield self.check_group_is_ours(group_id, and_exists=True, and_is_admin=user_id)
is_admin = yield self.store.is_user_admin_in_group(group_id, user_id)
if not is_admin:
raise SynapseError(403, "User is not admin in group")
yield self.store.remove_group_role( yield self.store.remove_group_role(
group_id=group_id, group_id=group_id,
@ -277,11 +258,7 @@ class GroupsServerHandler(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def update_group_summary_user(self, group_id, requester_user_id, user_id, role_id, def update_group_summary_user(self, group_id, requester_user_id, user_id, role_id,
content): content):
yield self.check_group_is_ours(group_id, and_exists=True) yield self.check_group_is_ours(group_id, and_exists=True, and_is_admin=user_id)
is_admin = yield self.store.is_user_admin_in_group(group_id, requester_user_id)
if not is_admin:
raise SynapseError(403, "User is not admin in group")
order = content.get("order", None) order = content.get("order", None)
@ -299,11 +276,7 @@ class GroupsServerHandler(object):
@defer.inlineCallbacks @defer.inlineCallbacks
def delete_group_summary_user(self, group_id, requester_user_id, user_id, role_id): def delete_group_summary_user(self, group_id, requester_user_id, user_id, role_id):
yield self.check_group_is_ours(group_id, and_exists=True) yield self.check_group_is_ours(group_id, and_exists=True, and_is_admin=user_id)
is_admin = yield self.store.is_user_admin_in_group(group_id, requester_user_id)
if not is_admin:
raise SynapseError(403, "User is not admin in group")
yield self.store.remove_user_from_summary( yield self.store.remove_user_from_summary(
group_id=group_id, group_id=group_id,
@ -419,12 +392,9 @@ class GroupsServerHandler(object):
def add_room(self, group_id, requester_user_id, room_id, content): def add_room(self, group_id, requester_user_id, room_id, content):
"""Add room to group """Add room to group
""" """
yield self.check_group_is_ours(
yield self.check_group_is_ours(group_id, and_exists=True) group_id, and_exists=True, and_is_admin=requester_user_id
)
is_admin = yield self.store.is_user_admin_in_group(group_id, requester_user_id)
if not is_admin:
raise SynapseError(403, "User is not admin in group")
# TODO: Check if room has already been added # TODO: Check if room has already been added
@ -439,13 +409,9 @@ class GroupsServerHandler(object):
"""Invite user to group """Invite user to group
""" """
group = yield self.check_group_is_ours(group_id, and_exists=True) group = yield self.check_group_is_ours(
group_id, and_exists=True, and_is_admin=requester_user_id
is_admin = yield self.store.is_user_admin_in_group(
group_id, requester_user_id
) )
if not is_admin:
raise SynapseError(403, "User is not admin in group")
# TODO: Check if user knocked # TODO: Check if user knocked
# TODO: Check if user is already invited # TODO: Check if user is already invited