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

Fix bug in User.all_roles query + use SA2.0 #16761

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Changes from all 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
38 changes: 17 additions & 21 deletions lib/galaxy/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,33 +836,29 @@ def all_roles(self):
"""
Return a unique list of Roles associated with this user or any of their groups.
"""
try:
db_session = object_session(self)
user = (
db_session.query(User)
.filter_by(id=self.id) # don't use get, it will use session variant.
.options(
joinedload(User.roles),
joinedload(User.roles.role),
joinedload(User.groups),
joinedload(User.groups.group),
joinedload(User.groups.group.roles),
joinedload(User.groups.group.roles.role),
)
.one()
)
except Exception:
# If not persistent user, just use models normaly and
# skip optimizations...
user = self
if self.id: # if user persistent, retrieve from database
roles = self._get_all_user_roles_from_db()
else:
# else: use models normally
roles = [ura.role for ura in self.roles]

roles = [ura.role for ura in user.roles]
for group in [uga.group for uga in user.groups]:
for group in [uga.group for uga in self.groups]:
for role in [gra.role for gra in group.roles]:
if role not in roles:
roles.append(role)
return roles

def _get_all_user_roles_from_db(self):
user_roles = select(Role).join(UserRoleAssociation).join(User).where(UserRoleAssociation.user_id == self.id)
user_group_roles = (
select(Role)
.join(GroupRoleAssociation)
.join(UserGroupAssociation, UserGroupAssociation.group_id == GroupRoleAssociation.group_id)
.where(UserGroupAssociation.user_id == self.id)
)
roles = select(Role).from_statement(user_roles.union(user_group_roles))
return object_session(self).scalars(roles)

Copy link
Member

Choose a reason for hiding this comment

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

Does the join implicitly load the models now?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not implicitly. It's the from_statement method: "...to use this SELECT statement as a source of complete [Foo] entities instead, we can link these columns to a regular ORM-enabled Select construct using the Select.from_statement() method" (ref)

def all_roles_exploiting_cache(self):
""" """
roles = [ura.role for ura in self.roles]
Expand Down
Loading