-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
from superset.extensions import db | ||
from superset.models.core import FavStar, FavStarClassName | ||
from superset.models.slice import Slice | ||
from superset.utils.core import get_user_id | ||
|
||
if TYPE_CHECKING: | ||
from superset.connectors.base.models import BaseDatasource | ||
|
@@ -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 commentThe 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 commentThe 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. |
||
ids = [chart.id for chart in charts] | ||
return [ | ||
star.obj_id | ||
for star in db.session.query(FavStar.obj_id) | ||
.filter( | ||
FavStar.class_name == FavStarClassName.CHART, | ||
FavStar.obj_id.in_(ids), | ||
FavStar.user_id == current_user_id, | ||
FavStar.user_id == get_user_id(), | ||
) | ||
.all() | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
from superset.models.embedded_dashboard import EmbeddedDashboard | ||
from superset.models.slice import Slice | ||
from superset.security.guest_token import GuestTokenResourceType, GuestUser | ||
from superset.utils.core import get_user_id | ||
from superset.views.base import BaseFilter, is_user_admin | ||
from superset.views.base_api import BaseFavoriteFilter | ||
|
||
|
@@ -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 commentThe 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 |
||
== get_user_id(), | ||
Dashboard.changed_by_fk # pylint: disable=comparison-with-callable | ||
== g.user.get_user_id(), | ||
== get_user_id(), | ||
) | ||
) | ||
|
||
|
@@ -126,17 +127,14 @@ def apply(self, query: Query, value: Any) -> Query: | |
|
||
users_favorite_dash_query = db.session.query(FavStar.obj_id).filter( | ||
and_( | ||
FavStar.user_id == security_manager.user_model.get_user_id(), | ||
FavStar.user_id == get_user_id(), | ||
FavStar.class_name == "Dashboard", | ||
) | ||
) | ||
owner_ids_query = ( | ||
db.session.query(Dashboard.id) | ||
.join(Dashboard.owners) | ||
.filter( | ||
security_manager.user_model.id | ||
== security_manager.user_model.get_user_id() | ||
) | ||
.filter(security_manager.user_model.id == get_user_id()) | ||
) | ||
|
||
feature_flagged_filters = [] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. See above. This is wrong, |
||
return user.id if not user.is_anonymous else None |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
from sqlalchemy.orm import relationship | ||
|
||
from superset import db | ||
from superset.utils.core import get_user_id | ||
|
||
Base = declarative_base() | ||
|
||
|
@@ -63,17 +64,10 @@ class User(Base): | |
|
||
|
||
class AuditMixin: | ||
@classmethod | ||
def get_user_id(cls): | ||
try: | ||
return g.user.id | ||
except Exception: | ||
return None | ||
|
||
@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 commentThe reason will be displayed to describe this comment to others. Learn more. Same logic but ensuring we use the same |
||
) | ||
|
||
@declared_attr | ||
|
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.