diff --git a/src/bin/update_groups.rs b/src/bin/update_groups.rs index 6ae177c..2009998 100644 --- a/src/bin/update_groups.rs +++ b/src/bin/update_groups.rs @@ -1,6 +1,6 @@ -use skynet_ldap_backend::{db_init, get_config, update_group, Accounts, Config}; +use skynet_ldap_backend::{db_init, get_config, get_now_iso, get_wolves, update_group, Accounts, Config}; use sqlx::{Pool, Sqlite}; -use std::{collections::HashSet, env}; +use std::{collections::HashSet, env, error::Error}; #[async_std::main] async fn main() -> tide::Result<()> { @@ -32,6 +32,11 @@ async fn update(config: &Config) -> tide::Result<()> { } } + // pull from wolves csv + for user in from_csv(&db).await.unwrap_or_default() { + users_tmp.insert(user); + } + get_from_env(&mut users_tmp, &mut admins_tmp, "USERS_ADMIN"); get_from_env(&mut users_tmp, &mut committee_tmp, "USERS_COMMITTEE"); get_from_env(&mut users_tmp, &mut trainees_tmp, "USERS_TRAINEE"); @@ -68,6 +73,60 @@ fn get_from_env(users: &mut HashSet, other: &mut HashSet, key: & } } +async fn from_csv(db: &Pool) -> Result, Box> { + let mut uids = HashSet::new(); + + for record in get_wolves(db).await { + // only import users if it is actually active. + if record.expiry < get_now_iso(true) { + continue; + } + if let Some(uid) = account_mail_get_uid(db, &record.email).await { + uids.insert(uid); + } else if let Some(id_student) = record.id_student { + if let Some(uid) = account_id_get_uid(db, &id_student).await { + uids.insert(uid); + } + } + } + + Ok(uids) +} + +async fn account_mail_get_uid(db: &Pool, mail: &str) -> Option { + match sqlx::query_as::<_, Accounts>( + r#" + SELECT * + FROM accounts + WHERE mail == ? + "#, + ) + .bind(mail) + .fetch_one(db) + .await + { + Ok(res) => Some(res.user.to_owned()), + Err(_) => None, + } +} + +async fn account_id_get_uid(db: &Pool, id: &str) -> Option { + match sqlx::query_as::<_, Accounts>( + r#" + SELECT * + FROM accounts + WHERE student_id == ? + "#, + ) + .bind(id) + .fetch_one(db) + .await + { + Ok(res) => Some(res.student_id.to_owned()), + Err(_) => None, + } +} + struct AccountsSecure { users: Vec, admins: Vec,