-
Notifications
You must be signed in to change notification settings - Fork 14.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Cleanup user get_id/get_user_id #20492
refactor: Cleanup user get_id/get_user_id #20492
Conversation
@@ -805,7 +805,7 @@ def favorite_status(self, **kwargs: Any) -> Response: | |||
charts = ChartDAO.find_by_ids(requested_ids) | |||
if not charts: | |||
return self.response_404() | |||
favorited_chart_ids = ChartDAO.favorited_ids(charts, g.user.get_id()) | |||
favorited_chart_ids = ChartDAO.favorited_ids(charts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoiding passing around the global user. More on this in a later PR.
@@ -57,9 +58,9 @@ def apply(self, query: Query, value: Any) -> Query: | |||
return query.filter( | |||
or_( | |||
Dashboard.created_by_fk # pylint: disable=comparison-with-callable | |||
== g.user.get_user_id(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The get_user_id method is a classmethod and thus doesn't return the ID associated with the user
object but rather the g.user.id
. In this case they're the same but it's very misleading.
@@ -67,4 +67,4 @@ def get_uuid_namespace(seed: str) -> UUID: | |||
|
|||
|
|||
def get_owner(user: User) -> Optional[int]: | |||
return user.get_user_id() if not user.is_anonymous else None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above. This is wrong, user.get_user_id
does not return user.id
—for non-anonymous users—but rather g.user.id
. The reason this isn't an issue is that user
is always g.user
. I'm working on another PR which will replace get_owner
with get_user_id
.
@declared_attr | ||
def created_by_fk(cls): | ||
return Column( | ||
Integer, ForeignKey("ab_user.id"), default=cls.get_user_id, nullable=False | ||
Integer, ForeignKey("ab_user.id"), default=get_user_id, nullable=False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same logic but ensuring we use the same get_user_id
method.
@@ -145,7 +145,7 @@ def apply(self, query: Query, value: Any) -> Query: | |||
return query | |||
users_favorite_query = db.session.query(FavStar.obj_id).filter( | |||
and_( | |||
FavStar.user_id == g.user.get_id(), | |||
FavStar.user_id == get_user_id(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixed types, though likely benign from the database's perspective—at least for MySQL.
@@ -145,7 +146,7 @@ def post(self) -> FlaskResponse: # pylint: disable=no-self-use | |||
) | |||
( | |||
db.session.query(TabState) | |||
.filter_by(user_id=g.user.get_id()) | |||
.filter_by(user_id=get_user_id()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixed types, though likely benign from the database's perspective—at least for MySQL.
@@ -134,7 +135,7 @@ class TabStateView(BaseSupersetView): | |||
def post(self) -> FlaskResponse: # pylint: disable=no-self-use | |||
query_editor = json.loads(request.form["queryEditor"]) | |||
tab_state = TabState( | |||
user_id=g.user.get_id(), | |||
user_id=get_user_id(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what the ramifications of assigning user_id
to a str
was.
return Response(status=403) | ||
|
||
( | ||
db.session.query(TabState) | ||
.filter_by(user_id=g.user.get_id()) | ||
.filter_by(user_id=get_user_id()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixed types, though likely benign from the database's perspective—at least for MySQL.
@@ -242,7 +243,7 @@ def delete_query( # pylint: disable=no-self-use | |||
.filter( | |||
and_( | |||
Query.client_id != client_id, | |||
Query.user_id == g.user.get_id(), | |||
Query.user_id == get_user_id(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixed types, though likely benign from the database's perspective—at least for MySQL.
@@ -255,7 +256,7 @@ def delete_query( # pylint: disable=no-self-use | |||
|
|||
db.session.query(Query).filter_by( | |||
client_id=client_id, | |||
user_id=g.user.get_id(), | |||
user_id=get_user_id(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixed types, though likely benign from the database's perspective—at least for MySQL.
Codecov Report
@@ Coverage Diff @@
## master #20492 +/- ##
===========================================
- Coverage 66.75% 54.98% -11.77%
===========================================
Files 1740 1745 +5
Lines 65156 65394 +238
Branches 6900 6900
===========================================
- Hits 43492 35960 -7532
- Misses 19915 27685 +7770
Partials 1749 1749
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
@@ -70,15 +71,15 @@ def overwrite(slc: Slice, commit: bool = True) -> None: | |||
db.session.commit() | |||
|
|||
@staticmethod | |||
def favorited_ids(charts: List[Slice], current_user_id: int) -> List[FavStar]: | |||
def favorited_ids(charts: List[Slice]) -> List[FavStar]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will we ever need to get a list of favorited charts for other users?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ktmud no. Also this logic is going to refactored in another PR. In general Superset does not handle users other than the current logged in user. There have been recent mechanisms added to override the user globally if needed.
fbd0f62
to
409b7d6
Compare
968ccc3
to
5fbada6
Compare
5fbada6
to
02bf074
Compare
Co-authored-by: John Bodley <john.bodley@airbnb.com>
SUMMARY
This is the first PR in a slew of many to cleanup/standardize the user ID/username logic. Specifically it:
Optional[str]
with aget_user_id
convenience method which returnsOptional[int]
. I'm not sure how problematic this was given I doubt authors know of this nuance and thus many Python==
checks would have failed, i.e.,1 == "1"
evaluates toFalse
, however the database filters may have been more lenient, i.e., in MySQLSELECT 1 = '1'
evaluates to true.get_owner
which returned the ID of the current user and not the referenced user.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TESTING INSTRUCTIONS
CI.
ADDITIONAL INFORMATION