diff --git a/src/bin/new_users.rs b/src/bin/new_users.rs index b723b43..9e887a0 100644 --- a/src/bin/new_users.rs +++ b/src/bin/new_users.rs @@ -5,9 +5,8 @@ use lettre::{ Message, SmtpTransport, Transport, }; use maud::html; -use rand::distributions::Alphanumeric; -use rand::{thread_rng, Rng}; -use skynet_ldap_backend::{db_init, get_config, Accounts, AccountsNew, Config, read_csv, Record}; +use rand::{distributions::Alphanumeric, thread_rng, Rng}; +use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, AccountsNew, Config, Record}; use sqlx::{Pool, Sqlite}; #[async_std::main] diff --git a/src/bin/update_groups.rs b/src/bin/update_groups.rs index c97a951..61983dc 100644 --- a/src/bin/update_groups.rs +++ b/src/bin/update_groups.rs @@ -1,10 +1,9 @@ use chrono::{Datelike, Utc}; use dotenvy::dotenv; -use ldap3::{LdapConn, Mod, Scope, SearchEntry}; -use skynet_ldap_backend::{get_config, Config, read_csv}; -use std::collections::{HashMap, HashSet}; -use std::env; -use std::error::Error; +use ldap3::{LdapConn, Mod}; +use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, Config}; +use sqlx::{Pool, Sqlite}; +use std::{collections::HashSet, env, error::Error}; #[async_std::main] async fn main() -> tide::Result<()> { @@ -126,61 +125,11 @@ async fn update_group(config: &Config, group: &str, users: &[&str], replace: boo Ok(()) } -async fn ldap_get_accounts(config: &Config) -> Result<(HashMap, HashMap), Box> { - // connect to ldap - let mut ldap = LdapConn::new(&config.ldap_host)?; - ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw)?.success()?; - - let mut uid_idstudent: HashMap = HashMap::new(); - let mut uid_email: HashMap = HashMap::new(); - - let (rs, _res) = ldap - .search("ou=users,dc=skynet,dc=ie", Scope::OneLevel, "(objectClass=*)", vec!["uid", "mail", "skID", "skSecure"]) - .unwrap() - .success() - .unwrap(); - - for entry in rs { - let tmp = SearchEntry::construct(entry); - - // skSecure is a standin for teh password, only 1 if the password is SSHA512 - if !tmp.attrs.contains_key("skSecure") { - continue; - } - if tmp.attrs["skSecure"].is_empty() { - continue; - } - - // make sure there is an id; - let uid = if !tmp.attrs["uid"].is_empty() { - tmp.attrs["uid"][0].clone() - } else { - continue; - }; - - if !tmp.attrs["skID"].is_empty() { - let skid = tmp.attrs["skID"][0].clone(); - if &skid != "00000000" { - uid_idstudent.insert(skid, uid.clone()); - } - } - - if !tmp.attrs["mail"].is_empty() { - let mail = tmp.attrs["mail"][0].clone(); - if &mail != "nomail@skynet.ie" { - uid_email.insert(mail, uid.clone()); - } - } - } - ldap.unbind()?; - - Ok((uid_idstudent, uid_email)) -} - async fn from_csv(config: &Config) -> Result, Box> { + let db = db_init(config).await.unwrap(); + let mut uids = HashSet::new(); - let (uid_idstudent, uid_email) = ldap_get_accounts(config).await?; let records = read_csv(config)?; let now = Utc::now(); @@ -192,13 +141,58 @@ async fn from_csv(config: &Config) -> Result, Box> { continue; } - if let Some(uid) = uid_email.get(&record.email) { - uids.insert(uid.clone()); - } - if let Some(uid) = uid_idstudent.get(&record.id_student) { - uids.insert(uid.clone()); + if let Some(uid) = account_mail_get_uid(&db, &record.email).await { + uids.insert(uid); + } else if let Some(uid) = account_id_get_uid(&db, &record.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 user + FROM accounts + WHERE mail == ? + "#, + ) + .bind(mail) + .fetch_all(db) + .await + { + Ok(res) => { + if res.is_empty() { + None + } else { + Some(res[0].user.to_owned()) + } + } + Err(_) => None, + } +} + +async fn account_id_get_uid(db: &Pool, id: &str) -> Option { + match sqlx::query_as::<_, Accounts>( + r#" + SELECT student_id + FROM accounts + WHERE mail == ? + "#, + ) + .bind(id) + .fetch_all(db) + .await + { + Ok(res) => { + if res.is_empty() { + None + } else { + Some(res[0].student_id.to_owned()) + } + } + Err(_) => None, + } +} diff --git a/src/lib.rs b/src/lib.rs index 3609b48..e395452 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -252,7 +252,6 @@ async fn update_accounts(pool: &Pool, config: &Config) { ldap.unbind().unwrap(); } - #[derive(Debug, serde::Deserialize)] pub struct Record { #[serde(rename = "MemID")] @@ -285,4 +284,4 @@ pub fn read_csv(config: &Config) -> Result, Box