Exemplo n.º 1
0
    def _do_get(self, user_id):
        # always get from redis here, since the user is not logged in
        session = self.env.auth.get_user_info(user_id)

        channels = self.env.db.get_channels()
        activity = parse_to_as({
            "actor": {
                "id": user_id
            },
            "target": dict(),
            "verb": "filter"
        })

        # filter_channels_by_acl() expects channels in a certain format
        temp_activity = utils.activity_for_list_channels(channels)
        channels_with_acl = temp_activity["object"]["attachments"]

        # filter the channels and replace it on the activity we created
        filtered_channels = utils.filter_channels_by_acl(activity, channels_with_acl, session_to_use=session)
        filtered_rooms = dict()
        channel_names = dict()

        for channel in filtered_channels:
            channel_id = channel["id"]
            channel_names[channel_id] = self.env.db.get_channel_name(channel_id)
            all_rooms_in_channel = self.env.db.rooms_for_channel(channel_id)

            for room_id, room in all_rooms_in_channel.items():
                room["id"] = room_id

                try:
                    acls = utils.get_acls_in_room_for_action(room_id, ApiActions.JOIN)
                except NoSuchRoomException:
                    continue

                activity.target.id = room_id
                is_valid, error_msg = validation.acl.validate_acl_for_action(
                    activity, 
                    ApiTargets.ROOM, 
                    ApiActions.JOIN, 
                    acls,
                    object_type=ApiTargets.ROOM,
                    session_to_use=session
                )
                if not is_valid:
                    logger.info("user {} is not allowed to join room {}".format(user_id, room_id))
                    continue

                is_banned, info_dict = utils.is_banned(user_id, room_id)
                if is_banned:
                    logger.info("user {} is banned from room {}".format(user_id, room_id))
                    continue

                if channel_id not in filtered_rooms:
                    filtered_rooms[channel_id] = list()

                filtered_rooms[channel_id].append(room)

        formatted_rooms = list()
        for channel_id, rooms in filtered_rooms.items():
            for room in rooms:
                formatted_rooms.append({
                    "status": "temporary" if room["ephemeral"] else "static",
                    "users": room["users"],
                    "room_id": room["id"],
                    "room_name": room["name"],
                    "channel_name": channel_names.get(channel_id, ""),
                    "channel_id": channel_id,
                })

        return formatted_rooms
Exemplo n.º 2
0
 def test_is_not_banned(self):
     is_banned, msg = utils.is_banned(BaseWithDb.USER_ID, BaseWithDb.ROOM_ID)
     self.assertFalse(is_banned)
Exemplo n.º 3
0
 def test_is_banned_global(self):
     self.ban_user(target='global')
     is_banned, msg = utils.is_banned(BaseWithDb.USER_ID, BaseWithDb.ROOM_ID)
     self.assertTrue(is_banned)
Exemplo n.º 4
0
    def on_join(self, activity: Activity) -> (bool, int, str):
        room_id = activity.target.id
        room_name = activity.target.display_name
        user_id = environ.env.session.get(SessionKeys.user_id.value, None)

        if user_id is None or len(user_id.strip()) == 0:
            user_id = activity.actor.id

        if room_id is not None and len(room_id.strip()) > 0:
            try:
                room_name = utils.get_room_name(room_id)
            except NoSuchRoomException:
                return False, ECodes.NO_SUCH_ROOM, 'room does not exist'
        else:
            if room_name is None or len(room_name.strip()) == 0:
                return False, ECodes.MISSING_TARGET_DISPLAY_NAME, 'neither room id nor name supplied'

            try:
                room_id = utils.get_room_id(room_name)
            except NoSuchRoomException:
                return False, ECodes.NO_SUCH_ROOM, 'room does not exists with given name'
            except MultipleRoomsFoundForNameException:
                return False, ECodes.MULTIPLE_ROOMS_WITH_NAME, 'found multiple rooms with name "%s"' % room_name

        if not hasattr(activity, 'object'):
            activity.object = DefObject(dict())

        if not utils.user_is_online(user_id):
            user_name = '<unknown>'
            try:
                user_name = utils.get_user_name_for(user_id)
            except NoSuchUserException:
                logger.error('could not get username for user id %s' % user_id)

            logger.warning(
                'user "%s" (%s) is not online, not joining room "%s" (%s)!' %
                (user_name, user_id, room_name, room_id))
            return False, ECodes.NOT_ONLINE, 'user is not online'

        if utils.is_super_user(user_id) or utils.is_global_moderator(user_id):
            return True, None, None
        if utils.is_owner(room_id, user_id):
            return True, None, None

        channel_id = utils.get_channel_for_room(room_id)

        if utils.is_owner_channel(channel_id, user_id):
            return True, None, None

        activity.object.url = channel_id
        activity.object.display_name = utils.get_channel_name(channel_id)
        activity.target.object_type = 'room'

        try:
            acls = utils.get_acls_in_room_for_action(room_id, ApiActions.JOIN)
        except NoSuchRoomException:
            return False, ECodes.NO_SUCH_ROOM, 'no such room'

        is_valid, error_msg = validation.acl.validate_acl_for_action(
            activity, ApiTargets.ROOM, ApiActions.JOIN, acls)
        if not is_valid:
            return False, ECodes.NOT_ALLOWED, error_msg

        is_banned, info_dict = utils.is_banned(user_id, room_id)
        if is_banned:
            scope = info_dict['scope']
            seconds_left = info_dict['seconds']
            target_id = info_dict['id']
            target_name = ''
            if scope == 'room':
                target_name = utils.get_room_name(target_id)
            elif scope == 'channel':
                target_name = utils.get_channel_name(target_id)
            reason = utils.reason_for_ban(user_id, scope, target_id)

            json_act = utils.activity_for_already_banned(
                seconds_left, reason, scope, target_id, target_name)
            return False, ECodes.USER_IS_BANNED, json_act

        return True, None, None