fmt: clippy and fmt

This commit is contained in:
silver 2023-07-30 01:19:19 +01:00
parent c3ed7fe02c
commit 4ce3e94c36
3 changed files with 59 additions and 67 deletions

View file

@ -5,9 +5,8 @@ use lettre::{
Message, SmtpTransport, Transport, Message, SmtpTransport, Transport,
}; };
use maud::html; use maud::html;
use rand::distributions::Alphanumeric; use rand::{distributions::Alphanumeric, thread_rng, Rng};
use rand::{thread_rng, Rng}; use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, AccountsNew, Config, Record};
use skynet_ldap_backend::{db_init, get_config, Accounts, AccountsNew, Config, read_csv, Record};
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
#[async_std::main] #[async_std::main]

View file

@ -1,10 +1,9 @@
use chrono::{Datelike, Utc}; use chrono::{Datelike, Utc};
use dotenvy::dotenv; use dotenvy::dotenv;
use ldap3::{LdapConn, Mod, Scope, SearchEntry}; use ldap3::{LdapConn, Mod};
use skynet_ldap_backend::{get_config, Config, read_csv}; use skynet_ldap_backend::{db_init, get_config, read_csv, Accounts, Config};
use std::collections::{HashMap, HashSet}; use sqlx::{Pool, Sqlite};
use std::env; use std::{collections::HashSet, env, error::Error};
use std::error::Error;
#[async_std::main] #[async_std::main]
async fn main() -> tide::Result<()> { async fn main() -> tide::Result<()> {
@ -126,61 +125,11 @@ async fn update_group(config: &Config, group: &str, users: &[&str], replace: boo
Ok(()) Ok(())
} }
async fn ldap_get_accounts(config: &Config) -> Result<(HashMap<String, String>, HashMap<String, String>), Box<dyn Error>> {
// 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<String, String> = HashMap::new();
let mut uid_email: HashMap<String, String> = 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<HashSet<String>, Box<dyn Error>> { async fn from_csv(config: &Config) -> Result<HashSet<String>, Box<dyn Error>> {
let db = db_init(config).await.unwrap();
let mut uids = HashSet::new(); let mut uids = HashSet::new();
let (uid_idstudent, uid_email) = ldap_get_accounts(config).await?;
let records = read_csv(config)?; let records = read_csv(config)?;
let now = Utc::now(); let now = Utc::now();
@ -192,13 +141,58 @@ async fn from_csv(config: &Config) -> Result<HashSet<String>, Box<dyn Error>> {
continue; continue;
} }
if let Some(uid) = uid_email.get(&record.email) { if let Some(uid) = account_mail_get_uid(&db, &record.email).await {
uids.insert(uid.clone()); uids.insert(uid);
} } else if let Some(uid) = account_id_get_uid(&db, &record.id_student).await {
if let Some(uid) = uid_idstudent.get(&record.id_student) { uids.insert(uid);
uids.insert(uid.clone());
} }
} }
Ok(uids) Ok(uids)
} }
async fn account_mail_get_uid(db: &Pool<Sqlite>, mail: &str) -> Option<String> {
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<Sqlite>, id: &str) -> Option<String> {
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,
}
}

View file

@ -252,7 +252,6 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
ldap.unbind().unwrap(); ldap.unbind().unwrap();
} }
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
pub struct Record { pub struct Record {
#[serde(rename = "MemID")] #[serde(rename = "MemID")]