Skip to main content

systemprompt_cli/commands/admin/users/
mod.rs

1//! User administration command tree.
2//!
3//! [`UsersCommands`] groups the user CRUD, search, export, stats, merge, and
4//! the `bulk`, `role`, `session`, `ban`, and `webauthn` subcommand trees. On a
5//! `--database-url` invocation only the read-only commands are served; write
6//! operations require a full profile context.
7
8mod ban;
9mod bulk;
10mod count;
11mod create;
12pub(crate) mod delete;
13mod export;
14mod list;
15mod merge;
16mod role;
17mod search;
18mod session;
19mod show;
20mod stats;
21mod types;
22mod update;
23mod webauthn;
24
25use crate::context::CommandContext;
26use crate::shared::render_result;
27use anyhow::{Result, bail};
28use clap::Subcommand;
29
30pub use types::*;
31
32#[derive(Debug, Subcommand)]
33pub enum UsersCommands {
34    #[command(about = "List users with pagination and filtering")]
35    List(list::ListArgs),
36
37    #[command(about = "Show detailed user information")]
38    Show(show::ShowArgs),
39
40    #[command(about = "Search users by name, email, or full name")]
41    Search(search::SearchArgs),
42
43    #[command(about = "Create a new user")]
44    Create(create::CreateArgs),
45
46    #[command(about = "Update user fields")]
47    Update(update::UpdateArgs),
48
49    #[command(about = "Delete a user")]
50    Delete(delete::DeleteArgs),
51
52    #[command(about = "Get total user count")]
53    Count(count::CountArgs),
54
55    #[command(about = "Export users to JSON")]
56    Export(export::ExportArgs),
57
58    #[command(about = "Show user statistics dashboard")]
59    Stats,
60
61    #[command(about = "Merge source user into target user")]
62    Merge(merge::MergeArgs),
63
64    #[command(subcommand, about = "Bulk operations on users")]
65    Bulk(bulk::BulkCommands),
66
67    #[command(subcommand, about = "Role management commands")]
68    Role(role::RoleCommands),
69
70    #[command(subcommand, about = "Session management commands")]
71    Session(session::SessionCommands),
72
73    #[command(subcommand, about = "IP ban management commands")]
74    Ban(ban::BanCommands),
75
76    #[command(subcommand, about = "WebAuthn credential management commands")]
77    Webauthn(webauthn::WebauthnCommands),
78}
79
80pub async fn execute(cmd: UsersCommands, ctx: &CommandContext) -> Result<()> {
81    if ctx.is_database_scoped()
82        && matches!(
83            cmd,
84            UsersCommands::Create(_)
85                | UsersCommands::Update(_)
86                | UsersCommands::Delete(_)
87                | UsersCommands::Merge(_)
88                | UsersCommands::Bulk(_)
89                | UsersCommands::Webauthn(_)
90        )
91    {
92        bail!("Write operations require full profile context");
93    }
94
95    match cmd {
96        UsersCommands::List(args) => {
97            let result = list::execute(args, ctx).await?;
98            render_result(&result, &ctx.cli);
99            Ok(())
100        },
101        UsersCommands::Show(args) => {
102            let result = show::execute(args, ctx).await?;
103            render_result(&result, &ctx.cli);
104            Ok(())
105        },
106        UsersCommands::Search(args) => {
107            let result = search::execute(args, ctx).await?;
108            render_result(&result, &ctx.cli);
109            Ok(())
110        },
111        UsersCommands::Create(args) => {
112            let result = create::execute(args, ctx).await?;
113            render_result(&result, &ctx.cli);
114            Ok(())
115        },
116        UsersCommands::Update(args) => {
117            let result = update::execute(args, ctx).await?;
118            render_result(&result, &ctx.cli);
119            Ok(())
120        },
121        UsersCommands::Delete(args) => {
122            let result = delete::execute(args, ctx).await?;
123            render_result(&result, &ctx.cli);
124            Ok(())
125        },
126        UsersCommands::Count(args) => {
127            let result = count::execute(args, ctx).await?;
128            render_result(&result, &ctx.cli);
129            Ok(())
130        },
131        UsersCommands::Export(args) => {
132            let result = export::execute(args, ctx).await?;
133            render_result(&result, &ctx.cli);
134            Ok(())
135        },
136        UsersCommands::Stats => {
137            let result = stats::execute(ctx).await?;
138            render_result(&result, &ctx.cli);
139            Ok(())
140        },
141        UsersCommands::Merge(args) => {
142            let result = merge::execute(args, ctx).await?;
143            render_result(&result, &ctx.cli);
144            Ok(())
145        },
146        UsersCommands::Bulk(cmd) => bulk::execute(cmd, ctx).await,
147        UsersCommands::Role(cmd) => role::execute(cmd, ctx).await,
148        UsersCommands::Session(cmd) => session::execute(cmd, ctx).await,
149        UsersCommands::Ban(cmd) => ban::execute(cmd, ctx).await,
150        UsersCommands::Webauthn(cmd) => webauthn::execute(cmd, ctx).await,
151    }
152}