-
Notifications
You must be signed in to change notification settings - Fork 138
feat: permission elevation #1740
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
Merged
+425
−13
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
762ac69
feat: permission elevation foundation
tomkis 0f61620
docs: better docs
tomkis 13f0c82
fix: code review comments
tomkis d343733
feat: user listing
tomkis 97b49b4
feat: filter by email
tomkis 90aa5a6
feat: CLI for permission elevation
tomkis e4e7d08
docs: update CLI reference
tomkis 57a0b21
fix: code review comments
tomkis 91bf08b
fix: change set-user "role" to argument
JanPokorny 3970570
feat: use iat instead of role_version
JanPokorny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,92 @@ | ||
| # Copyright 2025 © BeeAI a Series of LF Projects, LLC | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import typing | ||
| from datetime import datetime | ||
|
|
||
| import typer | ||
| from agentstack_sdk.platform import User | ||
| from agentstack_sdk.platform.user import UserRole | ||
| from rich.table import Column | ||
|
|
||
| from agentstack_cli.async_typer import AsyncTyper, console, create_table | ||
| from agentstack_cli.configuration import Configuration | ||
| from agentstack_cli.utils import announce_server_action, confirm_server_action | ||
|
|
||
| app = AsyncTyper() | ||
| configuration = Configuration() | ||
|
|
||
| ROLE_DISPLAY = { | ||
| "admin": "[red]admin[/red]", | ||
| "developer": "[cyan]developer[/cyan]", | ||
| "user": "user", | ||
| } | ||
|
|
||
|
|
||
| @app.command("list") | ||
| async def list_users( | ||
| email: typing.Annotated[str | None, typer.Option(help="Filter by email (case-insensitive partial match)")] = None, | ||
| limit: typing.Annotated[int, typer.Option(help="Results per page (1-100)")] = 40, | ||
| after: typing.Annotated[str | None, typer.Option(help="Pagination cursor (page_token)")] = None, | ||
| ): | ||
| """List platform users (admin only).""" | ||
| announce_server_action("Listing users on") | ||
|
|
||
| async with configuration.use_platform_client(): | ||
| result = await User.list(email=email, limit=limit, page_token=after) | ||
|
|
||
| items = result.items | ||
| has_more = result.has_more | ||
| next_page_token = result.next_page_token | ||
|
|
||
| with create_table( | ||
| Column("ID", style="yellow"), | ||
| Column("Email"), | ||
| Column("Role"), | ||
| Column("Created"), | ||
| Column("Role Updated"), | ||
| no_wrap=True, | ||
| ) as table: | ||
| for user in items: | ||
| role_display = ROLE_DISPLAY.get(user.role, user.role) | ||
|
|
||
| created_at = _format_date(user.created_at) | ||
| role_updated_at = _format_date(user.role_updated_at) if user.role_updated_at else "-" | ||
|
|
||
| table.add_row( | ||
| user.id, | ||
| user.email, | ||
| role_display, | ||
| created_at, | ||
| role_updated_at, | ||
| ) | ||
|
|
||
| console.print() | ||
| console.print(table) | ||
|
|
||
| if has_more and next_page_token: | ||
| console.print(f"\n[dim]Use --after {next_page_token} to see more[/dim]") | ||
|
|
||
|
|
||
| @app.command("set-role") | ||
| async def set_role( | ||
| user_id: typing.Annotated[str, typer.Argument(help="User UUID")], | ||
| role: typing.Annotated[UserRole, typer.Argument(help="Target role (admin, developer, user)")], | ||
| yes: typing.Annotated[bool, typer.Option("--yes", "-y", help="Skip confirmation prompts.")] = False, | ||
| ): | ||
| """Change user role (admin only).""" | ||
| url = announce_server_action(f"Changing user {user_id} to role '{role}' on") | ||
| await confirm_server_action("Proceed with role change on", url=url, yes=yes) | ||
|
|
||
| async with configuration.use_platform_client(): | ||
| result = await User.set_role(user_id, UserRole(role)) | ||
|
|
||
| role_display = ROLE_DISPLAY.get(result.new_role, result.new_role) | ||
|
|
||
| console.success(f"User role updated to [cyan]{role_display}[/cyan]") | ||
|
|
||
|
|
||
| def _format_date(dt: datetime | None) -> str: | ||
| if not dt: | ||
| return "-" | ||
| return dt.strftime("%Y-%m-%d %H:%M") | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
61 changes: 61 additions & 0 deletions
61
apps/agentstack-server/src/agentstack_server/api/routes/users.py
This file contains hidden or 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,61 @@ | ||
| # Copyright 2025 © BeeAI a Series of LF Projects, LLC | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import logging | ||
| from typing import Annotated | ||
| from uuid import UUID | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException, Query, status | ||
|
|
||
| from agentstack_server.api.dependencies import UserServiceDependency, authorized_user | ||
| from agentstack_server.api.schema.user import ChangeRoleRequest, ChangeRoleResponse, UserListQuery, UserResponse | ||
| from agentstack_server.domain.models.common import PaginatedResult | ||
| from agentstack_server.domain.models.permissions import AuthorizedUser | ||
| from agentstack_server.domain.models.user import UserRole | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| router = APIRouter(tags=["users"]) | ||
|
|
||
|
|
||
| @router.get("", response_model=PaginatedResult[UserResponse]) | ||
| async def list_users( | ||
| query: Annotated[UserListQuery, Query()], | ||
tomkis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| user: Annotated[AuthorizedUser, Depends(authorized_user)], | ||
| user_service: UserServiceDependency, | ||
| ) -> PaginatedResult[UserResponse]: | ||
| if not user.user.role == UserRole.ADMIN: | ||
| raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Admin permission required") | ||
|
|
||
| result = await user_service.list_users( | ||
| limit=query.limit, | ||
| page_token=query.page_token, | ||
| email=query.email, | ||
| ) | ||
|
|
||
| return PaginatedResult( | ||
| items=[UserResponse(**u.model_dump()) for u in result.items], | ||
| total_count=result.total_count, | ||
| has_more=result.has_more, | ||
| ) | ||
|
|
||
|
|
||
| @router.put("/{user_id}/role", response_model=ChangeRoleResponse) | ||
| async def change_user_role( | ||
| user_id: UUID, | ||
| request: ChangeRoleRequest, | ||
| user: Annotated[AuthorizedUser, Depends(authorized_user)], | ||
| user_service: UserServiceDependency, | ||
| ) -> ChangeRoleResponse: | ||
| if not user.user.role == UserRole.ADMIN: | ||
| raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Admin permission required") | ||
|
|
||
| if user_id == user.user.id: | ||
| raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Cannot change own role") | ||
|
|
||
| updated_user = await user_service.change_role(user_id=user_id, new_role=request.new_role) | ||
tomkis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ChangeRoleResponse( | ||
| user_id=updated_user.id, | ||
| new_role=updated_user.role, | ||
| ) | ||
31 changes: 31 additions & 0 deletions
31
apps/agentstack-server/src/agentstack_server/api/schema/user.py
This file contains hidden or 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,31 @@ | ||
| # Copyright 2025 © BeeAI a Series of LF Projects, LLC | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from uuid import UUID | ||
|
|
||
| from pydantic import AwareDatetime, BaseModel, EmailStr, Field | ||
|
|
||
| from agentstack_server.domain.models.user import UserRole | ||
|
|
||
|
|
||
| class UserListQuery(BaseModel): | ||
| limit: int = Field(default=40, ge=1, le=100) | ||
| page_token: UUID | None = None | ||
| email: str | None = Field(default=None, description="Filter by email (case-insensitive partial match)") | ||
|
|
||
|
|
||
| class UserResponse(BaseModel): | ||
| id: UUID | ||
| email: EmailStr | ||
| role: UserRole | ||
| created_at: AwareDatetime | ||
| role_updated_at: AwareDatetime | None | ||
|
|
||
|
|
||
| class ChangeRoleRequest(BaseModel): | ||
| new_role: UserRole | ||
|
|
||
|
|
||
| class ChangeRoleResponse(BaseModel): | ||
| user_id: UUID | ||
| new_role: UserRole |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ | |
| from agentstack_server.api.routes.providers import router as provider_router | ||
| from agentstack_server.api.routes.user import router as user_router | ||
| from agentstack_server.api.routes.user_feedback import router as user_feedback_router | ||
| from agentstack_server.api.routes.users import router as users_router | ||
| from agentstack_server.api.routes.variables import router as variables_router | ||
| from agentstack_server.api.routes.vector_stores import router as vector_stores_router | ||
| from agentstack_server.api.utils import format_openai_error | ||
|
|
@@ -118,6 +119,7 @@ async def custom_http_exception_handler(request: Request, exc: Exception): | |
| def mount_routes(app: FastAPI): | ||
| server_router = APIRouter() | ||
| server_router.include_router(user_router, prefix="/user") | ||
| server_router.include_router(users_router, prefix="/users") | ||
|
Comment on lines
121
to
+122
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to deprecate
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll leave this for another PR since it involves deprecations |
||
| server_router.include_router(a2a_router, prefix="/a2a") | ||
| server_router.include_router(mcp_router, prefix="/mcp") | ||
| server_router.include_router(provider_router, prefix="/providers", tags=["providers"]) | ||
|
|
||
This file contains hidden or 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
12 changes: 9 additions & 3 deletions
12
apps/agentstack-server/src/agentstack_server/domain/repositories/user.py
This file contains hidden or 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,18 +1,24 @@ | ||
| # Copyright 2025 © BeeAI a Series of LF Projects, LLC | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from collections.abc import AsyncIterator | ||
| from typing import Protocol | ||
| from uuid import UUID | ||
|
|
||
| from agentstack_server.domain.models.common import PaginatedResult | ||
| from agentstack_server.domain.models.user import User | ||
|
|
||
|
|
||
| class IUserRepository(Protocol): | ||
| async def list(self) -> AsyncIterator[User]: | ||
| yield ... | ||
| async def list( | ||
| self, | ||
| *, | ||
| limit: int, | ||
| page_token: UUID | None = None, | ||
| email: str | None = None, | ||
| ) -> PaginatedResult[User]: ... | ||
|
|
||
| async def create(self, *, user: User) -> None: ... | ||
| async def get(self, *, user_id: UUID) -> User: ... | ||
| async def get_by_email(self, *, email: str) -> User: ... | ||
| async def delete(self, *, user_id: UUID) -> int: ... | ||
| async def update(self, *, user: User) -> None: ... |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.