feat: added the function to get teh data and updated teh times tehy run at #35
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,8 +12,7 @@ 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 {
|
||||
for record in get_wolves(&db).await {
|
||||
// skynet emails not permitted
|
||||
if record.email.trim().ends_with("@skynet.ie") {
|
||||
continue;
|
||||
|
@ -40,7 +39,6 @@ async fn main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn check(db: &Pool<Sqlite>, mail: &str) -> bool {
|
||||
check_pending(db, mail).await && check_users(db, mail).await
|
||||
|
@ -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,6 +1,5 @@
|
|||
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<()> {
|
||||
|
@ -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")]
|
||||
|
@ -78,7 +67,6 @@ fn get_csv(config: &Config) -> Result<Vec<RecordCSV>, Box<dyn std::error::Error>
|
|||
Ok(records)
|
||||
}
|
||||
|
||||
|
||||
async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves) {
|
||||
sqlx::query_as::<_, AccountWolves>(
|
||||
"
|
||||
|
|
23
src/lib.rs
23
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,
|
||||
|
@ -57,7 +67,6 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
|||
.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