fmt: fmt and clippy

This commit is contained in:
silver 2023-07-17 00:14:48 +01:00
parent b9bcd5352e
commit 6374b44e08
3 changed files with 32 additions and 45 deletions

View file

@ -1,10 +1,9 @@
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};
use std::{env, io};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::env;
use std::error::Error; use std::error::Error;
use csv;
#[async_std::main] #[async_std::main]
async fn main() -> tide::Result<()> { async fn main() -> tide::Result<()> {
@ -18,21 +17,16 @@ async fn main() -> tide::Result<()> {
} }
async fn update_users(config: &Config) -> tide::Result<()> { async fn update_users(config: &Config) -> tide::Result<()> {
let mut users_tmp = vec![ let mut users_tmp = HashSet::new();
// default user to ensure group is never empty // default user to ensure group is never empty
String::from("compsoc"), users_tmp.insert(String::from("compsoc"));
];
// add lifetime folks
if let Ok(x) = env::var("USERS_LIFETIME") { if let Ok(x) = env::var("USERS_LIFETIME") {
for user in x.split(',').collect::<Vec<&str>>() { for user in x.split(',').collect::<Vec<&str>>() {
users_tmp.push(user.to_string()); users_tmp.insert(user.to_string());
} }
} }
for user in from_csv(config).await.unwrap_or_default() {
users_tmp.push(user);
}
/* /*
pull in data from wolves (csv or api (hopefully api) pull in data from wolves (csv or api (hopefully api)
pull entire ldap data pull entire ldap data
@ -40,16 +34,15 @@ 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
*/ */
// pull from wolves csv
for user in from_csv(config).await.unwrap_or_default() {
users_tmp.insert(user);
}
// sorting makes it easier/faster // sorting makes it easier/faster
users_tmp.sort();
if let Ok(x) = env::var("USERS_BANNED") { if let Ok(x) = env::var("USERS_BANNED") {
for user in x.split(',').collect::<Vec<&str>>() { for user in x.split(',').collect::<Vec<&str>>() {
// find its position users_tmp.remove(user);
while let Ok(index) = users_tmp.binary_search(&user.to_string()) {
// in case it just so happens to be there multiple times
users_tmp.remove(index);
}
} }
} }
@ -132,8 +125,7 @@ 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>>{ async fn ldap_get_accounts(config: &Config) -> Result<(HashMap<String, String>, HashMap<String, String>), Box<dyn Error>> {
// connect to ldap // connect to ldap
let mut ldap = LdapConn::new(&config.ldap_host)?; let mut ldap = LdapConn::new(&config.ldap_host)?;
ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw)?.success()?; ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw)?.success()?;
@ -141,31 +133,27 @@ async fn ldap_get_accounts(config: &Config) -> Result<(HashMap<String, String>,
let mut uid_idstudent: HashMap<String, String> = HashMap::new(); let mut uid_idstudent: HashMap<String, String> = HashMap::new();
let mut uid_email: HashMap<String, String> = HashMap::new(); let mut uid_email: HashMap<String, String> = HashMap::new();
let (rs, _res) = ldap.search( let (rs, _res) = ldap
"ou=users,dc=skynet,dc=ie", .search("ou=users,dc=skynet,dc=ie", Scope::OneLevel, "(objectClass=*)", vec!["uid", "mail", "skID", "skSecure"])
Scope::OneLevel, .unwrap()
"(objectClass=*)", .success()
vec!["uid", "mail", "skID", "skSecure"] .unwrap();
)
.unwrap()
.success()
.unwrap();
for entry in rs { for entry in rs {
let tmp = SearchEntry::construct(entry); let tmp = SearchEntry::construct(entry);
// skSecure is a standin for teh password, only 1 if the password is SSHA512 // skSecure is a standin for teh password, only 1 if the password is SSHA512
if !tmp.attrs.contains_key("skSecure"){ if !tmp.attrs.contains_key("skSecure") {
continue; continue;
} }
if tmp.attrs["skSecure"].is_empty() { if tmp.attrs["skSecure"].is_empty() {
continue; continue;
} }
// make sure there is an id; // make sure there is an id;
let uid = if !tmp.attrs["uid"].is_empty() { let uid = if !tmp.attrs["uid"].is_empty() {
tmp.attrs["uid"][0].clone() tmp.attrs["uid"][0].clone()
}else { } else {
continue; continue;
}; };
@ -184,16 +172,16 @@ async fn ldap_get_accounts(config: &Config) -> Result<(HashMap<String, String>,
} }
} }
ldap.unbind()?; ldap.unbind()?;
Ok((uid_idstudent, uid_email)) 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 mut uids = HashSet::new(); let mut uids = HashSet::new();
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()?;
for record in records { for record in records {
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,15 +190,14 @@ async fn from_csv(config: &Config) -> Result<HashSet<String>, Box<dyn Error>>{
uids.insert(uid.clone()); uids.insert(uid.clone());
} }
} }
Ok(uids) Ok(uids)
} }
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
struct Record { struct Record {
#[serde(rename = "MemID")] // #[serde(rename = "MemID")]
id_wolves: String, // id_wolves: String,
#[serde(rename = "Student Num")] #[serde(rename = "Student Num")]
id_student: String, id_student: String,
#[serde(rename = "Contact Email")] #[serde(rename = "Contact Email")]
@ -219,7 +206,7 @@ struct Record {
fn read_csv() -> Result<Vec<Record>, Box<dyn Error>> { fn read_csv() -> Result<Vec<Record>, Box<dyn Error>> {
let mut records: Vec<Record> = vec![]; let mut records: Vec<Record> = vec![];
if let Ok(mut rdr) = csv::Reader::from_path("tmp.csv") { if let Ok(mut rdr) = csv::Reader::from_path("tmp.csv") {
for result in rdr.deserialize() { for result in rdr.deserialize() {
// Notice that we need to provide a type hint for automatic // Notice that we need to provide a type hint for automatic
@ -228,6 +215,6 @@ fn read_csv() -> Result<Vec<Record>, Box<dyn Error>> {
records.push(record); records.push(record);
} }
} }
Ok(records) Ok(records)
} }

View file

@ -296,7 +296,7 @@ async fn account_verification_new_account(ldap: &mut LdapConn, user_details: &Ac
// need to get this from wolves // need to get this from wolves
//("skID", HashSet::from(["12345678"])), //("skID", HashSet::from(["12345678"])),
("skCreated", HashSet::from([sk_created.as_str()])), ("skCreated", HashSet::from([sk_created.as_str()])),
// 1 = secure, automatic since its a new account // 1 = secure, automatic since its a new account
("skSecure", HashSet::from(["1"])), ("skSecure", HashSet::from(["1"])),
], ],
)? )?

View file

@ -54,12 +54,12 @@ pub async fn post_update_ldap(mut req: Request<State>) -> tide::Result {
// main value we are updating // main value we are updating
Mod::Replace(field, HashSet::from([value])), Mod::Replace(field, HashSet::from([value])),
]; ];
// if teh password is changing then its inherentrly secure, same if its currently an empty field // if teh password is changing then its inherentrly secure, same if its currently an empty field
if !pw_keep_same || !pw_secure { if !pw_keep_same || !pw_secure {
mods.push(Mod::Replace(String::from("skSecure"), HashSet::from([String::from("1")]))); mods.push(Mod::Replace(String::from("skSecure"), HashSet::from([String::from("1")])));
} }
ldap.modify(&dn, mods)?.success()?; ldap.modify(&dn, mods)?.success()?;
// pass back the "old" and "new" passwords // pass back the "old" and "new" passwords