Skip to content

Commit

Permalink
Fix SA2.0 usage in managers.roles
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavcs committed Oct 3, 2023
1 parent 490e6e3 commit 5b0521e
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/galaxy/managers/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import logging
from typing import List

from sqlalchemy import false
from sqlalchemy import (
false,
select,
)
from sqlalchemy.orm import exc as sqlalchemy_exceptions

import galaxy.exceptions
Expand Down Expand Up @@ -44,7 +47,8 @@ def get(self, trans: ProvidesUserContext, role_id: int) -> model.Role:
:raises: InconsistentDatabase, RequestParameterInvalidException, InternalServerError
"""
try:
role = self.session().query(self.model_class).filter(self.model_class.id == role_id).one()
stmt = select(self.model_class).where(self.model_class.id == role_id)
role = self.session().execute(stmt).scalar_one()
except sqlalchemy_exceptions.MultipleResultsFound:
raise galaxy.exceptions.InconsistentDatabase("Multiple roles found with the same id.")
except sqlalchemy_exceptions.NoResultFound:
Expand All @@ -59,7 +63,8 @@ def get(self, trans: ProvidesUserContext, role_id: int) -> model.Role:

def list_displayable_roles(self, trans: ProvidesUserContext) -> List[Role]:
roles = []
for role in trans.sa_session.query(Role).filter(Role.deleted == false()):
stmt = select(Role).where(Role.deleted == false())
for role in trans.sa_session.scalars(stmt):
if trans.user_is_admin or trans.app.security_agent.ok_to_display(trans.user, role):
roles.append(role)
return roles
Expand All @@ -70,15 +75,16 @@ def create_role(self, trans: ProvidesUserContext, role_definition_model: RoleDef
user_ids = role_definition_model.user_ids or []
group_ids = role_definition_model.group_ids or []

if trans.sa_session.query(Role).filter(Role.name == name).first():
stmt = select(Role).where(Role.name == name).limit(1)
if trans.sa_session.scalars(stmt).first():
raise RequestParameterInvalidException(f"A role with that name already exists [{name}]")

role_type = Role.types.ADMIN # TODO: allow non-admins to create roles

role = Role(name=name, description=description, type=role_type)
trans.sa_session.add(role)
users = [trans.sa_session.query(model.User).get(i) for i in user_ids]
groups = [trans.sa_session.query(model.Group).get(i) for i in group_ids]
users = [trans.sa_session.get(model.User, i) for i in user_ids]
groups = [trans.sa_session.get(model.Group, i) for i in group_ids]

# Create the UserRoleAssociations
for user in users:
Expand Down

0 comments on commit 5b0521e

Please sign in to comment.