user.py•5.05 kB
"""
User management commands for the MCP Connection Hub CLI.
This module provides commands for managing users and API keys.
"""
import typer
from typing import Optional
from rich.console import Console
from rich.table import Table
import uuid
# Create Typer app for user commands
app = typer.Typer(name="user", help="Manage users and API keys")
# Create Console for rich output
console = Console()
@app.command("list")
def list_users():
"""
List all users.
"""
# Placeholder implementation (will be implemented properly in future milestones)
table = Table(title="Users")
table.add_column("ID", style="cyan")
table.add_column("Name", style="green")
table.add_column("API Keys", style="yellow")
table.add_column("Role", style="blue")
# Sample user data
table.add_row("1", "admin", "1", "Administrator")
table.add_row("2", "api-user", "3", "API User")
console.print(table)
@app.command("add")
def add_user(
name: str = typer.Argument(..., help="Username"),
role: str = typer.Option("user", "--role", "-r", help="User role (admin, user)"),
create_key: bool = typer.Option(True, "--create-key/--no-create-key", help="Create API key for user")
):
"""
Add a new user.
"""
# Placeholder implementation (will be implemented properly in future milestones)
console.print(f"[green]Adding user {name} with role {role}[/green]")
if create_key:
# Generate a random API key
api_key = str(uuid.uuid4())
console.print(f"[green]Created API key: {api_key}[/green]")
console.print("[yellow]Important: Save this key as it will not be shown again![/yellow]")
@app.command("remove")
def remove_user(
name: str = typer.Argument(..., help="Username to remove"),
force: bool = typer.Option(False, "--force", "-f", help="Force removal without confirmation")
):
"""
Remove a user.
"""
# Placeholder implementation (will be implemented properly in future milestones)
if not force:
confirm = typer.confirm(f"Are you sure you want to remove user {name}?")
if not confirm:
console.print("[yellow]User removal cancelled[/yellow]")
return
console.print(f"[green]User {name} has been removed[/green]")
@app.command("update")
def update_user(
name: str = typer.Argument(..., help="Username to update"),
role: Optional[str] = typer.Option(None, "--role", "-r", help="New role (admin, user)")
):
"""
Update a user's properties.
"""
# Placeholder implementation (will be implemented properly in future milestones)
if role:
console.print(f"[green]Updated role for user {name}: {role}[/green]")
else:
console.print("[yellow]No changes specified[/yellow]")
@app.command("key-list")
def list_keys(
user: Optional[str] = typer.Argument(None, help="Username to list keys for (all if omitted)")
):
"""
List API keys.
"""
# Placeholder implementation (will be implemented properly in future milestones)
table = Table(title=f"API Keys{f' for {user}' if user else ''}")
table.add_column("ID", style="cyan")
table.add_column("Name", style="green")
table.add_column("Created", style="yellow")
table.add_column("Last Used", style="blue")
table.add_column("Status", style="magenta")
# Sample API key data
if not user or user == "admin":
table.add_row("1", "Admin Key", "2023-01-01", "2023-06-01", "Active")
if not user or user == "api-user":
table.add_row("2", "API User Key 1", "2023-02-15", "2023-05-30", "Active")
table.add_row("3", "API User Key 2", "2023-03-10", "2023-05-15", "Active")
table.add_row("4", "API User Key 3", "2023-04-20", "Never", "Inactive")
console.print(table)
@app.command("key-create")
def create_key(
name: str = typer.Argument(..., help="Name for the API key"),
user: str = typer.Option("admin", "--user", "-u", help="User to create key for")
):
"""
Create a new API key.
"""
# Placeholder implementation (will be implemented properly in future milestones)
# Generate a random API key
api_key = str(uuid.uuid4())
console.print(f"[green]Created API key '{name}' for user {user}:[/green]")
console.print(f"[bold]{api_key}[/bold]")
console.print("[yellow]Important: Save this key as it will not be shown again![/yellow]")
@app.command("key-revoke")
def revoke_key(
key_id: str = typer.Argument(..., help="ID of the API key to revoke"),
force: bool = typer.Option(False, "--force", "-f", help="Force revocation without confirmation")
):
"""
Revoke an API key.
"""
# Placeholder implementation (will be implemented properly in future milestones)
if not force:
confirm = typer.confirm(f"Are you sure you want to revoke API key {key_id}?")
if not confirm:
console.print("[yellow]Key revocation cancelled[/yellow]")
return
console.print(f"[green]API key {key_id} has been revoked[/green]")