Skip to content

Commit

Permalink
Merge pull request #16746 from jdavcs/dev_fix_shared_code
Browse files Browse the repository at this point in the history
Account for shared usage between TS and galaxy apps
  • Loading branch information
jmchilton authored Sep 26, 2023
2 parents f31ffd8 + 77d10c1 commit b180b79
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lib/galaxy/managers/api_keys.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
from typing import Optional

from sqlalchemy import (
false,
select,
update,
)

from galaxy.model import (
APIKeys,
User,
)
from galaxy.model.base import transaction
from galaxy.structured_app import BasicSharedApp

Expand All @@ -19,11 +13,12 @@ def __init__(self, app: BasicSharedApp):
self.app = app
self.session = self.app.model.context

def get_api_key(self, user: User) -> Optional["APIKeys"]:
def get_api_key(self, user):
APIKeys = self.app.model.APIKeys
stmt = select(APIKeys).filter_by(user_id=user.id, deleted=False).order_by(APIKeys.create_time.desc()).limit(1)
return self.session.scalars(stmt).first()

def create_api_key(self, user: User) -> "APIKeys":
def create_api_key(self, user):
guid = self.app.security.get_new_guid()
new_key = self.app.model.APIKeys()
new_key.user_id = user.id
Expand All @@ -33,15 +28,15 @@ def create_api_key(self, user: User) -> "APIKeys":
self.session.commit()
return new_key

def get_or_create_api_key(self, user: User) -> str:
def get_or_create_api_key(self, user) -> str:
# Logic Galaxy has always used - but it would appear to have a race
# condition. Worth fixing? Would kind of need a message queue to fix
# in multiple process mode.
api_key = self.get_api_key(user)
key = api_key.key if api_key else self.create_api_key(user).key
return key

def delete_api_key(self, user: User) -> None:
def delete_api_key(self, user) -> None:
"""Marks the current user API key as deleted."""
# Before it was possible to create multiple API keys for the same user although they were not considered valid
# So all non-deleted keys are marked as deleted for backward compatibility
Expand All @@ -50,6 +45,7 @@ def delete_api_key(self, user: User) -> None:
self.session.commit()

def _mark_all_api_keys_as_deleted(self, user_id: int):
APIKeys = self.app.model.APIKeys
stmt = (
update(APIKeys)
.where(APIKeys.user_id == user_id)
Expand Down

0 comments on commit b180b79

Please sign in to comment.