feat: frontload updating teh local db

This commit is contained in:
silver 2023-07-29 19:48:44 +01:00
parent 83a33e09f7
commit 2f27597525

View file

@ -25,9 +25,12 @@ pub struct AccountsPending {
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] #[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
pub struct Accounts { pub struct Accounts {
user: String, user: String,
uid_number: i64, uid: i64,
discord: Option<String>, discord: Option<String>,
mail: String,
student_id: String,
enabled: bool, enabled: bool,
secure: bool,
} }
pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> { pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
@ -55,15 +58,18 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts ( "CREATE TABLE IF NOT EXISTS accounts (
user text primary key, user text primary key,
uid_number integer not null, uid integer not null,
discord text, 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) .execute(&pool)
.await?; .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) .execute(&pool)
.await?; .await?;
@ -141,16 +147,25 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw).unwrap().success().unwrap(); 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() { if let Ok((rs, _res)) = x.success() {
for entry in rs { for entry in rs {
let tmp = SearchEntry::construct(entry); let tmp = SearchEntry::construct(entry);
let mut tmp_account = Accounts { let mut tmp_account = Accounts {
user: "".to_string(), user: "".to_string(),
uid_number: 0, uid: 0,
discord: None, discord: None,
mail: "".to_string(),
student_id: "".to_string(),
enabled: false, enabled: false,
secure: false,
}; };
// pull out the required info // pull out the required info
@ -158,29 +173,41 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
tmp_account.user = tmp.attrs["uid"][0].clone(); tmp_account.user = tmp.attrs["uid"][0].clone();
} }
if tmp.attrs.contains_key("uidNumber") && !tmp.attrs["uidNumber"].is_empty() { 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() { if tmp.attrs.contains_key("skDiscord") && !tmp.attrs["skDiscord"].is_empty() {
tmp_account.discord = Option::from(tmp.attrs["skDiscord"][0].clone()); 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") if tmp.attrs.contains_key("skMemberOf")
&& !tmp.attrs["skMemberOf"].is_empty() && !tmp.attrs["skMemberOf"].is_empty()
&& tmp.attrs["skMemberOf"].contains(&String::from("cn=skynet-users-linux,ou=groups,dc=skynet,dc=ie")) && tmp.attrs["skMemberOf"].contains(&String::from("cn=skynet-users-linux,ou=groups,dc=skynet,dc=ie"))
{ {
tmp_account.enabled = true; 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() { if !tmp_account.user.is_empty() {
sqlx::query_as::<_, Accounts>( sqlx::query_as::<_, Accounts>(
" "
INSERT OR REPLACE INTO accounts (user, uid_number, discord, enabled) INSERT OR REPLACE INTO accounts (user, uid, discord, mail, student_id, enabled, secure)
VALUES (?1, ?2, ?3, ?4) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
", ",
) )
.bind(&tmp_account.user) .bind(&tmp_account.user)
.bind(tmp_account.uid_number) .bind(tmp_account.uid)
.bind(&tmp_account.discord) .bind(&tmp_account.discord)
.bind(&tmp_account.mail)
.bind(&tmp_account.student_id)
.bind(tmp_account.enabled) .bind(tmp_account.enabled)
.bind(tmp_account.secure)
.fetch_optional(pool) .fetch_optional(pool)
.await .await
.ok(); .ok();