Exemplo n.º 1
0
    def get_user_names(self, kwargs=None):
        monitor_db = database.MonitorDatabase()

        user_cond = ''
        if session.get_session_user_id() and int(
                session.get_session_access_level()) < 3:
            user_cond = 'AND user_id = %s ' % session.get_session_user_id()

        try:
            query = 'SELECT user_id, ' \
                    '(CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" \
                    THEN users.username ELSE users.friendly_name END) AS friendly_name ' \
                    'FROM users ' \
                    'WHERE deleted_user = 0 %s' % user_cond

            result = monitor_db.select(query)
        except Exception as e:
            logger.warn(
                u"Tautulli Users :: Unable to execute database query for get_user_names: %s."
                % e)
            return None

        return session.friendly_name_to_username(result)
Exemplo n.º 2
0
    def __init__(self, username=None, password=None, token=None, headers=None):
        self.username = username
        self.password = password
        self.token = token
        self.headers = headers

        self.urls = 'https://plex.tv'
        self.timeout = plexpy.CONFIG.PMS_TIMEOUT
        self.ssl_verify = plexpy.CONFIG.VERIFY_SSL_CERT

        if self.username is None and self.password is None:
            if not self.token:
                # Check if we should use the admin token, or the guest server token
                if session.get_session_user_id():
                    user_data = users.Users()
                    user_tokens = user_data.get_tokens(
                        user_id=session.get_session_user_id())
                    self.token = user_tokens['server_token']
                else:
                    self.token = plexpy.CONFIG.PMS_TOKEN

            if not self.token:
                logger.error(
                    u"Tautulli PlexTV :: PlexTV called, but no token provided."
                )
                return

        self.http_handler = http_handler.HTTPHandler(
            urls=self.urls,
            token=self.token,
            timeout=self.timeout,
            ssl_verify=self.ssl_verify,
            headers=self.headers)

        plexpass = self.get_plexpass_status()
        plexpy.CONFIG.PMS_PLEXPASS = plexpass
        plexpy.CONFIG.write()
Exemplo n.º 3
0
        def get_user_details(user_id=user_id,
                             user=user,
                             email=email,
                             token=token):
            monitor_db = database.MonitorDatabase()

            query = 'SELECT id, user_id, username, friendly_name, thumb AS user_thumb, custom_avatar_url AS custom_thumb, ' \
                    'email, is_admin, is_home_user, is_allow_sync, is_restricted, do_notify, keep_history, deleted_user, ' \
                    'allow_guest, user_token, is_plextv, plexpass ' \
                    'FROM users '

            try:
                if str(user_id).isdigit():
                    query = query + ' WHERE user_id = ? '
                    result = monitor_db.select(query, args=[user_id])
                elif user:
                    query = query + ' WHERE username = ? COLLATE NOCASE '
                    result = monitor_db.select(query, args=[user])
                elif email:
                    query = query + ' WHERE email = ? COLLATE NOCASE '
                    result = monitor_db.select(query, args=[email])
                elif token:
                    query = query + ' WHERE user_token = ? '
                    result = monitor_db.select(query, args=[token])
                else:
                    result = []

            except Exception as e:
                logger.warn(
                    u"Tautulli Users :: Unable to execute database query for get_details: %s."
                    % e)
                result = []

            user_details = {}
            if result:
                for item in result:
                    if session.get_session_user_id() and int(
                            session.get_session_access_level()) < 3:
                        friendly_name = session.get_session_user()
                    elif item['friendly_name']:
                        friendly_name = item['friendly_name']
                    else:
                        friendly_name = item['username']

                    if item['custom_thumb'] and item['custom_thumb'] != item[
                            'user_thumb']:
                        user_thumb = item['custom_thumb']
                    elif item['user_thumb']:
                        user_thumb = item['user_thumb']
                    else:
                        user_thumb = common.DEFAULT_USER_THUMB

                    query = 'SELECT server_id, shared_libraries ' \
                            'FROM user_shared_libraries ' \
                            'WHERE id = ? '
                    result_shared_libraries = monitor_db.select(
                        query, args=[item['id']])
                    shared_libraries = []
                    shared_servers = []
                    for item_shared_libraries in result_shared_libraries:
                        sl = tuple(
                            item_shared_libraries['shared_libraries'].split(
                                ';')
                        ) if item_shared_libraries['shared_libraries'] else ()
                        shared_libraries.extend(sl)
                        shared_servers.append(
                            item_shared_libraries['server_id'])

                    user_details = {
                        'user_id':
                        item['user_id'],
                        'username':
                        item['username'],
                        'friendly_name':
                        friendly_name,
                        'user_thumb':
                        user_thumb,
                        'email':
                        item['email'],
                        'is_plextv':
                        item['is_plextv'],
                        'is_admin':
                        item['is_admin'],
                        'is_home_user':
                        item['is_home_user'],
                        'is_allow_sync':
                        item['is_allow_sync'],
                        'is_restricted':
                        item['is_restricted'],
                        'plexpass':
                        item['plexpass'],
                        'user_token':
                        item['user_token'],
                        'do_notify':
                        item['do_notify'],
                        'keep_history':
                        item['keep_history'],
                        'deleted_user':
                        item['deleted_user'],
                        'allow_guest':
                        item['allow_guest'],
                        'access_level':
                        (9 if item['is_admin'] else item['allow_guest']),
                        'shared_libraries':
                        shared_libraries,
                        'shared_servers':
                        shared_servers,
                    }
            return user_details
Exemplo n.º 4
0
    def get_datatables_list(self, kwargs=None):
        default_return = {
            'recordsFiltered': 0,
            'recordsTotal': 0,
            'draw': 0,
            'data': 'null',
            'error': 'Unable to execute database query.'
        }

        data_tables = datatables.DataTables()

        custom_where = [['users.deleted_user', 0]]

        if session.get_session_user_id() and int(
                session.get_session_access_level()) < 3:
            custom_where.append(
                ['users.user_id',
                 session.get_session_user_id()])

        if kwargs.get('user_id'):
            custom_where.append(['users.user_id', kwargs.get('user_id')])

        columns = [
            'users.user_id',
            '(CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" \
                    THEN users.username ELSE users.friendly_name END) AS friendly_name',
            'users.thumb AS user_thumb',
            'users.custom_avatar_url AS custom_thumb',
            'COUNT(session_history.id) AS plays',
            'SUM(CASE WHEN session_history.stopped > 0 THEN (session_history.stopped - session_history.started) \
                    ELSE 0 END) - SUM(CASE WHEN session_history.paused_counter IS NULL THEN 0 ELSE \
                    session_history.paused_counter END) AS duration',
            'MAX(session_history.started) AS last_seen',
            'MAX(session_history.id) AS id',
            'session_history_metadata.full_title AS last_played',
            'servers.pms_name AS last_server', 'session_history.ip_address',
            'session_history.platform', 'session_history.player',
            'session_history.rating_key',
            'session_history_metadata.media_type',
            'session_history_metadata.thumb',
            'session_history_metadata.parent_thumb',
            'session_history_metadata.grandparent_thumb',
            'session_history_metadata.parent_title',
            'session_history_metadata.year',
            'session_history_metadata.media_index',
            'session_history_metadata.parent_media_index',
            'session_history_media_info.transcode_decision',
            'users.do_notify as do_notify',
            'users.keep_history as keep_history',
            'users.allow_guest as allow_guest'
        ]
        try:
            query = data_tables.ssp_query(
                table_name='users',
                columns=columns,
                custom_where=custom_where,
                group_by=['users.user_id'],
                join_types=[
                    'LEFT OUTER JOIN', 'LEFT OUTER JOIN', 'LEFT OUTER JOIN',
                    'LEFT OUTER JOIN'
                ],
                join_tables=[
                    'session_history', 'session_history_metadata',
                    'session_history_media_info', 'servers'
                ],
                join_evals=[
                    ['session_history.user_id', 'users.user_id'],
                    ['session_history.id', 'session_history_metadata.id'],
                    ['session_history.id', 'session_history_media_info.id'],
                    ['session_history.server_id', 'servers.id']
                ],
                kwargs=kwargs)
        except Exception as e:
            logger.warn(
                u"Tautulli Users :: Unable to execute database query for get_list: %s."
                % e)
            return default_return

        users = query['result']

        rows = []
        for item in users:
            if item['media_type'] == 'episode' and item['parent_thumb']:
                thumb = item['parent_thumb']
            elif item['media_type'] == 'episode':
                thumb = item['grandparent_thumb']
            else:
                thumb = item['thumb']

            if item['custom_thumb'] and item['custom_thumb'] != item[
                    'user_thumb']:
                user_thumb = item['custom_thumb']
            elif item['user_thumb']:
                user_thumb = item['user_thumb']
            else:
                user_thumb = common.DEFAULT_USER_THUMB

            # Rename Mystery platform names
            platform = common.PLATFORM_NAME_OVERRIDES.get(
                item['platform'], item['platform'])

            row = {
                'user_id': item['user_id'],
                'friendly_name': item['friendly_name'],
                'user_thumb': user_thumb,
                'plays': item['plays'],
                'duration': item['duration'],
                'last_seen': item['last_seen'],
                'last_played': item['last_played'],
                'last_server': item['last_server'],
                'id': item['id'],
                'ip_address': item['ip_address'],
                'platform': platform,
                'player': item['player'],
                'rating_key': item['rating_key'],
                'media_type': item['media_type'],
                'thumb': thumb,
                'parent_title': item['parent_title'],
                'year': item['year'],
                'media_index': item['media_index'],
                'parent_media_index': item['parent_media_index'],
                'transcode_decision': item['transcode_decision'],
                'do_notify': helpers.checked(item['do_notify']),
                'keep_history': helpers.checked(item['keep_history']),
                'allow_guest': item['allow_guest']
            }

            rows.append(row)

        dict = {
            'recordsFiltered': query['filteredCount'],
            'recordsTotal': query['totalCount'],
            'data': session.friendly_name_to_username(rows),
            'draw': query['draw']
        }

        return dict
Exemplo n.º 5
0
    def get_datatables_user_login(self, user_id=None, kwargs=None):
        default_return = {
            'recordsFiltered': 0,
            'recordsTotal': 0,
            'draw': 0,
            'data': 'null',
            'error': 'Unable to execute database query.'
        }

        if not session.allow_session_user(user_id):
            return default_return

        data_tables = datatables.DataTables()

        if session.get_session_user_id() and int(
                session.get_session_access_level()) < 5:
            custom_where = [[
                'user_login.user_id',
                session.get_session_user_id()
            ]]
        else:
            custom_where = [['user_login.user_id', user_id]] if user_id else []

        columns = [
            'user_login.timestamp', 'user_login.user_id', 'user_login.user',
            'user_login.user_group', 'user_login.ip_address',
            'user_login.host', 'user_login.user_agent', 'user_login.success',
            '(CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" \
                    THEN users.username ELSE users.friendly_name END) AS friendly_name'
        ]

        try:
            query = data_tables.ssp_query(
                table_name='user_login',
                columns=columns,
                custom_where=custom_where,
                group_by=[],
                join_types=['LEFT OUTER JOIN'],
                join_tables=['users'],
                join_evals=[['user_login.user_id', 'users.user_id']],
                kwargs=kwargs)
        except Exception as e:
            logger.warn(
                u"Tautulli Users :: Unable to execute database query for get_datatables_user_login: %s."
                % e)
            return default_return

        results = query['result']

        rows = []
        for item in results:
            (os, browser) = httpagentparser.simple_detect(item['user_agent'])

            row = {
                'timestamp': item['timestamp'],
                'user_id': item['user_id'],
                'user_group': item['user_group'],
                'ip_address': item['ip_address'],
                'host': item['host'],
                'user_agent': item['user_agent'],
                'os': os,
                'browser': browser,
                'success': item['success'],
                'friendly_name': item['friendly_name'] or item['user']
            }

            rows.append(row)

        dict = {
            'recordsFiltered': query['filteredCount'],
            'recordsTotal': query['totalCount'],
            'data': session.friendly_name_to_username(rows),
            'draw': query['draw']
        }

        return dict