feat: added the function to get teh data and updated teh times tehy run at #35
3 changed files with 115 additions and 34 deletions
|
@ -4,6 +4,8 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
[[bin]]
|
||||||
|
name = "update_wolves"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "update_groups"
|
name = "update_groups"
|
||||||
|
|
99
src/bin/update_wolves.rs
Normal file
99
src/bin/update_wolves.rs
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
use ldap3::{LdapConn, Scope, SearchEntry};
|
||||||
|
use skynet_ldap_backend::{db_init, get_config, Accounts, Config};
|
||||||
|
use sqlx::{Pool, Sqlite};
|
||||||
|
use tide::prelude::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
#[async_std::main]
|
||||||
|
async fn main() -> tide::Result<()> {
|
||||||
|
let config = get_config();
|
||||||
|
let db = db_init(&config).await.unwrap();
|
||||||
|
|
||||||
|
let mut records = vec![];
|
||||||
|
|
||||||
|
if let Ok(accounts) = get_csv(&config){
|
||||||
|
for account in accounts {
|
||||||
|
records.push(AccountWolves::from(account));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for account in records {
|
||||||
|
update_account(&db, &account).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
|
||||||
|
pub struct AccountWolves {
|
||||||
|
pub id_wolves: String,
|
||||||
|
pub id_student: String,
|
||||||
|
pub email: String,
|
||||||
|
pub expiry: String,
|
||||||
|
pub name_first: String,
|
||||||
|
pub name_second: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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 {
|
||||||
|
AccountWolves{
|
||||||
|
id_wolves: input.mem_id,
|
||||||
|
id_student: input.id_student,
|
||||||
|
email: input.email,
|
||||||
|
expiry: input.expiry,
|
||||||
|
name_first: input.name_first,
|
||||||
|
name_second: input.name_second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_csv(config: &Config) -> Result<Vec<RecordCSV>, Box<dyn std::error::Error>> {
|
||||||
|
let mut records: Vec<RecordCSV> = vec![];
|
||||||
|
|
||||||
|
if let Ok(mut rdr) = csv::Reader::from_path(format!("{}/{}", &config.home, &config.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(records)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves){
|
||||||
|
sqlx::query_as::<_, AccountWolves>(
|
||||||
|
"
|
||||||
|
INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_student, email, expiry, name_first, name_second)
|
||||||
|
VALUES (?1, ?2, ?3, ?4, ?5, ?6)
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.bind(&account.id_wolves)
|
||||||
|
.bind(&account.id_student)
|
||||||
|
.bind(&account.email)
|
||||||
|
.bind(&account.expiry)
|
||||||
|
.bind(&account.name_first)
|
||||||
|
.bind(&account.name_second)
|
||||||
|
.fetch_optional(db)
|
||||||
|
.await
|
||||||
|
.ok();
|
||||||
|
}
|
48
src/lib.rs
48
src/lib.rs
|
@ -44,6 +44,20 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
||||||
.connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true))
|
.connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
sqlx::query(
|
||||||
|
"CREATE TABLE IF NOT EXISTS accounts_wolves (
|
||||||
|
id_wolves text primary key,
|
||||||
|
id_student text not null,
|
||||||
|
email text not null,
|
||||||
|
expiry text not null,
|
||||||
|
name_first text not null,
|
||||||
|
name_surname integer not null
|
||||||
|
)",
|
||||||
|
)
|
||||||
|
.execute(&pool)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"CREATE TABLE IF NOT EXISTS accounts_new (
|
"CREATE TABLE IF NOT EXISTS accounts_new (
|
||||||
mail text primary key,
|
mail text primary key,
|
||||||
|
@ -246,40 +260,6 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
|
||||||
ldap.unbind().unwrap();
|
ldap.unbind().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, serde::Deserialize)]
|
|
||||||
pub struct Record {
|
|
||||||
#[serde(rename = "MemID")]
|
|
||||||
pub mem_id: String,
|
|
||||||
#[serde(rename = "Student Num")]
|
|
||||||
pub id_student: String,
|
|
||||||
#[serde(rename = "Contact Email")]
|
|
||||||
pub email: String,
|
|
||||||
#[serde(rename = "Expiry")]
|
|
||||||
pub expiry: String,
|
|
||||||
#[serde(rename = "First Name")]
|
|
||||||
pub name_first: String,
|
|
||||||
#[serde(rename = "Last Name")]
|
|
||||||
pub name_second: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_csv(config: &Config) -> Result<Vec<Record>, Box<dyn std::error::Error>> {
|
|
||||||
let mut records: Vec<Record> = vec![];
|
|
||||||
|
|
||||||
if let Ok(mut rdr) = csv::Reader::from_path(format!("{}/{}", &config.home, &config.csv)) {
|
|
||||||
for result in rdr.deserialize() {
|
|
||||||
// Notice that we need to provide a type hint for automatic
|
|
||||||
// deserialization.
|
|
||||||
let record: Record = result?;
|
|
||||||
if record.mem_id.is_empty() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
records.push(record);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(records)
|
|
||||||
}
|
|
||||||
|
|
||||||
// from https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html#create-random-passwords-from-a-set-of-alphanumeric-characters
|
// from https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html#create-random-passwords-from-a-set-of-alphanumeric-characters
|
||||||
pub fn random_string(len: usize) -> String {
|
pub fn random_string(len: usize) -> String {
|
||||||
thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
|
thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
|
||||||
|
|
Loading…
Reference in a new issue