Skip to content
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: Issue #25040; Refactored sync_role_definition function in order to reduce number of query. #25340

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Changes from 2 commits
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
24 changes: 15 additions & 9 deletions superset/security/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from jwt.api_jwt import _jwt_global_obj
from sqlalchemy import and_, inspect, or_
from sqlalchemy.engine.base import Connection
from sqlalchemy.orm import Session
from sqlalchemy.orm import Session, eagerload
from sqlalchemy.orm.mapper import Mapper
from sqlalchemy.orm.query import Query as SqlaQuery

Expand Down Expand Up @@ -736,7 +736,9 @@ def create_missing_perms(self) -> None:

logger.info("Fetching a set of all perms to lookup which ones are missing")
all_pvs = set()
for pv in self.get_session.query(self.permissionview_model).all():
for pv in self.get_session.query(self.permissionview_model).options(
eagerload(self.permissionview_model.permission),
eagerload(self.permissionview_model.view_menu)).all():
if pv.permission and pv.view_menu:
all_pvs.add((pv.permission.name, pv.view_menu.name))

Expand Down Expand Up @@ -784,11 +786,17 @@ def sync_role_definitions(self) -> None:

self.create_custom_permissions()

#Fetch all pvms
pvms = self.get_session.query(PermissionView).options(
eagerload(PermissionView.permission),
eagerload(PermissionView.view_menu)).all()
pvms = [p for p in pvms if p.permission and p.view_menu]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this check do anything? These are required relations, aren't they?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is required relation but as this condition was written in previous query I thought of not removing it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, I see you haven't changed this. Fair enough.


# Creating default roles
self.set_role("Admin", self._is_admin_pvm)
self.set_role("Alpha", self._is_alpha_pvm)
self.set_role("Gamma", self._is_gamma_pvm)
self.set_role("sql_lab", self._is_sql_lab_pvm)
self.set_role("Admin", self._is_admin_pvm, pvms)
self.set_role("Alpha", self._is_alpha_pvm, pvms)
self.set_role("Gamma", self._is_gamma_pvm, pvms)
self.set_role("sql_lab", self._is_sql_lab_pvm, pvms)

# Configure public role
if current_app.config["PUBLIC_ROLE_LIKE"]:
Expand Down Expand Up @@ -864,7 +872,7 @@ def copy_role(
self.get_session.commit()

def set_role(
self, role_name: str, pvm_check: Callable[[PermissionView], bool]
self, role_name: str, pvm_check: Callable[[PermissionView], bool], pvms:[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type should be list[PermissionView] here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. Changes Done.

) -> None:
"""
Set the FAB permission/views for the role.
Expand All @@ -874,8 +882,6 @@ def set_role(
"""

logger.info("Syncing %s perms", role_name)
pvms = self.get_session.query(PermissionView).all()
pvms = [p for p in pvms if p.permission and p.view_menu]
role = self.add_role(role_name)
role_pvms = [
permission_view for permission_view in pvms if pvm_check(permission_view)
Expand Down