delete the home directory
This commit is contained in:
parent
f3a18641e0
commit
4062f7a6bb
@ -1,20 +1,21 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub enum DeleteHome {
|
pub enum DeleteHome {
|
||||||
Delete,
|
Delete,
|
||||||
Keep,
|
Keep,
|
||||||
Archive { path: PathBuf },
|
Archive { path: PathBuf },
|
||||||
}
|
}
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
pub enum DeletePrimaryGroup {
|
pub enum DeletePrimaryGroup {
|
||||||
Delete,
|
Delete,
|
||||||
Keep,
|
Keep,
|
||||||
DeleteIfEmpty,
|
DeleteIfEmpty,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Builder)]
|
#[derive(Debug, Builder, Eq, PartialEq)]
|
||||||
#[builder(public)]
|
#[builder(public)]
|
||||||
|
#[builder(default)]
|
||||||
pub struct NewUserArgs<'a> {
|
pub struct NewUserArgs<'a> {
|
||||||
pub username: &'a str,
|
pub username: &'a str,
|
||||||
pub delete_home: DeleteHome,
|
pub delete_home: DeleteHome,
|
||||||
|
@ -23,6 +23,7 @@ fn main() {
|
|||||||
let user_res: Result<adduser::User, adduser::UserLibError> = db.delete_user(
|
let user_res: Result<adduser::User, adduser::UserLibError> = db.delete_user(
|
||||||
adduser::api::NewUserArgs::builder()
|
adduser::api::NewUserArgs::builder()
|
||||||
.username("teste")
|
.username("teste")
|
||||||
|
// .delete_home(adduser::api::DeleteHome::Delete)
|
||||||
.build()
|
.build()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
@ -85,6 +85,12 @@ impl From<String> for UserLibError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<std::io::Error> for UserLibError {
|
||||||
|
fn from(e: std::io::Error) -> Self {
|
||||||
|
UserLibError::Message(MyMessage::Simple(e.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<(String, std::io::Error)> for UserLibError {
|
impl From<(String, std::io::Error)> for UserLibError {
|
||||||
fn from((m, e): (String, std::io::Error)) -> Self {
|
fn from((m, e): (String, std::io::Error)) -> Self {
|
||||||
UserLibError::Message(MyMessage::IOError(m, e))
|
UserLibError::Message(MyMessage::IOError(m, e))
|
||||||
|
@ -132,6 +132,19 @@ impl UserDBLocal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn delete_home(user: &crate::User) -> std::io::Result<()> {
|
||||||
|
match user.get_home_dir() {
|
||||||
|
Some(dir) => std::fs::remove_dir_all(dir),
|
||||||
|
None => {
|
||||||
|
error!("Failed to remove the home directory! As the user did not have one.");
|
||||||
|
Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::InvalidInput,
|
||||||
|
"Failed to remove the home directory! As the user did not have one.",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn get_group_pos_by_id(&self, id: u32) -> Option<(&crate::Group, usize)> {
|
fn get_group_pos_by_id(&self, id: u32) -> Option<(&crate::Group, usize)> {
|
||||||
for (i, group) in self.groups.iter().enumerate() {
|
for (i, group) in self.groups.iter().enumerate() {
|
||||||
if group.get_gid()? == id {
|
if group.get_gid()? == id {
|
||||||
@ -142,9 +155,9 @@ impl UserDBLocal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::api::UserDBWrite;
|
use crate::api::{DeleteHome, DeletePrimaryGroup, NewUserArgs, UserDBRead, UserDBWrite};
|
||||||
impl UserDBWrite for UserDBLocal {
|
impl UserDBWrite for UserDBLocal {
|
||||||
fn delete_user(&mut self, args: crate::api::NewUserArgs) -> Result<crate::User, UserLibError> {
|
fn delete_user(&mut self, args: NewUserArgs) -> Result<crate::User, UserLibError> {
|
||||||
// try to get the user from the database
|
// try to get the user from the database
|
||||||
let user_opt = self.get_user_by_name(args.username);
|
let user_opt = self.get_user_by_name(args.username);
|
||||||
let user = match user_opt {
|
let user = match user_opt {
|
||||||
@ -179,6 +192,9 @@ impl UserDBWrite for UserDBLocal {
|
|||||||
} else {
|
} else {
|
||||||
UserDBLocal::delete_from_passwd(user, passwd_file_content, &mut locked_p)?;
|
UserDBLocal::delete_from_passwd(user, passwd_file_content, &mut locked_p)?;
|
||||||
UserDBLocal::delete_from_shadow(user, shadow_file_content, &mut locked_s)?;
|
UserDBLocal::delete_from_shadow(user, shadow_file_content, &mut locked_s)?;
|
||||||
|
if args.delete_home == DeleteHome::Delete {
|
||||||
|
UserDBLocal::delete_home(user)?;
|
||||||
|
}
|
||||||
let group = self.get_group_pos_by_id(user.get_gid());
|
let group = self.get_group_pos_by_id(user.get_gid());
|
||||||
match group {
|
match group {
|
||||||
Some((group, id)) => {
|
Some((group, id)) => {
|
||||||
@ -268,7 +284,6 @@ impl UserDBWrite for UserDBLocal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::api::UserDBRead;
|
|
||||||
impl UserDBRead for UserDBLocal {
|
impl UserDBRead for UserDBLocal {
|
||||||
fn get_all_users(&self) -> Vec<&crate::User> {
|
fn get_all_users(&self) -> Vec<&crate::User> {
|
||||||
let mut res: Vec<&crate::User> = self.users.iter().map(|(_, x)| x).collect();
|
let mut res: Vec<&crate::User> = self.users.iter().map(|(_, x)| x).collect();
|
||||||
|
Loading…
Reference in New Issue
Block a user