Skip to main content

systemprompt_cli/commands/admin/users/
mod.rs

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