Esempio n. 1
0
    def _add_and_remove_lines(self, users):
        lines = set((line['id'], user['uuid'], user['tenant_uuid'])
                    for user in users for line in user['lines'])
        lines_cached = set(
            (line.id, str(line.user_uuid), str(line.tenant_uuid))
            for line in self._dao.line.list_())

        lines_missing = lines - lines_cached
        with session_scope():
            for id_, user_uuid, tenant_uuid in lines_missing:
                try:
                    user = self._dao.user.get([tenant_uuid], user_uuid)
                except UnknownUserException as e:
                    logger.warning(e)
                    continue
                if self._dao.line.find(id_):
                    logger.warning(
                        'Line "%s" already created. Line multi-users not supported',
                        id_)
                    continue
                line = Line(id=id_)
                logger.debug('Create line "%s"', id_)
                self._dao.user.add_line(user, line)

        lines_expired = lines_cached - lines
        with session_scope():
            for id_, user_uuid, tenant_uuid in lines_expired:
                try:
                    user = self._dao.user.get([tenant_uuid], user_uuid)
                    line = self._dao.line.get(id_)
                except UnknownUserException:
                    logger.debug('Line "%s" already deleted', id_)
                    continue
                logger.debug('Delete line "%s"', id_)
                self._dao.user.remove_session(user, line)
Esempio n. 2
0
    def _add_and_remove_users(self, users):
        users = set((user['uuid'], user['tenant_uuid']) for user in users)
        users_cached = set((str(u.uuid), str(u.tenant_uuid))
                           for u in self._dao.user.list_(tenant_uuids=None))

        users_missing = users - users_cached
        with session_scope():
            for uuid, tenant_uuid in users_missing:
                # Avoid race condition between init tenant and init user
                tenant = self._dao.tenant.find_or_create(tenant_uuid)

                logger.debug('Create user "%s"', uuid)
                user = User(uuid=uuid, tenant=tenant, state='unavailable')
                self._dao.user.create(user)

        users_expired = users_cached - users
        with session_scope():
            for uuid, tenant_uuid in users_expired:
                try:
                    user = self._dao.user.get([tenant_uuid], uuid)
                except UnknownUserException as e:
                    logger.warning(e)
                    continue
                logger.debug('Delete user "%s"', uuid)
                self._dao.user.delete(user)
Esempio n. 3
0
    def initiate_channels(self, events):
        with session_scope():
            logger.debug('Delete all channels')
            self._dao.channel.delete_all()
            for event in events:
                if event.get('Event') != 'CoreShowChannel':
                    continue

                channel_name = event['Channel']
                endpoint_name = extract_endpoint_from_channel(channel_name)
                line = self._dao.line.find_by(endpoint_name=endpoint_name)
                if not line:
                    logger.debug(
                        'Unknown line with endpoint "%s" for channel "%s"',
                        endpoint_name,
                        channel_name,
                    )
                    continue

                state = CHANNEL_STATE_MAP.get(event['ChannelStateDesc'],
                                              'undefined')
                if event['ChanVariable'].get('XIVO_ON_HOLD') == '1':
                    state = 'holding'

                channel_args = {
                    'name': channel_name,
                    'state': state,
                }
                logger.debug(
                    'Create channel "%s" with state "%s"',
                    channel_args['name'],
                    channel_args['state'],
                )
                channel = Channel(**channel_args)
                self._dao.line.add_channel(line, channel)
Esempio n. 4
0
 def _user_deleted(self, event):
     user_uuid = event['uuid']
     tenant_uuid = event['tenant_uuid']
     with session_scope():
         user = self._dao.user.get([tenant_uuid], user_uuid)
         logger.debug('Delete user "%s"', user_uuid)
         self._dao.user.delete(user)
Esempio n. 5
0
 def _update_sessions(self, sessions):
     with session_scope():
         for session in sessions:
             cached_session = self._dao.session.find(session['uuid'])
             if cached_session and session[
                     'mobile'] != cached_session.mobile:
                 cached_session.mobile = session['mobile']
                 self._dao.session.update(cached_session)
Esempio n. 6
0
 def _update_refresh_tokens(self, tokens):
     with session_scope():
         for token in tokens:
             cached_token = self._dao.refresh_token.find(
                 token['user_uuid'], token['client_id'])
             if cached_token and token['mobile'] != cached_token.mobile:
                 cached_token.mobile = token['mobile']
                 self._dao.refresh_token.update(cached_token)
Esempio n. 7
0
 def _user_created(self, event):
     user_uuid = event['uuid']
     tenant_uuid = event['tenant_uuid']
     with session_scope():
         tenant = self._dao.tenant.find_or_create(tenant_uuid)
         logger.debug('Create user "%s"', user_uuid)
         user = User(uuid=user_uuid, tenant=tenant, state='unavailable')
         self._dao.user.create(user)
Esempio n. 8
0
 def _user_line_dissociated(self, event):
     line_id = event['line']['id']
     user_uuid = event['user']['uuid']
     tenant_uuid = event['user']['tenant_uuid']
     with session_scope():
         user = self._dao.user.get([tenant_uuid], user_uuid)
         line = self._dao.line.get(line_id)
         logger.debug('Delete line "%s"', line_id)
         self._dao.user.remove_line(user, line)
         self._notifier.updated(user)
Esempio n. 9
0
 def _user_dnd_updated(self, event):
     user_uuid = event['user_uuid']
     tenant_uuid = event['tenant_uuid']
     enabled = event['enabled']
     with session_scope():
         user = self._dao.user.get([tenant_uuid], user_uuid)
         logger.debug('Updating DND status of user "%s" to "%s"', user_uuid,
                      enabled)
         user.do_not_disturb = enabled
         self._dao.user.update(user)
         self._notifier.updated(user)
Esempio n. 10
0
    def _channel_deleted(self, event):
        channel_name = event['Channel']
        with session_scope():
            channel = self._dao.channel.find(channel_name)
            if not channel:
                logger.debug('Unknown channel "%s"', channel_name)
                return

            logger.debug('Delete channel "%s"', channel_name)
            self._dao.line.remove_channel(channel.line, channel)

            self._notifier.updated(channel.line.user)
Esempio n. 11
0
    def _add_and_remove_refresh_tokens(self, tokens):
        tokens = set(
            (token['client_id'], token['user_uuid'], token['tenant_uuid'])
            for token in tokens)
        tokens_cached = set(
            (token.client_id, str(token.user_uuid), str(token.tenant_uuid))
            for token in self._dao.refresh_token.list_())

        tokens_missing = tokens - tokens_cached
        with session_scope():
            for client_id, user_uuid, tenant_uuid in tokens_missing:
                try:
                    user = self._dao.user.get([tenant_uuid], user_uuid)
                except UnknownUserException:
                    logger.debug(
                        'Refresh token "%s" has no valid user "%s"',
                        client_id,
                        user_uuid,
                    )
                    continue

                logger.debug('Create refresh token "%s" for user "%s"',
                             client_id, user_uuid)
                token = RefreshToken(client_id=client_id, user_uuid=user_uuid)
                self._dao.user.add_refresh_token(user, token)

        tokens_expired = tokens_cached - tokens
        with session_scope():
            for client_id, user_uuid, tenant_uuid in tokens_expired:
                try:
                    user = self._dao.user.get([tenant_uuid], user_uuid)
                    token = self._dao.refresh_token.get(user_uuid, client_id)
                except (UnknownUserException,
                        UnknownRefreshTokenException) as e:
                    logger.warning(e)
                    continue

                logger.debug('Delete refresh token "%s" for user "%s"',
                             client_id, user_uuid)
                self._dao.user.remove_refresh_token(user, token)
Esempio n. 12
0
    def initiate_tenants(self, tenants):
        tenants = set(tenant['uuid'] for tenant in tenants)
        tenants_cached = set(
            str(tenant.uuid) for tenant in self._dao.tenant.list_())

        tenants_missing = tenants - tenants_cached
        with session_scope():
            for uuid in tenants_missing:
                logger.debug('Create tenant "%s"', uuid)
                tenant = Tenant(uuid=uuid)
                self._dao.tenant.create(tenant)

        tenants_expired = tenants_cached - tenants
        with session_scope():
            for uuid in tenants_expired:
                try:
                    tenant = self._dao.tenant.get(uuid)
                except UnknownTenantException as e:
                    logger.warning(e)
                    continue
                logger.debug('Delete tenant "%s"', uuid)
                self._dao.tenant.delete(tenant)
Esempio n. 13
0
    def _channel_hold(self, event):
        channel_name = event['Channel']
        with session_scope():
            channel = self._dao.channel.find(channel_name)
            if not channel:
                logger.debug('Unknown channel "%s"', channel_name)
                return

            logger.debug('Update channel "%s" with state "holding"',
                         channel_name)
            channel.state = 'holding'
            self._dao.channel.update(channel)

            self._notifier.updated(channel.line.user)
Esempio n. 14
0
    def _add_missing_endpoints(self, users):
        lines = set((line['id'], extract_endpoint_from_line(line))
                    for user in users for line in user['lines'])
        with session_scope():
            for line_id, endpoint_name in lines:
                if not endpoint_name:
                    logger.warning('Line "%s" doesn\'t have name', line_id)
                    continue

                endpoint = self._dao.endpoint.find_by(name=endpoint_name)
                if endpoint:
                    continue

                logger.debug('Create endpoint "%s"', endpoint_name)
                self._dao.endpoint.create(Endpoint(name=endpoint_name))
Esempio n. 15
0
    def _add_and_remove_sessions(self, sessions):
        sessions = set(
            (session['uuid'], session['user_uuid'], session['tenant_uuid'])
            for session in sessions)
        sessions_cached = set((str(session.uuid), str(session.user_uuid),
                               str(session.tenant_uuid))
                              for session in self._dao.session.list_())

        sessions_missing = sessions - sessions_cached
        with session_scope():
            for uuid, user_uuid, tenant_uuid in sessions_missing:
                try:
                    user = self._dao.user.get([tenant_uuid], user_uuid)
                except UnknownUserException:
                    logger.debug('Session "%s" has no valid user "%s"', uuid,
                                 user_uuid)
                    continue

                logger.debug('Create session "%s" for user "%s"', uuid,
                             user_uuid)
                session = Session(uuid=uuid, user_uuid=user_uuid)
                self._dao.user.add_session(user, session)

        sessions_expired = sessions_cached - sessions
        with session_scope():
            for uuid, user_uuid, tenant_uuid in sessions_expired:
                try:
                    user = self._dao.user.get([tenant_uuid], user_uuid)
                    session = self._dao.session.get(uuid)
                except (UnknownUserException, UnknownSessionException) as e:
                    logger.warning(e)
                    continue

                logger.debug('Delete session "%s" for user "%s"', uuid,
                             user_uuid)
                self._dao.user.remove_session(user, session)
Esempio n. 16
0
    def _channel_unhold(self, event):
        channel_name = event['Channel']
        state = CHANNEL_STATE_MAP.get(event['ChannelStateDesc'], 'undefined')
        with session_scope():
            channel = self._dao.channel.find(channel_name)
            if not channel:
                logger.debug('Unknown channel "%s"', channel_name)
                return

            logger.debug('Update channel "%s" with state "%s"', channel_name,
                         state)
            channel.state = state
            self._dao.channel.update(channel)

            self._notifier.updated(channel.line.user)
Esempio n. 17
0
    def _device_state_change(self, event):
        endpoint_name = event['Device']
        state = DEVICE_STATE_MAP.get(event['State'], 'unavailable')
        with session_scope():
            endpoint = self._dao.endpoint.find_or_create(endpoint_name)
            if endpoint.state == state:
                return

            endpoint.state = state
            logger.debug('Update endpoint "%s" with state "%s"', endpoint.name,
                         endpoint.state)
            self._dao.endpoint.update(endpoint)

            if endpoint.line:
                self._notifier.updated(endpoint.line.user)
Esempio n. 18
0
    def _refresh_token_deleted(self, event):
        tenant_uuid = event['tenant_uuid']
        user_uuid = event['user_uuid']
        client_id = event['client_id']
        with session_scope():
            try:
                user = self._dao.user.get([tenant_uuid], user_uuid)
            except UnknownUserException:
                logger.debug('Refresh token "%s" has no valid user "%s"',
                             client_id, user_uuid)
                return

            refresh_token = self._dao.refresh_token.get(user_uuid, client_id)
            logger.debug('Delete refresh token "%s" for user "%s"', client_id,
                         user_uuid)
            self._dao.user.remove_refresh_token(user, refresh_token)
            self._notifier.updated(user)
Esempio n. 19
0
    def _session_deleted(self, event):
        session_uuid = event['uuid']
        tenant_uuid = event['tenant_uuid']
        user_uuid = event['user_uuid']
        with session_scope():
            try:
                user = self._dao.user.get([tenant_uuid], user_uuid)
            except UnknownUserException:
                logger.debug('Session "%s" has no valid user "%s"',
                             session_uuid, user_uuid)
                return

            session = self._dao.session.get(session_uuid)
            logger.debug('Delete session "%s" for user "%s"', session_uuid,
                         user_uuid)
            self._dao.user.remove_session(user, session)
            self._notifier.updated(user)
Esempio n. 20
0
 def _update_services_users(self, users):
     with session_scope() as session:
         for confd_user in users:
             try:
                 user = self._dao.user.get([confd_user['tenant_uuid']],
                                           confd_user['uuid'])
             except UnknownUserException as e:
                 logger.warning(e)
                 continue
             do_not_disturb_status = confd_user['services']['dnd'][
                 'enabled']
             logger.debug(
                 'Updating user "%s" DND status to "%s"',
                 user.uuid,
                 do_not_disturb_status,
             )
             user.do_not_disturb = do_not_disturb_status
             session.flush()
Esempio n. 21
0
 def _associate_line_endpoint(self, users):
     lines = set((line['id'], extract_endpoint_from_line(line))
                 for user in users for line in user['lines'])
     with session_scope():
         for line_id, endpoint_name in lines:
             try:
                 line = self._dao.line.get(line_id)
                 endpoint = self._dao.endpoint.get_by(name=endpoint_name)
             except (UnknownLineException, UnknownEndpointException):
                 logger.debug(
                     'Unable to associate line "%s" with endpoint "%s"',
                     line_id,
                     endpoint_name,
                 )
                 continue
             logger.debug('Associate line "%s" with endpoint "%s"', line.id,
                          endpoint.name)
             self._dao.line.associate_endpoint(line, endpoint)
Esempio n. 22
0
    def initiate_endpoints(self, events):
        with session_scope():
            logger.debug('Delete all endpoints')
            self._dao.endpoint.delete_all()
            for event in events:
                if event.get('Event') != 'DeviceStateChange':
                    continue

                endpoint_args = {
                    'name': event['Device'],
                    'state': DEVICE_STATE_MAP.get(event['State'],
                                                  'unavailable'),
                }
                logger.debug(
                    'Create endpoint "%s" with state "%s"',
                    endpoint_args['name'],
                    endpoint_args['state'],
                )
                self._dao.endpoint.create(Endpoint(**endpoint_args))
Esempio n. 23
0
    def _channel_created(self, event):
        channel_name = event['Channel']
        state = CHANNEL_STATE_MAP.get(event['ChannelStateDesc'], 'undefined')
        endpoint_name = extract_endpoint_from_channel(channel_name)
        with session_scope():
            line = self._dao.line.find_by(endpoint_name=endpoint_name)
            if not line:
                logger.debug(
                    'Unknown line with endpoint "%s" for channel "%s"',
                    endpoint_name,
                    channel_name,
                )
                return

            channel = Channel(name=channel_name, state=state)
            logger.debug('Create channel "%s" for line "%s"', channel.name,
                         line.id)
            self._dao.line.add_channel(line, channel)

            self._notifier.updated(channel.line.user)
Esempio n. 24
0
    def _refresh_token_created(self, event):
        mobile = event['mobile']
        tenant_uuid = event['tenant_uuid']
        user_uuid = event['user_uuid']
        client_id = event['client_id']
        with session_scope():
            try:
                user = self._dao.user.get([tenant_uuid], user_uuid)
            except UnknownUserException:
                logger.debug('Refresh token "%s" has no valid user "%s"',
                             client_id, user_uuid)
                return

            logger.debug('Create refresh token "%s" for user "%s"', client_id,
                         user_uuid)
            refresh_token = RefreshToken(client_id=client_id,
                                         user_uuid=user_uuid,
                                         mobile=mobile)
            self._dao.user.add_refresh_token(user, refresh_token)
            self._notifier.updated(user)
Esempio n. 25
0
    def _session_created(self, event):
        mobile = event['mobile']
        session_uuid = event['uuid']
        tenant_uuid = event['tenant_uuid']
        user_uuid = event['user_uuid']
        with session_scope():
            try:
                user = self._dao.user.get([tenant_uuid], user_uuid)
            except UnknownUserException:
                logger.debug('Session "%s" has no valid user "%s"',
                             session_uuid, user_uuid)
                return

            logger.debug('Create session "%s" for user "%s"', session_uuid,
                         user_uuid)
            session = Session(uuid=session_uuid,
                              user_uuid=user_uuid,
                              mobile=mobile)
            self._dao.user.add_session(user, session)
            self._notifier.updated(user)
Esempio n. 26
0
    def _user_line_associated(self, event):
        line_id = event['line']['id']
        user_uuid = event['user']['uuid']
        tenant_uuid = event['user']['tenant_uuid']
        endpoint_name = extract_endpoint_from_line(event['line'])
        with session_scope():
            user = self._dao.user.get([tenant_uuid], user_uuid)
            line = self._dao.line.find(line_id)
            if not line:
                line = Line(id=line_id)
            logger.debug('Associate user "%s" with line "%s"', user_uuid,
                         line_id)
            self._dao.user.add_line(user, line)

            if not endpoint_name:
                logger.warning('Line "%s" doesn\'t have name', line_id)
                self._notifier.updated(user)
                return
            endpoint = self._dao.endpoint.find_or_create(endpoint_name)
            logger.debug('Associate line "%s" with endpoint "%s"', line_id,
                         endpoint_name)
            self._dao.line.associate_endpoint(line, endpoint)
            self._notifier.updated(user)
Esempio n. 27
0
 def _tenant_deleted(self, event):
     tenant_uuid = event['uuid']
     with session_scope():
         tenant = self._dao.tenant.get(tenant_uuid)
         logger.debug('Delete tenant "%s"', tenant_uuid)
         self._dao.tenant.delete(tenant)
Esempio n. 28
0
 def _tenant_created(self, event):
     tenant_uuid = event['uuid']
     with session_scope():
         logger.debug('Create tenant "%s"', tenant_uuid)
         tenant = Tenant(uuid=tenant_uuid)
         self._dao.tenant.create(tenant)