make delete_user take a struct use derive_builder
This commit is contained in:
parent
31fbfbe628
commit
f3a18641e0
114
Cargo.lock
generated
114
Cargo.lock
generated
@ -5,6 +5,7 @@ name = "adduser"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"derive_builder",
|
||||
"env_logger",
|
||||
"lazy_static",
|
||||
"log",
|
||||
@ -57,6 +58,66 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"darling_macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling_core"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b"
|
||||
dependencies = [
|
||||
"fnv",
|
||||
"ident_case",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"strsim",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling_macro"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive_builder"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2658621297f2cf68762a6f7dc0bb7e1ff2cfd6583daef8ee0fed6f7ec468ec0"
|
||||
dependencies = [
|
||||
"darling",
|
||||
"derive_builder_core",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive_builder_core"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2791ea3e372c8495c0bc2033991d76b512cd799d07491fbd6890124db9458bef"
|
||||
dependencies = [
|
||||
"darling",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.8.1"
|
||||
@ -70,6 +131,12 @@ dependencies = [
|
||||
"termcolor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fnv"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.17"
|
||||
@ -85,6 +152,12 @@ version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a"
|
||||
|
||||
[[package]]
|
||||
name = "ident_case"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
@ -131,6 +204,24 @@ dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.3.9"
|
||||
@ -160,6 +251,23 @@ dependencies = [
|
||||
"termcolor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.48"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.1.0"
|
||||
@ -189,6 +297,12 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.0+wasi-snapshot-preview1"
|
||||
|
@ -12,4 +12,5 @@ lazy_static = "1.4"
|
||||
log = "0.4"
|
||||
simplelog = "0.8"
|
||||
chrono = "0.4"
|
||||
env_logger = "0.8"
|
||||
env_logger = "0.8"
|
||||
derive_builder = "0.9"
|
@ -1,3 +1,6 @@
|
||||
pub mod newuser_args;
|
||||
|
||||
pub use newuser_args::{DeleteHome, DeletePrimaryGroup, NewUserArgs};
|
||||
pub trait UserDBRead {
|
||||
fn get_all_users(&self) -> Vec<&crate::User>;
|
||||
fn get_user_by_name(&self, name: &str) -> Option<&crate::User>;
|
||||
@ -15,7 +18,10 @@ pub trait UserDBValidation {
|
||||
}
|
||||
|
||||
pub trait UserDBWrite {
|
||||
fn delete_user(&mut self, user: &str) -> Result<crate::User, crate::UserLibError>;
|
||||
fn delete_user(
|
||||
&mut self,
|
||||
params: newuser_args::NewUserArgs,
|
||||
) -> Result<crate::User, crate::UserLibError>;
|
||||
fn new_user(
|
||||
&mut self, /*
|
||||
username: String,
|
38
src/api/newuser_args.rs
Normal file
38
src/api/newuser_args.rs
Normal file
@ -0,0 +1,38 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum DeleteHome {
|
||||
Delete,
|
||||
Keep,
|
||||
Archive { path: PathBuf },
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum DeletePrimaryGroup {
|
||||
Delete,
|
||||
Keep,
|
||||
DeleteIfEmpty,
|
||||
}
|
||||
|
||||
#[derive(Debug, Builder)]
|
||||
#[builder(public)]
|
||||
pub struct NewUserArgs<'a> {
|
||||
pub username: &'a str,
|
||||
pub delete_home: DeleteHome,
|
||||
pub delete_primary_group: DeletePrimaryGroup,
|
||||
}
|
||||
|
||||
impl<'a> NewUserArgs<'a> {
|
||||
pub fn builder() -> NewUserArgsBuilder<'a> {
|
||||
NewUserArgsBuilder::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for NewUserArgs<'_> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
username: "defaultuser",
|
||||
delete_home: DeleteHome::Keep,
|
||||
delete_primary_group: DeletePrimaryGroup::DeleteIfEmpty,
|
||||
}
|
||||
}
|
||||
}
|
@ -20,7 +20,12 @@ fn main() {
|
||||
|
||||
let mut db = adduser::UserDBLocal::load_files(mf).unwrap();
|
||||
|
||||
let user_res: Result<adduser::User, adduser::UserLibError> = db.delete_user("teste");
|
||||
let user_res: Result<adduser::User, adduser::UserLibError> = db.delete_user(
|
||||
adduser::api::NewUserArgs::builder()
|
||||
.username("teste")
|
||||
.build()
|
||||
.unwrap(),
|
||||
);
|
||||
match user_res {
|
||||
Ok(u) => info!(
|
||||
"The user <{}> has been deleted! ",
|
||||
|
@ -1,5 +1,7 @@
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate derive_builder;
|
||||
|
||||
extern crate log;
|
||||
|
||||
|
@ -144,9 +144,9 @@ impl UserDBLocal {
|
||||
|
||||
use crate::api::UserDBWrite;
|
||||
impl UserDBWrite for UserDBLocal {
|
||||
fn delete_user(&mut self, username: &str) -> Result<crate::User, UserLibError> {
|
||||
fn delete_user(&mut self, args: crate::api::NewUserArgs) -> Result<crate::User, UserLibError> {
|
||||
// try to get the user from the database
|
||||
let user_opt = self.get_user_by_name(username);
|
||||
let user_opt = self.get_user_by_name(args.username);
|
||||
let user = match user_opt {
|
||||
Some(user) => user,
|
||||
None => {
|
||||
@ -156,7 +156,7 @@ impl UserDBWrite for UserDBLocal {
|
||||
|
||||
if self.source_files.is_virtual() {
|
||||
warn!("There are no associated files working in dummy mode!");
|
||||
let res = self.users.remove(username);
|
||||
let res = self.users.remove(args.username);
|
||||
match res {
|
||||
Some(u) => Ok(u),
|
||||
None => Err(UserLibError::NotFound), // should not happen anymore as existence is checked.
|
||||
@ -175,7 +175,7 @@ impl UserDBWrite for UserDBLocal {
|
||||
| src.shadow.has_changed(&shadow_file_content)
|
||||
{
|
||||
error!("The source files have changed. Deleting the user could corrupt the userdatabase. Aborting!");
|
||||
Err(format!("The userdatabase has been changed {}", username).into())
|
||||
Err(format!("The userdatabase has been changed {}", args.username).into())
|
||||
} else {
|
||||
UserDBLocal::delete_from_passwd(user, passwd_file_content, &mut locked_p)?;
|
||||
UserDBLocal::delete_from_shadow(user, shadow_file_content, &mut locked_s)?;
|
||||
@ -207,7 +207,7 @@ impl UserDBWrite for UserDBLocal {
|
||||
),
|
||||
}
|
||||
// Remove the user from the memory database(HashMap)
|
||||
let res = self.users.remove(username);
|
||||
let res = self.users.remove(args.username);
|
||||
match res {
|
||||
Some(u) => Ok(u),
|
||||
None => Err("Failed to remove the user from the internal HashMap".into()),
|
||||
@ -463,11 +463,16 @@ fn test_user_db_read_implementation() {
|
||||
|
||||
#[test]
|
||||
fn test_user_db_write_implementation() {
|
||||
use crate::api::NewUserArgs;
|
||||
let mut data = UserDBLocal::import_from_strings("test:x:1001:1001:full Name,004,000342,001-2312,myemail@test.com:/home/test:/bin/test", "test:!!$6$/RotIe4VZzzAun4W$7YUONvru1rDnllN5TvrnOMsWUD5wSDUPAD6t6/Xwsr/0QOuWF3HcfAhypRkGa8G1B9qqWV5kZSnCb8GKMN9N61:18260:0:99999:7:::", "teste:x:1002:test,test");
|
||||
let user = "test";
|
||||
|
||||
assert_eq!(data.get_all_users().len(), 1);
|
||||
assert!(data.delete_user(&user).is_ok());
|
||||
assert!(data.delete_user(&user).is_err());
|
||||
assert!(data
|
||||
.delete_user(NewUserArgs::builder().username(&user).build().unwrap())
|
||||
.is_ok());
|
||||
assert!(data
|
||||
.delete_user(NewUserArgs::builder().username(&user).build().unwrap())
|
||||
.is_err());
|
||||
assert_eq!(data.get_all_users().len(), 0);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user