feat: save wolves data in a table
This commit is contained in:
parent
dd5ebd6bd3
commit
58bf1e80fd
4 changed files with 64 additions and 59 deletions
|
@ -4,7 +4,7 @@ use lettre::{
|
|||
Message, SmtpTransport, Transport,
|
||||
};
|
||||
use maud::html;
|
||||
use skynet_ldap_backend::{db_init, get_config, get_now_iso, random_string, read_csv, Accounts, AccountsNew, Config, Record};
|
||||
use skynet_ldap_backend::{db_init, get_config, get_now_iso, get_wolves, random_string, AccountWolves, Accounts, AccountsNew, Config};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
#[async_std::main]
|
||||
|
@ -12,31 +12,29 @@ async fn main() {
|
|||
let config = get_config();
|
||||
let db = db_init(&config).await.unwrap();
|
||||
|
||||
if let Ok(records) = read_csv(&config) {
|
||||
for record in records {
|
||||
// skynet emails not permitted
|
||||
if record.email.trim().ends_with("@skynet.ie") {
|
||||
continue;
|
||||
}
|
||||
for record in get_wolves(&db).await {
|
||||
// skynet emails not permitted
|
||||
if record.email.trim().ends_with("@skynet.ie") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// check if the email is already in the db
|
||||
if !check(&db, &record.email).await {
|
||||
continue;
|
||||
}
|
||||
// check if the email is already in the db
|
||||
if !check(&db, &record.email).await {
|
||||
continue;
|
||||
}
|
||||
|
||||
// generate a auth key
|
||||
let auth = random_string(50);
|
||||
// generate a auth key
|
||||
let auth = random_string(50);
|
||||
|
||||
match send_mail(&config, &record, &auth) {
|
||||
Ok(_) => match save_to_db(&db, &record, &auth).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("Unable to save to db {} {e:?}", &record.email);
|
||||
}
|
||||
},
|
||||
match send_mail(&config, &record, &auth) {
|
||||
Ok(_) => match save_to_db(&db, &record, &auth).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("Unable to send mail to {} {e:?}", &record.email);
|
||||
println!("Unable to save to db {} {e:?}", &record.email);
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Unable to send mail to {} {e:?}", &record.email);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -75,7 +73,7 @@ async fn check_pending(db: &Pool<Sqlite>, mail: &str) -> bool {
|
|||
}
|
||||
|
||||
// using https://github.com/lettre/lettre/blob/57886c367d69b4d66300b322c94bd910b1eca364/examples/maud_html.rs
|
||||
fn send_mail(config: &Config, record: &Record, auth: &str) -> Result<Response, lettre::transport::smtp::Error> {
|
||||
fn send_mail(config: &Config, record: &AccountWolves, auth: &str) -> Result<Response, lettre::transport::smtp::Error> {
|
||||
let recipient = &record.name_first;
|
||||
let mail = &record.email;
|
||||
let url_base = "https://sso.skynet.ie";
|
||||
|
@ -180,7 +178,7 @@ fn send_mail(config: &Config, record: &Record, auth: &str) -> Result<Response, l
|
|||
mailer.send(&email)
|
||||
}
|
||||
|
||||
async fn save_to_db(db: &Pool<Sqlite>, record: &Record, auth: &str) -> Result<Option<AccountsNew>, sqlx::Error> {
|
||||
async fn save_to_db(db: &Pool<Sqlite>, record: &AccountWolves, auth: &str) -> Result<Option<AccountsNew>, sqlx::Error> {
|
||||
sqlx::query_as::<_, AccountsNew>(
|
||||
"
|
||||
INSERT OR REPLACE INTO accounts_new (mail, auth_code, date_iso, date_expiry, name_first, name_surname, id_student)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use ldap3::{LdapConn, Mod};
|
||||
use skynet_ldap_backend::{db_init, get_config, get_now_iso, read_csv, Accounts, Config};
|
||||
use skynet_ldap_backend::{db_init, get_config, get_now_iso, get_wolves, Accounts, Config};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use std::{collections::HashSet, env, error::Error};
|
||||
|
||||
|
@ -116,9 +116,7 @@ async fn from_csv(config: &Config) -> Result<HashSet<String>, Box<dyn Error>> {
|
|||
|
||||
let mut uids = HashSet::new();
|
||||
|
||||
let records = read_csv(config)?;
|
||||
|
||||
for record in records {
|
||||
for record in get_wolves(&db).await {
|
||||
// only import users if it is actually active.
|
||||
if record.expiry < get_now_iso(true) {
|
||||
continue;
|
||||
|
|
|
@ -1,20 +1,19 @@
|
|||
use skynet_ldap_backend::{db_init, get_config, Config};
|
||||
use skynet_ldap_backend::{db_init, get_config, AccountWolves, 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){
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -22,16 +21,6 @@ async fn main() -> tide::Result<()> {
|
|||
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")]
|
||||
|
@ -49,7 +38,7 @@ struct RecordCSV {
|
|||
}
|
||||
impl From<RecordCSV> for AccountWolves {
|
||||
fn from(input: RecordCSV) -> Self {
|
||||
AccountWolves{
|
||||
AccountWolves {
|
||||
id_wolves: input.mem_id,
|
||||
id_student: input.id_student,
|
||||
email: input.email,
|
||||
|
@ -78,21 +67,20 @@ fn get_csv(config: &Config) -> Result<Vec<RecordCSV>, Box<dyn std::error::Error>
|
|||
Ok(records)
|
||||
}
|
||||
|
||||
|
||||
async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves){
|
||||
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();
|
||||
}
|
||||
.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();
|
||||
}
|
||||
|
|
27
src/lib.rs
27
src/lib.rs
|
@ -14,6 +14,16 @@ use std::{
|
|||
};
|
||||
use tide::prelude::*;
|
||||
|
||||
#[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, Clone, Deserialize, Serialize, sqlx::FromRow)]
|
||||
pub struct AccountsNew {
|
||||
pub mail: String,
|
||||
|
@ -54,10 +64,9 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
|||
name_surname integer not null
|
||||
)",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
|
||||
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS accounts_new (
|
||||
mail text primary key,
|
||||
|
@ -264,3 +273,15 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
|
|||
pub fn random_string(len: usize) -> String {
|
||||
thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
|
||||
}
|
||||
|
||||
pub async fn get_wolves(db: &Pool<Sqlite>) -> Vec<AccountWolves> {
|
||||
sqlx::query_as::<_, AccountWolves>(
|
||||
r#"
|
||||
SELECT *
|
||||
FROM accounts_wolves
|
||||
"#,
|
||||
)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap_or(vec![])
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue