fix: make sure that an old csv does not cause issues.

This commit is contained in:
silver 2023-07-17 00:38:02 +01:00
parent 6374b44e08
commit 480860ca26

View file

@ -1,3 +1,4 @@
use chrono::{Datelike, Utc};
use dotenv::dotenv; use dotenv::dotenv;
use ldap3::{LdapConn, Mod, Scope, SearchEntry}; use ldap3::{LdapConn, Mod, Scope, SearchEntry};
use skynet_ldap_backend::{get_config, Config}; use skynet_ldap_backend::{get_config, Config};
@ -34,6 +35,7 @@ async fn update_users(config: &Config) -> tide::Result<()> {
for every valid user in wolves match to ldap for every valid user in wolves match to ldap
add to users add to users
*/ */
println!("{:#?}", users_tmp);
// pull from wolves csv // pull from wolves csv
for user in from_csv(config).await.unwrap_or_default() { for user in from_csv(config).await.unwrap_or_default() {
users_tmp.insert(user); users_tmp.insert(user);
@ -46,6 +48,8 @@ async fn update_users(config: &Config) -> tide::Result<()> {
} }
} }
println!("{:#?}", users_tmp);
// easier to work with Strings above but easier to work with &str below // easier to work with Strings above but easier to work with &str below
let users: Vec<&str> = users_tmp.iter().map(|s| &**s).collect(); let users: Vec<&str> = users_tmp.iter().map(|s| &**s).collect();
@ -182,7 +186,15 @@ async fn from_csv(config: &Config) -> Result<HashSet<String>, Box<dyn Error>> {
let (uid_idstudent, uid_email) = ldap_get_accounts(config).await?; let (uid_idstudent, uid_email) = ldap_get_accounts(config).await?;
let records = read_csv()?; let records = read_csv()?;
let now = Utc::now();
let today = format!("{}-{:02}-{:02}", now.year(), now.month(), now.day());
for record in records { for record in records {
// only import users if it is actually active.
if record.expiry < today {
continue;
}
if let Some(uid) = uid_email.get(&record.email) { if let Some(uid) = uid_email.get(&record.email) {
uids.insert(uid.clone()); uids.insert(uid.clone());
} }
@ -202,6 +214,8 @@ struct Record {
id_student: String, id_student: String,
#[serde(rename = "Contact Email")] #[serde(rename = "Contact Email")]
email: String, email: String,
#[serde(rename = "Expiry")]
expiry: String,
} }
fn read_csv() -> Result<Vec<Record>, Box<dyn Error>> { fn read_csv() -> Result<Vec<Record>, Box<dyn Error>> {