feat: nixos side of the config #45
2 changed files with 59 additions and 47 deletions
|
@ -1,4 +1,5 @@
|
|||
use ldap3::{LdapConn, Scope, SearchEntry};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use skynet_ldap_backend::{db_init, get_config, AccountWolves, Accounts, Config};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
|
@ -14,15 +15,7 @@ async fn main() -> tide::Result<()> {
|
|||
}
|
||||
|
||||
async fn update_wolves(config: &Config, db: &Pool<Sqlite>) {
|
||||
let mut records = vec![];
|
||||
|
||||
if let Ok(accounts) = get_csv(config) {
|
||||
for account in accounts {
|
||||
records.push(AccountWolves::from(account));
|
||||
}
|
||||
}
|
||||
|
||||
for account in records {
|
||||
for account in get_wolves(config).await {
|
||||
update_account(db, &account).await;
|
||||
}
|
||||
}
|
||||
|
@ -103,55 +96,64 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
|||
// done with ldap
|
||||
ldap.unbind().unwrap();
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
struct RecordCSV {
|
||||
#[serde(rename = "MemID")]
|
||||
mem_id: String,
|
||||
#[serde(rename = "Student Num")]
|
||||
id_student: String,
|
||||
#[serde(rename = "Contact Email")]
|
||||
email: String,
|
||||
#[serde(rename = "Expiry")]
|
||||
expiry: String,
|
||||
#[serde(rename = "First Name")]
|
||||
name_first: String,
|
||||
#[serde(rename = "Last Name")]
|
||||
name_second: String,
|
||||
}
|
||||
impl From<RecordCSV> for AccountWolves {
|
||||
fn from(input: RecordCSV) -> Self {
|
||||
impl From<&WolvesResultUser> for AccountWolves {
|
||||
fn from(input: &WolvesResultUser) -> Self {
|
||||
AccountWolves {
|
||||
id_wolves: input.mem_id,
|
||||
id_student: if input.id_student.is_empty() { None } else { Some(input.id_student) },
|
||||
email: input.email,
|
||||
expiry: input.expiry,
|
||||
name_first: if input.name_first.is_empty() { None } else { Some(input.name_first) },
|
||||
name_second: if input.name_second.is_empty() { None } else { Some(input.name_second) },
|
||||
id_wolves: input.wolves_id.to_owned(),
|
||||
id_student: input.student_id.to_owned(),
|
||||
email: input.email.to_owned(),
|
||||
expiry: input.expiry.to_owned(),
|
||||
name_first: Some(input.first_name.to_owned()),
|
||||
name_second: Some(input.last_name.to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_csv(config: &Config) -> Result<Vec<RecordCSV>, Box<dyn std::error::Error>> {
|
||||
let mut records: Vec<RecordCSV> = vec![];
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResultUser {
|
||||
committee: String,
|
||||
wolves_id: String,
|
||||
first_name: String,
|
||||
last_name: String,
|
||||
email: String,
|
||||
student_id: Option<String>,
|
||||
note: Option<String>,
|
||||
expiry: String,
|
||||
requested: String,
|
||||
approved: String,
|
||||
sitename: String,
|
||||
domain: String
|
||||
}
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct WolvesResult {
|
||||
success: i8,
|
||||
result: Vec<WolvesResultUser>
|
||||
}
|
||||
|
||||
let csv = format!("{}/{}", &config.home, &config.csv);
|
||||
println!("CSV: {:?}", &csv);
|
||||
if let Ok(mut rdr) = csv::Reader::from_path(csv) {
|
||||
for result in rdr.deserialize() {
|
||||
// Notice that we need to provide a type hint for automatic
|
||||
// deserialization.
|
||||
let record: RecordCSV = result?;
|
||||
if record.mem_id.is_empty() {
|
||||
continue;
|
||||
}
|
||||
records.push(record);
|
||||
}
|
||||
async fn get_wolves(config: &Config) -> Vec<AccountWolves> {
|
||||
if config.wolves_key.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
if config.wolves_url.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
Ok(records)
|
||||
// get wolves data
|
||||
let uri = &config.wolves_url;
|
||||
let mut res = surf::post(uri).header("X-AM-Identity", &config.wolves_key).await.unwrap();
|
||||
|
||||
if let Ok(WolvesResult { success, result }) = res.body_json().await {
|
||||
if success != 1 {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
return result.iter().map(|wolves| AccountWolves::from(wolves) ).collect::<Vec<AccountWolves>>();
|
||||
}
|
||||
|
||||
vec![]
|
||||
}
|
||||
|
||||
|
||||
async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves) {
|
||||
sqlx::query_as::<_, AccountWolves>(
|
||||
"
|
||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -188,6 +188,8 @@ pub struct Config {
|
|||
pub ssh_root: String,
|
||||
pub auth_discord: String,
|
||||
pub users_restricted: Vec<String>,
|
||||
pub wolves_url: String,
|
||||
pub wolves_key: String,
|
||||
}
|
||||
|
||||
pub fn get_config() -> Config {
|
||||
|
@ -208,6 +210,8 @@ pub fn get_config() -> Config {
|
|||
ssh_root: "skynet_old".to_string(),
|
||||
auth_discord: "".to_string(),
|
||||
users_restricted: vec![],
|
||||
wolves_url: "".to_string(),
|
||||
wolves_key: "".to_string(),
|
||||
};
|
||||
|
||||
if let Ok(x) = env::var("LDAP_HOST") {
|
||||
|
@ -246,6 +250,12 @@ pub fn get_config() -> Config {
|
|||
if let Ok(x) = env::var("LDAP_DISCORD_AUTH") {
|
||||
config.auth_discord = x.trim().to_string();
|
||||
}
|
||||
if let Ok(x) = env::var("WOLVES_URL") {
|
||||
config.wolves_url = x.trim().to_string();
|
||||
}
|
||||
if let Ok(x) = env::var("WOLVES_KEY") {
|
||||
config.wolves_key = x.trim().to_string();
|
||||
}
|
||||
|
||||
if let Ok(x) = env::var("USERS_RESTRICTED") {
|
||||
// usernames that are restricted
|
||||
|
|
Loading…
Reference in a new issue