From 915b6224e949e628c1a994a72baf38fe2f7ec777 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 2 Dec 2018 18:09:00 -0300 Subject: [PATCH] permissions: fix overwrite fetching in compute_overwrites - pubsub.guild: only send a chan action to channels user can read --- litecord/permissions.py | 5 +++-- litecord/pubsub/guild.py | 28 +++++++++++++++++----------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/litecord/permissions.py b/litecord/permissions.py index 2c7cee6..21369d8 100644 --- a/litecord/permissions.py +++ b/litecord/permissions.py @@ -177,7 +177,8 @@ async def role_permissions(guild_id: int, role_id: int, return perms -async def compute_overwrites(base_perms, user_id, channel_id: int, +async def compute_overwrites(base_perms: Permissions, + user_id, channel_id: int, guild_id: int = None, storage=None): """Compute the permissions in the context of a channel.""" if not storage: @@ -195,7 +196,7 @@ async def compute_overwrites(base_perms, user_id, channel_id: int, guild_id = await storage.guild_from_channel(channel_id) # make it a map for better usage - overwrites = {o['id']: o for o in overwrites} + overwrites = {int(o['id']): o for o in overwrites} perms = overwrite_find_mix(perms, overwrites, guild_id) diff --git a/litecord/pubsub/guild.py b/litecord/pubsub/guild.py index c53eb60..c687d25 100644 --- a/litecord/pubsub/guild.py +++ b/litecord/pubsub/guild.py @@ -3,6 +3,7 @@ from typing import Any from logbook import Logger from .dispatcher import DispatcherWithState +from litecord.permissions import get_permissions log = Logger(__name__) @@ -12,13 +13,26 @@ class GuildDispatcher(DispatcherWithState): KEY_TYPE = int VAL_TYPE = int - async def _chan_action(self, action: str, guild_id: int, user_id: int): + async def _chan_action(self, action: str, + guild_id: int, user_id: int): """Send an action to all channels of the guild.""" chan_ids = await self.app.storage.get_channel_ids(guild_id) - # TODO: check READ_MESSAGE permissions for the user - for chan_id in chan_ids: + + # only do an action for users that can + # actually read the channel to start with. + chan_perms = await get_permissions( + user_id, chan_id, + storage=self.main_dispatcher.app.storage) + + print(user_id, chan_id, chan_perms.bits.read_messages) + + if not chan_perms.bits.read_messages: + log.debug('skipping cid={}, no read messages', + chan_id) + continue + log.debug('sending raw action {!r} to chan={}', action, chan_id) @@ -42,19 +56,11 @@ class GuildDispatcher(DispatcherWithState): async def sub(self, guild_id: int, user_id: int): """Subscribe a user to the guild.""" await super().sub(guild_id, user_id) - - # when subbing a user to the guild, we should sub them - # to every channel they have access to, in the guild. - - # TODO: check for permissions - await self._chan_action('sub', guild_id, user_id) async def unsub(self, guild_id: int, user_id: int): """Unsubscribe a user from the guild.""" await super().unsub(guild_id, user_id) - - # same thing happening from sub() happens on unsub() await self._chan_action('unsub', guild_id, user_id) async def dispatch_filter(self, guild_id: int, func,