-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): user of a given key (#4442)
* feat(auth): user of a given key * remove user role not used * format * fix casing
- Loading branch information
1 parent
bcf273d
commit f8bbf25
Showing
11 changed files
with
125 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from collections import defaultdict | ||
from typing import DefaultDict, List, Optional | ||
|
||
from sqlalchemy import select | ||
from strawberry.dataloader import DataLoader | ||
from typing_extensions import TypeAlias | ||
|
||
from phoenix.db import models | ||
from phoenix.server.types import DbSessionFactory | ||
|
||
UserRoleId: TypeAlias = int | ||
Key: TypeAlias = UserRoleId | ||
Result: TypeAlias = Optional[models.UserRole] | ||
|
||
|
||
class UserRolesDataLoader(DataLoader[Key, Result]): | ||
"""DataLoader that batches together user roles by their ids.""" | ||
|
||
def __init__(self, db: DbSessionFactory) -> None: | ||
super().__init__(load_fn=self._load_fn) | ||
self._db = db | ||
|
||
async def _load_fn(self, keys: List[Key]) -> List[Result]: | ||
user_roles_by_id: DefaultDict[Key, Result] = defaultdict(None) | ||
async with self._db() as session: | ||
data = await session.stream_scalars(select(models.UserRole)) | ||
async for user_role in data: | ||
user_roles_by_id[user_role.id] = user_role | ||
|
||
return [user_roles_by_id.get(role_id) for role_id in keys] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from collections import defaultdict | ||
from typing import DefaultDict, List, Optional | ||
|
||
from sqlalchemy import select | ||
from strawberry.dataloader import DataLoader | ||
from typing_extensions import TypeAlias | ||
|
||
from phoenix.db import models | ||
from phoenix.server.types import DbSessionFactory | ||
|
||
UserId: TypeAlias = int | ||
Key: TypeAlias = UserId | ||
Result: TypeAlias = Optional[models.User] | ||
|
||
|
||
class UsersDataLoader(DataLoader[Key, Result]): | ||
"""DataLoader that batches together users by their ids.""" | ||
|
||
def __init__(self, db: DbSessionFactory) -> None: | ||
super().__init__(load_fn=self._load_fn) | ||
self._db = db | ||
|
||
async def _load_fn(self, keys: List[Key]) -> List[Result]: | ||
user_ids = list(set(keys)) | ||
users_by_id: DefaultDict[Key, Result] = defaultdict(None) | ||
async with self._db() as session: | ||
data = await session.stream_scalars( | ||
select(models.User).where(models.User.id.in_(user_ids)) | ||
) | ||
async for user in data: | ||
users_by_id[user.id] = user | ||
|
||
return [users_by_id.get(user_id) for user_id in keys] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
import strawberry | ||
from strawberry import Private | ||
from strawberry.relay.types import Node, NodeID | ||
from strawberry.types import Info | ||
|
||
from phoenix.server.api.context import Context | ||
from phoenix.server.api.exceptions import NotFound | ||
|
||
from .ApiKey import ApiKey | ||
from .User import User, to_gql_user | ||
|
||
|
||
@strawberry.type | ||
class UserApiKey(ApiKey, Node): | ||
id_attr: NodeID[int] | ||
user_id: Private[int] | ||
|
||
@strawberry.field | ||
async def user(self, info: Info[Context, None]) -> User: | ||
user = await info.context.data_loaders.users.load(self.user_id) | ||
if user is None: | ||
raise NotFound(f"User with id {self.user_id} not found") | ||
return to_gql_user(user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
import strawberry | ||
from strawberry.relay import Node, NodeID | ||
|
||
from phoenix.db import models | ||
|
||
|
||
@strawberry.type | ||
class UserRole(Node): | ||
id_attr: NodeID[int] | ||
name: str | ||
|
||
|
||
def to_gql_user_role(role: models.UserRole) -> UserRole: | ||
"""Convert an ORM user role to a GraphQL user role.""" | ||
return UserRole(id_attr=role.id, name=role.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters