ctb_storage/
cli.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! CLI execution helpers for storage operations.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use crate as ctb_storage;
30
31pub fn adduser(username: &str, password_stdin: bool) -> Result<ToolResult> {
32    use std::io::IsTerminal;
33    use std::io::Write;
34
35    let password_str = if !password_stdin && std::io::stdin().is_terminal() {
36        print!("Enter password for '{username}': ");
37        std::io::stdout().flush()?;
38        let mut p1 = String::new();
39        std::io::stdin().read_line(&mut p1)?;
40        let p1 = p1.trim_end_matches(['\r', '\n']).to_string();
41
42        print!("Confirm password: ");
43        std::io::stdout().flush()?;
44        let mut p2 = String::new();
45        std::io::stdin().read_line(&mut p2)?;
46        let p2 = p2.trim_end_matches(['\r', '\n']).to_string();
47
48        if p1 != p2 {
49            bail!("Passwords do not match");
50        }
51        p1
52    } else {
53        let mut p = String::new();
54        std::io::stdin().read_line(&mut p)?;
55        p.trim_end_matches(['\r', '\n']).to_string()
56    };
57
58    if password_str.is_empty() {
59        bail!("Password cannot be empty");
60    }
61
62    let password =
63        ctb_utilities::password::Password::from_string(&password_str);
64    let user = ctb_storage::user::add_non_admin_user(username, &password)?;
65
66    println!(
67        "User '{name}' registered successfully with ID {id}.",
68        name = user.name(),
69        id = user.local_id()
70    );
71    Ok(ToolResult::immediate_ok(Vec::new()))
72}
73
74#[cfg(test)]
75#[allow(
76    clippy::panic,
77    clippy::expect_used,
78    clippy::unwrap_used,
79    clippy::unwrap_in_result,
80    clippy::panic_in_result_fn,
81    clippy::indexing_slicing,
82    clippy::arithmetic_side_effects,
83    reason = "Standard repository test boilerplate"
84)]
85mod tests {
86    use super::*;
87
88    #[crate::ctb_test]
89    fn test_adduser_command() -> Result<()> {
90        let username = format!("cli_user_{}", function_name!());
91        ctb_storage::user::User::delete_by_name(&username).ok();
92
93        // Ensure allow_local_account_creation is true for this test
94        let mut settings =
95            ctb_utilities::pc_settings::PcSettings::load().unwrap_or_default();
96        settings.allow_local_account_creation =
97            ctb_utilities::json::maybe_value::MaybeValue::Value(true);
98        settings.save()?;
99
100        // Note: Password comes from stdin or add_non_admin_user directly.
101        // Let's test calling add_non_admin_user
102        let password = ctb_utilities::password::Password::from_string(
103            ctb_utilities::password::TEST_USER_PASS,
104        );
105        let user = ctb_storage::user::add_non_admin_user(&username, &password)?;
106        assert_eq!(user.name(), username);
107        assert!(!user.is_admin());
108
109        user.delete()?;
110        Ok(())
111    }
112}