-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: On window focus, redirect to login if the user has been logged …
…out (#18773) * /me api * test it * watch for window activation and check auth * simplify * more comment * making ci happy * mypy should ignore tests
- Loading branch information
Showing
10 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ describe('canUserEditDashboard', () => { | |
email: '[email protected]', | ||
firstName: 'Test', | ||
isActive: true, | ||
isAnonymous: false, | ||
lastName: 'User', | ||
userId: 1, | ||
username: 'owner', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ export const user: UserWithPermissionsAndRoles = { | |
userId: 5, | ||
email: '[email protected]', | ||
isActive: true, | ||
isAnonymous: false, | ||
permissions: { | ||
datasource_access: ['table1', 'table2'], | ||
database_access: ['db1', 'db2', 'db3'], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# 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 flask import g, Response | ||
from flask_appbuilder.api import BaseApi, expose, safe | ||
|
||
from .schemas import UserResponseSchema | ||
|
||
user_response_schema = UserResponseSchema() | ||
|
||
|
||
class CurrentUserRestApi(BaseApi): | ||
""" An api to get information about the current user """ | ||
|
||
resource_name = "me" | ||
openapi_spec_tag = "Current User" | ||
openapi_spec_component_schemas = (UserResponseSchema,) | ||
|
||
@expose("/", methods=["GET"]) | ||
@safe | ||
def me(self) -> Response: | ||
"""Get the user object corresponding to the agent making the request | ||
--- | ||
get: | ||
description: >- | ||
Returns the user object corresponding to the agent making the request, | ||
or returns a 401 error if the user is unauthenticated. | ||
responses: | ||
200: | ||
description: The current user | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
result: | ||
$ref: '#/components/schemas/UserResponseSchema' | ||
401: | ||
$ref: '#/components/responses/401' | ||
""" | ||
if g.user is None or g.user.is_anonymous: | ||
return self.response_401() | ||
return self.response(200, result=user_response_schema.dump(g.user)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
from marshmallow import Schema | ||
from marshmallow.fields import Boolean, Integer, String | ||
|
||
|
||
class UserResponseSchema(Schema): | ||
id = Integer() | ||
username = String() | ||
email = String() | ||
first_name = String() | ||
last_name = String() | ||
is_active = Boolean() | ||
is_anonymous = Boolean() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# 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. | ||
# type: ignore | ||
"""Unit tests for Superset""" | ||
import json | ||
from unittest.mock import patch | ||
|
||
from superset import security_manager | ||
from tests.integration_tests.base_tests import SupersetTestCase | ||
|
||
meUri = "/api/v1/me/" | ||
|
||
|
||
class TestCurrentUserApi(SupersetTestCase): | ||
def test_get_me_logged_in(self): | ||
self.login(username="admin") | ||
|
||
rv = self.client.get(meUri) | ||
|
||
self.assertEqual(200, rv.status_code) | ||
response = json.loads(rv.data.decode("utf-8")) | ||
self.assertEqual("admin", response["result"]["username"]) | ||
self.assertEqual(True, response["result"]["is_active"]) | ||
self.assertEqual(False, response["result"]["is_anonymous"]) | ||
|
||
def test_get_me_unauthorized(self): | ||
self.logout() | ||
rv = self.client.get(meUri) | ||
self.assertEqual(401, rv.status_code) | ||
|
||
@patch("superset.security.manager.g") | ||
def test_get_me_anonymous(self, mock_g): | ||
mock_g.user = security_manager.get_anonymous_user | ||
rv = self.client.get(meUri) | ||
self.assertEqual(401, rv.status_code) |