def _add_channel_role(self, role: str, channel_id: str, user_id: str): self.get_channel_name(channel_id) roles = self.redis.hget(RedisKeys.channel_roles(channel_id), user_id) if roles is None: roles = role else: roles = set(str(roles, 'utf-8').split(',')) roles.add(role) roles = ','.join(roles) self.redis.hset(RedisKeys.channel_roles(channel_id), user_id, roles)
def _remove_channel_role(self, role: str, channel_id: str, user_id: str): roles = self.redis.hget(RedisKeys.channel_roles(channel_id), user_id) if roles is None: return roles = set(str(roles, 'utf-8').split(',')) if role not in roles: return roles.remove(role) roles = ','.join(roles) self.redis.hset(RedisKeys.channel_roles(channel_id), user_id, roles)
def get_user_roles(self, user_id: str) -> dict: output = { 'global': list(), 'channel': dict(), 'room': dict() } checked_channels = set() rooms = self.redis.hgetall(RedisKeys.rooms_for_user(user_id)) global_roles = self.redis.hget(RedisKeys.global_roles(), user_id) if global_roles is not None: global_roles = str(global_roles, 'utf-8') output['global'] = [a for a in global_roles.split(',')] for room_id, _ in rooms.items(): room_id = str(room_id, 'utf-8') channel_id = self.channel_for_room(room_id) room_roles = self.redis.hget(RedisKeys.room_roles(room_id), user_id) if channel_id not in checked_channels: checked_channels.add(channel_id) channel_roles = self.redis.hget(RedisKeys.channel_roles(channel_id), user_id) if channel_roles is not None: channel_roles = str(channel_roles, 'utf-8') output['channel'][channel_id] = [a for a in channel_roles.split(',')] if room_roles is not None: room_roles = str(room_roles, 'utf-8') output['room'][room_id] = [a for a in room_roles.split(',')] return output
def create_channel(self, room_id=None, room_name=None): if room_id is None: room_id = BaseTest.ROOM_ID if room_name is None: room_name = BaseTest.ROOM_NAME environ.env.db.redis.hset(RedisKeys.rooms(BaseTest.CHANNEL_ID), room_id, room_name) environ.env.db.redis.hset(RedisKeys.channels(), BaseTest.CHANNEL_ID, BaseTest.CHANNEL_NAME) environ.env.db.redis.hset(RedisKeys.channel_roles(BaseTest.CHANNEL_ID), BaseTest.USER_ID, RoleKeys.OWNER) environ.env.db.redis.hset(RedisKeys.auth_key(BaseTest.USER_ID), SessionKeys.user_name.value, BaseTest.USER_NAME) environ.env.db.redis.hset(RedisKeys.channel_for_rooms(), room_id, BaseTest.CHANNEL_ID) environ.env.db.redis.hset(RedisKeys.room_name_for_id(), room_id, room_name) environ.env.cache.set_channel_exists(BaseTest.CHANNEL_ID)
def _has_role_in_channel(self, role: str, channel_id: str, user_id: str) -> bool: roles = self.redis.hget(RedisKeys.channel_roles(channel_id), user_id) if roles is None: return False return role in str(roles, 'utf-8').split(',')
def _get_users_with_role_in_channel(self, channel_id: str, role_key: str): roles = self.redis.hgetall(RedisKeys.channel_roles(channel_id)) return self._get_users_with_role(roles, role_key)
def remove_owner_channel(self): environ.env.storage.redis.hdel( RedisKeys.channel_roles(BaseTest.CHANNEL_ID), BaseTest.USER_ID)