Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions superset/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def init_views(self) -> None:
Druid,
)
from superset.datasets.api import DatasetRestApi
from superset.queries.api import QueryRestApi
from superset.connectors.sqla.views import (
TableColumnInlineView,
SqlMetricInlineView,
Expand Down Expand Up @@ -184,6 +185,7 @@ def init_views(self) -> None:
appbuilder.add_api(DashboardRestApi)
appbuilder.add_api(DatabaseRestApi)
appbuilder.add_api(DatasetRestApi)
appbuilder.add_api(QueryRestApi)
#
# Setup regular views
#
Expand Down
16 changes: 16 additions & 0 deletions superset/queries/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
72 changes: 72 additions & 0 deletions superset/queries/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
import logging

from flask_appbuilder.models.sqla.interface import SQLAInterface

from superset.constants import RouteMethod
from superset.models.sql_lab import Query
from superset.queries.filters import QueryFilter
from superset.views.base_api import BaseSupersetModelRestApi

logger = logging.getLogger(__name__)


class QueryRestApi(BaseSupersetModelRestApi):
datamodel = SQLAInterface(Query)

resource_name = "query"
allow_browser_login = True
include_route_methods = {RouteMethod.GET, RouteMethod.GET_LIST}

class_permission_name = "QueryView"
list_columns = [
"user.username",
"database.database_name",
"status",
"start_time",
"end_time",
]
show_columns = [
"client_id",
"tmp_table_name",
"tmp_schema_name",
"status",
"tab_name",
"sql_editor_id",
"schema",
"sql",
"select_sql",
"executed_sql",
"limit",
"select_as_cta",
"select_as_cta_used",
"progress",
"rows",
"error_message",
"results_key",
"start_time",
"start_running_time",
"end_time",
"end_result_backend_time",
"tracking_url",
"changed_on",
]
base_filters = [["id", QueryFilter, lambda: []]]
base_order = ("changed_on", "desc")

openapi_spec_tag = "Queries"
28 changes: 28 additions & 0 deletions superset/queries/dao.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
import logging

from superset.dao.base import BaseDAO
from superset.models.sql_lab import Query
from superset.queries.filters import QueryFilter

logger = logging.getLogger(__name__)


class QueryDAO(BaseDAO):
model_cls = Query
base_filter = QueryFilter
37 changes: 37 additions & 0 deletions superset/queries/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
from typing import Callable

from flask import g
from flask_sqlalchemy import BaseQuery

from superset import security_manager
from superset.models.sql_lab import Query
from superset.views.base import BaseFilter


class QueryFilter(BaseFilter): # pylint: disable=too-few-public-methods
def apply(self, query: BaseQuery, value: Callable) -> BaseQuery:
"""
Filter queries to only those owned by current user. If
can_access_all_queries permission is set a user can list all queries

:returns: query
"""
if not security_manager.can_access_all_queries():
query = query.filter(Query.user_id == g.user.get_user_id())
return query
16 changes: 16 additions & 0 deletions tests/queries/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
Loading