use crate::{
models::{AccountInfo, CreateAccount},
types::QueryParams,
CpanelClient, CpanelError,
};
use serde::Serialize;
#[derive(Debug, Clone, Default, Serialize)]
pub struct ModifyAccount {
pub username: String,
pub plan: Option<String>,
pub ip: Option<String>,
pub suspend: Option<bool>,
}
impl CpanelClient {
pub async fn list_accounts(&self) -> Result<Vec<AccountInfo>, CpanelError> {
let result: Vec<AccountInfo> = self.whm("listaccts", &[]).await?;
Ok(result)
}
pub async fn list_accounts_paginated(
&self,
params: &QueryParams,
) -> Result<Vec<AccountInfo>, CpanelError> {
let pairs: Vec<(&str, String)> = params.to_pairs();
let pairs: Vec<(&str, &str)> = pairs.iter().map(|(k, v)| (*k, v.as_str())).collect();
let result: Vec<AccountInfo> = self.whm("listaccts", &pairs).await?;
Ok(result)
}
pub async fn get_account_info(&self, username: &str) -> Result<AccountInfo, CpanelError> {
let result: AccountInfo = self.whm("getacctinfo", &[("user", username)]).await?;
Ok(result)
}
pub async fn create_account(&self, create: &CreateAccount) -> Result<(), CpanelError> {
let username_s = create.username.clone();
let password_s = create.password.clone();
let domain_s = create.domain.clone();
let plan_s = create.plan.clone();
let ip_s = create.ip.clone();
let contact_s = create.contact_email.clone();
let max_ftp_s = create.max_ftp.map(|v| v.to_string());
let max_pop_s = create.max_pop.map(|v| v.to_string());
let max_sql_s = create.max_sql.map(|v| v.to_string());
let mut params: Vec<(&str, &str)> = vec![
("username", &username_s),
("password", &password_s),
("domain", &domain_s),
];
if let Some(ref s) = plan_s {
params.push(("plan", s));
}
if let Some(ref s) = ip_s {
params.push(("ip", s));
}
if let Some(ref s) = contact_s {
params.push(("contact_email", s));
}
if let Some(ref s) = max_ftp_s {
params.push(("max_ftp", s));
}
if let Some(ref s) = max_pop_s {
params.push(("max_pop", s));
}
if let Some(ref s) = max_sql_s {
params.push(("max_sql", s));
}
let _: () = self.whm("addacct", ¶ms).await?;
Ok(())
}
pub async fn delete_account(&self, username: &str) -> Result<(), CpanelError> {
let _: () = self.whm("delacct", &[("user", username)]).await?;
Ok(())
}
pub async fn suspend_account(&self, username: &str) -> Result<(), CpanelError> {
let _: () = self.whm("suspendacct", &[("user", username)]).await?;
Ok(())
}
pub async fn unsuspend_account(&self, username: &str) -> Result<(), CpanelError> {
let _: () = self.whm("unsuspendacct", &[("user", username)]).await?;
Ok(())
}
pub async fn modify_account(&self, modify: &ModifyAccount) -> Result<(), CpanelError> {
let user_s = modify.username.clone();
let plan_s = modify.plan.clone();
let ip_s = modify.ip.clone();
let suspend_s = modify.suspend.map(|v| v.to_string());
let mut params: Vec<(&str, &str)> = vec![("user", &user_s)];
if let Some(ref s) = plan_s {
params.push(("plan", s));
}
if let Some(ref s) = ip_s {
params.push(("ip", s));
}
if let Some(ref s) = suspend_s {
params.push(("suspend", s));
}
let _: () = self.whm("modifyacct", ¶ms).await?;
Ok(())
}
pub async fn change_password(&self, username: &str, password: &str) -> Result<(), CpanelError> {
let _: () = self
.whm("chpass", &[("user", username), ("password", password)])
.await?;
Ok(())
}
}