示例#1
0
class KickResource(BaseResource):
    def __init__(self):
        super(KickResource, self).__init__()
        self.user_manager = UserManager(environ.env)
        self.request = request

    @timeit(logger, 'on_rest_kick')
    def do_post(self):
        is_valid, msg, json = self.validate_json()
        output = dict()
        if not is_valid:
            logger.error('invalid json: %s' % msg)
            return output

        if json is None:
            raise RuntimeError('no json in request')
        if not isinstance(json, dict):
            raise RuntimeError('need a dict of user-room keys')
        logger.debug('POST request: %s' % str(json))

        for user_id, kick_info in json.items():
            try:
                reason = kick_info.get('reason')
                admin_id = kick_info.get('admin_id')
                room_id = kick_info.get('target')

                self.user_manager.kick_user(room_id, user_id, reason, admin_id)
                output[user_id] = 'OK'
            except Exception:
                logger.error(
                    'no such room when trying to kick user %s for %s' %
                    (user_id, kick_info))
                logger.error(traceback.format_exc())
                output[user_id] = 'FAIL'
                continue
        return output

    def validate_json(self):
        try:
            return True, None, self.request.get_json(silent=False)
        except Exception as e:
            logger.error('error: %s' % str(e))
            logger.exception(traceback.format_exc())
            return False, 'invalid json in request', None
示例#2
0
 def __init__(self):
     super(BroadcastResource, self).__init__()
     self.user_manager = UserManager(environ.env)
     self.request = request
示例#3
0
#!/usr/bin/env python

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__ = 'Oscar Eriksson <*****@*****.**>'

from dino.db.manager import ChannelManager
from dino.db.manager import RoomManager
from dino.db.manager import UserManager
from dino.db.manager import AclManager
from dino.db.manager import StorageManager
from dino.db.manager import BlackListManager
from dino.environ import env

channel_manager = ChannelManager(env)
room_manager = RoomManager(env)
user_manager = UserManager(env)
storage_manager = StorageManager(env)
acl_manager = AclManager(env)
blacklist_manager = BlackListManager(env)
示例#4
0
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__ = 'Oscar Eriksson <*****@*****.**>'

from dino.db.manager import ChannelManager
from dino.db.manager import RoomManager
from dino.db.manager import UserManager
from dino.db.manager import AclManager
from dino.db.manager import StorageManager
from dino.db.manager import BlackListManager
from dino.db.manager import SpamManager
from dino.db.manager import BroadcastManager
from dino import environ

channel_manager = ChannelManager(environ.env)
room_manager = RoomManager(environ.env)
user_manager = UserManager(environ.env)
storage_manager = StorageManager(environ.env)
acl_manager = AclManager(environ.env)
blacklist_manager = BlackListManager(environ.env)
broadcast_manager = BroadcastManager(environ.env)
spam_manager = SpamManager(environ.env)
示例#5
0
文件: ban.py 项目: imfht/flaskapps
 def __init__(self):
     super(BanResource, self).__init__()
     self.user_manager = UserManager(environ.env)
     self.executor = GreenPool()
     self.request = request
     self.env = environ.env
示例#6
0
文件: ban.py 项目: imfht/flaskapps
class BanResource(BaseResource):
    def __init__(self):
        super(BanResource, self).__init__()
        self.user_manager = UserManager(environ.env)
        self.executor = GreenPool()
        self.request = request
        self.env = environ.env

    def do_post(self):
        try:
            json = self._validate_params()
            self.schedule_execution(json)
            return ok()
        except Exception as e:
            logger.error('could not ban user: %s' % str(e))
            logger.exception(traceback.format_exc())
            self.env.capture_exception(sys.exc_info())
            return fail(str(e))

    def schedule_execution(self, json: dict):
        try:
            # avoid hanging clients
            self.executor.spawn_n(self._do_post, json)
        except Exception as e:
            logger.error('could not schedule ban request: %s' % str(e))
            logger.exception(e)
            self.env.capture_exception(sys.exc_info())

    def _validate_params(self):
        is_valid, msg, json = self.validate_json(self.request, silent=False)
        if not is_valid:
            raise RuntimeError('invalid json: %s' % msg)

        if json is None:
            raise RuntimeError('no json in request')
        if not isinstance(json, dict):
            raise RuntimeError('need a dict of user-room keys')

        for user_id, ban_info in json.items():
            try:
                target_type = ban_info['type']
            except KeyError:
                raise KeyError(
                    'missing target type for user id %s and request %s' %
                    (user_id, ban_info))

            try:
                ban_info['target']
            except KeyError:
                if target_type != 'global':
                    raise KeyError(
                        'missing target id for user id %s and request %s' %
                        (user_id, ban_info))

            try:
                ban_info['duration']
            except KeyError:
                raise KeyError(
                    'missing ban duration for user id %s and request %s' %
                    (user_id, ban_info))

            ban_info.get('reason')
            ban_info.get('admin_id')

        return json

    @timeit(logger, 'on_rest_ban')
    def _do_post(self, json: dict):
        logger.debug('POST request: %s' % str(json))
        for user_id, ban_info in json.items():
            try:
                self.ban_user(user_id, ban_info)
            except Exception as e:
                self.env.capture_exception(sys.exc_info())
                logger.error('could not ban user %s: %s' % (user_id, str(e)))

    def ban_user(self, user_id: str, ban_info: dict):
        target_type = ban_info.get('type', '')
        target_id = ban_info.get('target', '')
        duration = ban_info.get('duration', '')
        reason = ban_info.get('reason', '')
        banner_id = ban_info.get('admin_id', '')

        try:
            user_name = ban_info['name']
            user_name = utils.b64d(user_name)
        except KeyError:
            logger.warning(
                'no name specified in ban info, if we have to create the user it will get the ID as name'
            )
            user_name = user_id

        try:
            self.user_manager.ban_user(user_id,
                                       target_id,
                                       duration,
                                       target_type,
                                       reason=reason,
                                       banner_id=banner_id,
                                       user_name=user_name)
        except ValueError as e:
            logger.error('invalid ban duration "%s" for user %s: %s' %
                         (duration, user_id, str(e)))
            self.env.capture_exception(sys.exc_info())
        except NoSuchUserException as e:
            logger.error('no such user %s: %s' % (user_id, str(e)))
            self.env.capture_exception(sys.exc_info())
        except UnknownBanTypeException as e:
            logger.error('unknown ban type "%s" for user %s: %s' %
                         (target_type, user_id, str(e)))
            self.env.capture_exception(sys.exc_info())
        except Exception as e:
            logger.error('could not ban user %s: %s' % (user_id, str(e)))
            logger.error(traceback.format_exc())
            self.env.capture_exception(sys.exc_info())