mirror of
https://github.com/apache/superset.git
synced 2026-04-20 08:34:37 +00:00
feat(UserInfo): Migrate User Info FAB to React (#33620)
This commit is contained in:
@@ -14,16 +14,23 @@
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
from flask import g, redirect, Response
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict
|
||||
|
||||
from flask import g, redirect, request, Response
|
||||
from flask_appbuilder.api import expose, safe
|
||||
from flask_appbuilder.security.sqla.models import User
|
||||
from flask_jwt_extended.exceptions import NoAuthorizationError
|
||||
from marshmallow import ValidationError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
from superset import app, is_feature_enabled
|
||||
from superset.daos.user import UserDAO
|
||||
from superset.extensions import db, event_logger
|
||||
from superset.utils.slack import get_user_avatar, SlackClientError
|
||||
from superset.views.base_api import BaseSupersetApi
|
||||
from superset.views.users.schemas import UserResponseSchema
|
||||
from superset.views.base_api import BaseSupersetApi, requires_json, statsd_metrics
|
||||
from superset.views.users.schemas import CurrentUserPutSchema, UserResponseSchema
|
||||
from superset.views.utils import bootstrap_user_data
|
||||
|
||||
user_response_schema = UserResponseSchema()
|
||||
@@ -34,7 +41,23 @@ class CurrentUserRestApi(BaseSupersetApi):
|
||||
|
||||
resource_name = "me"
|
||||
openapi_spec_tag = "Current User"
|
||||
openapi_spec_component_schemas = (UserResponseSchema,)
|
||||
openapi_spec_component_schemas = (UserResponseSchema, CurrentUserPutSchema)
|
||||
|
||||
current_user_put_schema = CurrentUserPutSchema()
|
||||
|
||||
def pre_update(self, item: User, data: Dict[str, Any]) -> None:
|
||||
item.changed_on = datetime.now()
|
||||
item.changed_by_fk = g.user.id
|
||||
if "password" in data and data["password"]:
|
||||
item.password = generate_password_hash(
|
||||
password=data["password"],
|
||||
method=self.appbuilder.get_app.config.get(
|
||||
"FAB_PASSWORD_HASH_METHOD", "scrypt"
|
||||
),
|
||||
salt_length=self.appbuilder.get_app.config.get(
|
||||
"FAB_PASSWORD_HASH_SALT_LENGTH", 16
|
||||
),
|
||||
)
|
||||
|
||||
@expose("/", methods=("GET",))
|
||||
@safe
|
||||
@@ -98,6 +121,61 @@ class CurrentUserRestApi(BaseSupersetApi):
|
||||
user = bootstrap_user_data(g.user, include_perms=True)
|
||||
return self.response(200, result=user)
|
||||
|
||||
@expose("/", methods=["PUT"])
|
||||
@safe
|
||||
@statsd_metrics
|
||||
@event_logger.log_this_with_context(
|
||||
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.put",
|
||||
log_to_statsd=False,
|
||||
)
|
||||
@requires_json
|
||||
def update_me(self) -> Response:
|
||||
"""Update current user information
|
||||
---
|
||||
put:
|
||||
summary: Update the current user
|
||||
description: >-
|
||||
Updates the current user's first name, last name, or password.
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/CurrentUserPutSchema'
|
||||
responses:
|
||||
200:
|
||||
description: User updated successfully
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
result:
|
||||
$ref: '#/components/schemas/UserResponseSchema'
|
||||
400:
|
||||
$ref: '#/components/responses/400'
|
||||
401:
|
||||
$ref: '#/components/responses/401'
|
||||
"""
|
||||
try:
|
||||
if g.user is None or g.user.is_anonymous:
|
||||
return self.response_401()
|
||||
except NoAuthorizationError:
|
||||
return self.response_401()
|
||||
try:
|
||||
item = self.current_user_put_schema.load(request.json)
|
||||
if not item:
|
||||
return self.response_400(message="At least one field must be provided.")
|
||||
|
||||
for key, value in item.items():
|
||||
setattr(g.user, key, value)
|
||||
|
||||
self.pre_update(g.user, item)
|
||||
db.session.commit()
|
||||
return self.response(200, result=user_response_schema.dump(g.user))
|
||||
except ValidationError as error:
|
||||
return self.response_400(message=error.messages)
|
||||
|
||||
|
||||
class UserRestApi(BaseSupersetApi):
|
||||
"""An API to get information about users"""
|
||||
|
||||
Reference in New Issue
Block a user