Beispiel #1
0
    def post(self):
        params = request.get_json(force=True)

        if settings.FEATURE_TABLES_PERMISSIONS:
            metadata = utils.SQLMetaData(params['query'])

            if metadata.has_non_select_dml_statements or metadata.has_ddl_statements:
                return {'job': {'error': 'Only SELECT statements are allowed'}}

            if len(metadata.used_tables - current_user.allowed_tables
                   ) > 0 and '*' not in current_user.allowed_tables:
                logging.warning('Permission denied for user %s to table %s',
                                self.current_user.name, metadata.used_tables)
                return {
                    'job': {
                        'error':
                        'Access denied for table(s): %s' %
                        (metadata.used_tables)
                    }
                }

        models.ActivityLog(user=self.current_user,
                           type=models.ActivityLog.QUERY_EXECUTION,
                           activity=params['query']).save()

        max_age = int(params.get('max_age', -1))

        if max_age == 0:
            query_result = None
        else:
            query_result = models.QueryResult.get_latest(
                params['data_source_id'], params['query'], max_age)

        if query_result:
            return {'query_result': query_result.to_dict()}
        else:
            data_source = models.DataSource.get_by_id(params['data_source_id'])
            query_id = params.get('query_id', 'adhoc')
            job = QueryTask.add_task(params['query'],
                                     data_source,
                                     metadata={
                                         "Username": self.current_user.name,
                                         "Query ID": query_id
                                     })
            return {'job': job.to_dict()}
Beispiel #2
0
    def post(self):
        params = request.json

        if settings.FEATURE_TABLES_PERMISSIONS:
            metadata = utils.SQLMetaData(params['query'])

            if metadata.has_non_select_dml_statements or metadata.has_ddl_statements:
                return {'job': {'error': 'Only SELECT statements are allowed'}}

            if len(metadata.used_tables - current_user.allowed_tables
                   ) > 0 and '*' not in current_user.allowed_tables:
                logging.warning('Permission denied for user %s to table %s',
                                self.current_user.name, metadata.used_tables)
                return {
                    'job': {
                        'error':
                        'Access denied for table(s): %s' %
                        (metadata.used_tables)
                    }
                }

        models.ActivityLog(user=self.current_user,
                           type=models.ActivityLog.QUERY_EXECUTION,
                           activity=params['query']).save()

        if params['ttl'] == 0:
            query_result = None
        else:
            query_result = models.QueryResult.get_latest(
                params['data_source_id'], params['query'], int(params['ttl']))

        if query_result:
            return {'query_result': query_result.to_dict()}
        else:
            data_source = models.DataSource.get_by_id(params['data_source_id'])
            job = data_manager.add_job(params['query'], data.Job.HIGH_PRIORITY,
                                       data_source)
            return {'job': job.to_dict()}