diff --git a/src/bin/new_users.rs b/src/bin/new_users.rs index 6b89501..06105ed 100644 --- a/src/bin/new_users.rs +++ b/src/bin/new_users.rs @@ -4,7 +4,7 @@ use lettre::{ Message, SmtpTransport, Transport, }; use maud::html; -use skynet_ldap_backend::{db_init, get_config, get_now_iso, random_string, read_csv, Accounts, AccountsNew, Config, Record}; +use skynet_ldap_backend::{db_init, get_config, get_now_iso, get_wolves, random_string, AccountWolves, Accounts, AccountsNew, Config}; use sqlx::{Pool, Sqlite}; #[async_std::main] @@ -12,31 +12,29 @@ async fn main() { let config = get_config(); let db = db_init(&config).await.unwrap(); - if let Ok(records) = read_csv(&config) { - for record in records { - // skynet emails not permitted - if record.email.trim().ends_with("@skynet.ie") { - continue; - } + for record in get_wolves(&db).await { + // skynet emails not permitted + if record.email.trim().ends_with("@skynet.ie") { + continue; + } - // check if the email is already in the db - if !check(&db, &record.email).await { - continue; - } + // check if the email is already in the db + if !check(&db, &record.email).await { + continue; + } - // generate a auth key - let auth = random_string(50); + // generate a auth key + let auth = random_string(50); - match send_mail(&config, &record, &auth) { - Ok(_) => match save_to_db(&db, &record, &auth).await { - Ok(_) => {} - Err(e) => { - println!("Unable to save to db {} {e:?}", &record.email); - } - }, + match send_mail(&config, &record, &auth) { + Ok(_) => match save_to_db(&db, &record, &auth).await { + Ok(_) => {} Err(e) => { - println!("Unable to send mail to {} {e:?}", &record.email); + println!("Unable to save to db {} {e:?}", &record.email); } + }, + Err(e) => { + println!("Unable to send mail to {} {e:?}", &record.email); } } } @@ -75,7 +73,7 @@ async fn check_pending(db: &Pool, mail: &str) -> bool { } // using https://github.com/lettre/lettre/blob/57886c367d69b4d66300b322c94bd910b1eca364/examples/maud_html.rs -fn send_mail(config: &Config, record: &Record, auth: &str) -> Result { +fn send_mail(config: &Config, record: &AccountWolves, auth: &str) -> Result { let recipient = &record.name_first; let mail = &record.email; let url_base = "https://sso.skynet.ie"; @@ -180,7 +178,7 @@ fn send_mail(config: &Config, record: &Record, auth: &str) -> Result, record: &Record, auth: &str) -> Result, sqlx::Error> { +async fn save_to_db(db: &Pool, record: &AccountWolves, auth: &str) -> Result, sqlx::Error> { sqlx::query_as::<_, AccountsNew>( " INSERT OR REPLACE INTO accounts_new (mail, auth_code, date_iso, date_expiry, name_first, name_surname, id_student) diff --git a/src/bin/update_groups.rs b/src/bin/update_groups.rs index 2676980..388960e 100644 --- a/src/bin/update_groups.rs +++ b/src/bin/update_groups.rs @@ -1,5 +1,5 @@ use ldap3::{LdapConn, Mod}; -use skynet_ldap_backend::{db_init, get_config, get_now_iso, read_csv, Accounts, Config}; +use skynet_ldap_backend::{db_init, get_config, get_now_iso, get_wolves, Accounts, Config}; use sqlx::{Pool, Sqlite}; use std::{collections::HashSet, env, error::Error}; @@ -116,9 +116,7 @@ async fn from_csv(config: &Config) -> Result, Box> { let mut uids = HashSet::new(); - let records = read_csv(config)?; - - for record in records { + for record in get_wolves(&db).await { // only import users if it is actually active. if record.expiry < get_now_iso(true) { continue; diff --git a/src/bin/update_wolves.rs b/src/bin/update_wolves.rs index b598f9f..9154072 100644 --- a/src/bin/update_wolves.rs +++ b/src/bin/update_wolves.rs @@ -1,20 +1,19 @@ -use skynet_ldap_backend::{db_init, get_config, Config}; +use skynet_ldap_backend::{db_init, get_config, AccountWolves, Config}; use sqlx::{Pool, Sqlite}; -use tide::prelude::{Serialize, Deserialize}; #[async_std::main] async fn main() -> tide::Result<()> { let config = get_config(); let db = db_init(&config).await.unwrap(); - + let mut records = vec![]; - - if let Ok(accounts) = get_csv(&config){ + + if let Ok(accounts) = get_csv(&config) { for account in accounts { records.push(AccountWolves::from(account)); } } - + for account in records { update_account(&db, &account).await; } @@ -22,16 +21,6 @@ async fn main() -> tide::Result<()> { Ok(()) } -#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] -pub struct AccountWolves { - pub id_wolves: String, - pub id_student: String, - pub email: String, - pub expiry: String, - pub name_first: String, - pub name_second: String, -} - #[derive(Debug, serde::Deserialize)] struct RecordCSV { #[serde(rename = "MemID")] @@ -49,7 +38,7 @@ struct RecordCSV { } impl From for AccountWolves { fn from(input: RecordCSV) -> Self { - AccountWolves{ + AccountWolves { id_wolves: input.mem_id, id_student: input.id_student, email: input.email, @@ -78,21 +67,20 @@ fn get_csv(config: &Config) -> Result, Box Ok(records) } - -async fn update_account(db: &Pool, account: &AccountWolves){ +async fn update_account(db: &Pool, account: &AccountWolves) { sqlx::query_as::<_, AccountWolves>( " INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_student, email, expiry, name_first, name_second) VALUES (?1, ?2, ?3, ?4, ?5, ?6) ", ) - .bind(&account.id_wolves) - .bind(&account.id_student) - .bind(&account.email) - .bind(&account.expiry) - .bind(&account.name_first) - .bind(&account.name_second) - .fetch_optional(db) - .await - .ok(); -} \ No newline at end of file + .bind(&account.id_wolves) + .bind(&account.id_student) + .bind(&account.email) + .bind(&account.expiry) + .bind(&account.name_first) + .bind(&account.name_second) + .fetch_optional(db) + .await + .ok(); +} diff --git a/src/lib.rs b/src/lib.rs index ce287ae..4a269ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,16 @@ use std::{ }; use tide::prelude::*; +#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] +pub struct AccountWolves { + pub id_wolves: String, + pub id_student: String, + pub email: String, + pub expiry: String, + pub name_first: String, + pub name_second: String, +} + #[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)] pub struct AccountsNew { pub mail: String, @@ -54,10 +64,9 @@ pub async fn db_init(config: &Config) -> Result, Error> { name_surname integer not null )", ) - .execute(&pool) - .await?; + .execute(&pool) + .await?; - sqlx::query( "CREATE TABLE IF NOT EXISTS accounts_new ( mail text primary key, @@ -264,3 +273,15 @@ async fn update_accounts(pool: &Pool, config: &Config) { pub fn random_string(len: usize) -> String { thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect() } + +pub async fn get_wolves(db: &Pool) -> Vec { + sqlx::query_as::<_, AccountWolves>( + r#" + SELECT * + FROM accounts_wolves + "#, + ) + .fetch_all(db) + .await + .unwrap_or(vec![]) +}