-
Notifications
You must be signed in to change notification settings - Fork 136
feat: permission elevation foundation #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
Open
tomkis
wants to merge
8
commits into
main
Choose a base branch
from
feat/1734-permission-elevation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b1394ce
feat: permission elevation foundation
tomkis 40e7b96
docs: better docs
tomkis 9916c08
fix: code review comments
tomkis 352be38
feat: user listing
tomkis 2964ff2
feat: filter by email
tomkis 2e2a0bb
feat: CLI for permission elevation
tomkis 7dab197
docs: update CLI reference
tomkis 9472470
fix: code review comments
tomkis 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,96 @@ | ||
| # 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() | ||
|
|
||
|
|
||
| @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 = { | ||
| "admin": "[red]admin[/red]", | ||
| "developer": "[cyan]developer[/cyan]", | ||
| "user": "user", | ||
| }.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.Option("--role", "-r", help="Target role")], | ||
| 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)) | ||
tomkis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| role_display = { | ||
| "admin": "[red]admin[/red]", | ||
| "developer": "[cyan]developer[/cyan]", | ||
| "user": "user", | ||
| }.get(result.new_role, result.new_role) | ||
|
|
||
| console.success( | ||
| f"User role updated to [cyan]{role_display}[/cyan] (version [yellow]{result.role_version}[/yellow])" | ||
| ) | ||
|
|
||
|
|
||
| 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
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
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
| # 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, | ||
| role_version=updated_user.role_version, | ||
| ) | ||
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
| # 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 | ||
| role_version: int |
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
Oops, something went wrong.
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.