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,
};
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]

View file

@ -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<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>> {
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<HashSet<String>, Box<dyn Error>> {
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<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();
}
#[derive(Debug, serde::Deserialize)]
pub struct Record {
#[serde(rename = "MemID")]
@ -285,4 +284,4 @@ pub fn read_csv(config: &Config) -> Result<Vec<Record>, Box<dyn std::error::Erro
}
Ok(records)
}
}