From 2f27597525e65b8bdd7387e292dbed9e0e7eddc3 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sat, 29 Jul 2023 19:48:44 +0100 Subject: [PATCH] feat: frontload updating teh local db --- src/lib.rs | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 066190f..a87ae4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,9 +25,12 @@ pub struct AccountsPending { #[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] pub struct Accounts { user: String, - uid_number: i64, + uid: i64, discord: Option, + mail: String, + student_id: String, enabled: bool, + secure: bool, } pub async fn db_init(config: &Config) -> Result, Error> { @@ -55,15 +58,18 @@ pub async fn db_init(config: &Config) -> Result, Error> { sqlx::query( "CREATE TABLE IF NOT EXISTS accounts ( user text primary key, - uid_number integer not null, + uid integer not null, discord text, - enabled integer not null + mail text not null, + student_id text not null, + enabled integer not null, + secure integer not null )", ) .execute(&pool) .await?; - sqlx::query("CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid_number)") + sqlx::query("CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid)") .execute(&pool) .await?; @@ -141,16 +147,25 @@ async fn update_accounts(pool: &Pool, config: &Config) { ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw).unwrap().success().unwrap(); - if let Ok(x) = ldap.search("ou=users,dc=skynet,dc=ie", Scope::OneLevel, "(objectClass=*)", vec!["uid", "uidNumber", "skDiscord", "skMemberOf"]) { + // use this to pre load a large chunk of data + if let Ok(x) = ldap.search( + "ou=users,dc=skynet,dc=ie", + Scope::OneLevel, + "(objectClass=*)", + vec!["uid", "uidNumber", "skDiscord", "skMemberOf", "mail", "skID", "skSecure"] + ) { if let Ok((rs, _res)) = x.success() { for entry in rs { let tmp = SearchEntry::construct(entry); let mut tmp_account = Accounts { user: "".to_string(), - uid_number: 0, + uid: 0, discord: None, + mail: "".to_string(), + student_id: "".to_string(), enabled: false, + secure: false, }; // pull out the required info @@ -158,29 +173,41 @@ async fn update_accounts(pool: &Pool, config: &Config) { tmp_account.user = tmp.attrs["uid"][0].clone(); } if tmp.attrs.contains_key("uidNumber") && !tmp.attrs["uidNumber"].is_empty() { - tmp_account.uid_number = tmp.attrs["uidNumber"][0].clone().parse().unwrap_or(0); + tmp_account.uid = tmp.attrs["uidNumber"][0].clone().parse().unwrap_or(0); } if tmp.attrs.contains_key("skDiscord") && !tmp.attrs["skDiscord"].is_empty() { tmp_account.discord = Option::from(tmp.attrs["skDiscord"][0].clone()); } + if tmp.attrs.contains_key("mail") && !tmp.attrs["mail"].is_empty() { + tmp_account.mail = tmp.attrs["mail"][0].clone(); + } + if tmp.attrs.contains_key("skID") && !tmp.attrs["skID"].is_empty() { + tmp_account.student_id = tmp.attrs["skID"][0].clone(); + } if tmp.attrs.contains_key("skMemberOf") && !tmp.attrs["skMemberOf"].is_empty() && tmp.attrs["skMemberOf"].contains(&String::from("cn=skynet-users-linux,ou=groups,dc=skynet,dc=ie")) { tmp_account.enabled = true; } + if tmp.attrs.contains_key("skSecure") && !tmp.attrs["skSecure"].is_empty() { + tmp_account.secure = true; + } if !tmp_account.user.is_empty() { sqlx::query_as::<_, Accounts>( " - INSERT OR REPLACE INTO accounts (user, uid_number, discord, enabled) - VALUES (?1, ?2, ?3, ?4) + INSERT OR REPLACE INTO accounts (user, uid, discord, mail, student_id, enabled, secure) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) ", ) .bind(&tmp_account.user) - .bind(tmp_account.uid_number) + .bind(tmp_account.uid) .bind(&tmp_account.discord) + .bind(&tmp_account.mail) + .bind(&tmp_account.student_id) .bind(tmp_account.enabled) + .bind(tmp_account.secure) .fetch_optional(pool) .await .ok();