Exemple #1
0
 def test_not_allowed_different_channel(self):
     act = self.json()
     FakeDb._channel_acls[ApiActions.CROSSROOM] = {'samechannel': ''}
     act['provider']['url'] = BaseWithDb.OTHER_CHANNEL_ID
     allowed = utils.can_send_cross_room(as_parser(act), BaseWithDb.ROOM_ID,
                                         BaseWithDb.OTHER_ROOM_ID)
     self.assertFalse(allowed)
Exemple #2
0
 def test_not_allowed(self):
     act = self.json()
     FakeDb._channel_acls[ApiActions.CROSSROOM] = {'disallow': ''}
     allowed = utils.can_send_cross_room(as_parser(act), BaseWithDb.ROOM_ID, BaseWithDb.OTHER_ROOM_ID)
     self.assertFalse(allowed)
Exemple #3
0
 def test_allowed_same_room(self):
     act = self.json()
     FakeDb._channel_acls[ApiActions.CROSSROOM] = {'samechannel': ''}
     allowed = utils.can_send_cross_room(as_parser(act), BaseWithDb.ROOM_ID, BaseWithDb.ROOM_ID)
     self.assertTrue(allowed)
Exemple #4
0
    def on_message(self, activity: Activity) -> (bool, int, str):
        room_id = activity.target.id
        user_id = activity.actor.id
        object_type = activity.target.object_type
        message = activity.object.content

        from_room_id = None
        if hasattr(activity.actor, 'url'):
            from_room_id = activity.actor.url

        if message is None or len(message.strip()) == 0:
            return False, ECodes.EMPTY_MESSAGE, 'empty message body'

        if not utils.is_base64(message):
            return False, ECodes.NOT_BASE64, 'invalid message content, not base64 encoded'

        if room_id is None or room_id == '':
            return False, ECodes.MISSING_TARGET_ID, 'no room id specified when sending message'

        if object_type not in ['room', 'private']:
            return False, ECodes.INVALID_TARGET_TYPE, \
                   'invalid object_type "%s", must be one of [room, private]' % object_type

        if object_type == 'room':
            channel_id = None
            if hasattr(activity, 'object') and hasattr(activity.object, 'url'):
                channel_id = activity.object.url
            if channel_id is None or len(channel_id.strip()) == 0:
                channel_id = utils.get_channel_for_room(room_id)

            if channel_id is None or channel_id == '':
                return False, ECodes.MISSING_OBJECT_URL, 'no channel id specified when sending message'

            activity.object.url = channel_id
            activity.object.display_name = utils.get_channel_name(channel_id)

            if not utils.channel_exists(channel_id):
                return False, ECodes.NO_SUCH_CHANNEL, 'channel %s does not exists' % channel_id

            if not utils.room_exists(channel_id, room_id):
                return False, ECodes.NO_SUCH_ROOM, 'target room %s does not exist' % room_id

            if from_room_id is not None:
                if from_room_id != room_id and not utils.room_exists(
                        channel_id, from_room_id):
                    return False, ECodes.NO_SUCH_ROOM, 'origin room %s does not exist' % from_room_id

            if not utils.is_user_in_room(user_id, room_id):
                logger.warning('user "%s" is not in room "%s' %
                               (user_id, room_id))
                if from_room_id is None:
                    return False, ECodes.USER_NOT_IN_ROOM, 'user is not in target room'
                if not utils.is_user_in_room(user_id, from_room_id):
                    return False, ECodes.USER_NOT_IN_ROOM, 'user is not in origin room, cannot send message from there'
                if not utils.can_send_cross_room(activity, from_room_id,
                                                 room_id):
                    return False, ECodes.NOT_ALLOWED, \
                           'user not allowed to send cross-room msg from %s to %s' % (from_room_id, room_id)

        elif object_type == 'private':
            channel_id = None
            if hasattr(activity, 'object') and hasattr(activity.object, 'url'):
                channel_id = activity.object.url

            if channel_id is None or len(channel_id.strip()) == 0:
                try:
                    channel_id = utils.get_channel_for_room(room_id)
                except NoSuchRoomException:
                    # TODO: ignore for now, but capture so we can track; a user room won't exist, try to emit anyway
                    environ.env.capture_exception(sys.exc_info())
                    return True, False, False

            if not utils.channel_exists(channel_id):
                return False, ECodes.NO_SUCH_CHANNEL, 'channel %s does not exists' % channel_id
            if not utils.room_exists(channel_id, room_id):
                return False, ECodes.NO_SUCH_ROOM, 'target room %s does not exist' % room_id

        return True, None, None