diff --git a/src/bin/update_data.rs b/src/bin/update_data.rs index 93a9b06..c94d4e7 100644 --- a/src/bin/update_data.rs +++ b/src/bin/update_data.rs @@ -94,9 +94,9 @@ async fn update_ldap(config: &Config, db: &Pool) { if !tmp_account.user.is_empty() { sqlx::query_as::<_, Accounts>( " - INSERT OR REPLACE INTO accounts (user, uid, discord, mail, student_id, enabled, secure) - VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) - ", + INSERT OR REPLACE INTO accounts (user, uid, discord, mail, student_id, enabled, secure) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) + ", ) .bind(&tmp_account.user) .bind(tmp_account.uid) @@ -135,8 +135,7 @@ struct RecordCSV { impl From for AccountWolves { fn from(input: RecordCSV) -> Self { AccountWolves { - id_wolves: "".to_string(), - id_member: input.mem_id, + 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, @@ -169,12 +168,11 @@ fn get_csv(config: &Config) -> Result, Box async fn update_account(db: &Pool, account: &AccountWolves) { sqlx::query_as::<_, AccountWolves>( " - INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_member, id_student, email, expiry, name_first, name_second) - VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) - ", + 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_member) .bind(&account.id_student) .bind(&account.email) .bind(&account.expiry) diff --git a/src/bin/update_groups.rs b/src/bin/update_groups.rs index 81ec640..a3b0529 100644 --- a/src/bin/update_groups.rs +++ b/src/bin/update_groups.rs @@ -90,10 +90,10 @@ async fn from_csv(db: &Pool) -> Result, Box> async fn account_mail_get_uid(db: &Pool, mail: &str) -> Option { match sqlx::query_as::<_, Accounts>( r#" - SELECT * - FROM accounts - WHERE mail == ? - "#, + SELECT * + FROM accounts + WHERE mail == ? + "#, ) .bind(mail) .fetch_one(db) @@ -107,10 +107,10 @@ async fn account_mail_get_uid(db: &Pool, mail: &str) -> Option { async fn account_id_get_uid(db: &Pool, id: &str) -> Option { match sqlx::query_as::<_, Accounts>( r#" - SELECT * - FROM accounts - WHERE student_id == ? - "#, + SELECT * + FROM accounts + WHERE student_id == ? + "#, ) .bind(id) .fetch_one(db) @@ -160,10 +160,10 @@ async fn get_secure_sub(db: &Pool, group: &HashSet, cache: &mut async fn is_secure(db: &Pool, user: &str) -> bool { match sqlx::query_as::<_, Accounts>( r#" - SELECT * - FROM accounts - WHERE user == ? AND secure == 1 - "#, + SELECT * + FROM accounts + WHERE user == ? AND secure == 1 + "#, ) .bind(user) .fetch_all(db) diff --git a/src/lib.rs b/src/lib.rs index 66e116d..3fe40b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ pub mod methods; + use chrono::{Datelike, SecondsFormat, Utc}; use dotenvy::dotenv; use ldap3::{LdapConn, Mod}; @@ -17,7 +18,6 @@ use tide::prelude::*; #[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] pub struct AccountWolves { pub id_wolves: String, - pub id_member: String, pub id_student: Option, pub email: String, pub expiry: String, @@ -71,29 +71,27 @@ pub async fn db_init(config: &Config) -> Result, Error> { sqlx::query( "CREATE TABLE IF NOT EXISTS accounts_wolves ( - id_wolves text DEFAULT '', - id_member text DEFAULT '', - id_student text, - email text not null, - expiry text not null, - name_first text, - name_second text, - PRIMARY KEY (id_wolves, id_member) - )", + id_wolves text PRIMARY KEY, + id_student text, + email text NOT NULL, + expiry text NOT NULL, + name_first text, + name_second text + )", ) .execute(&pool) .await?; sqlx::query( "CREATE TABLE IF NOT EXISTS accounts_new ( - mail text primary key, - auth_code text not null, - date_iso text not null, - date_expiry text not null, - name_first text not null, - name_surname integer not null, - id_student text not null - )", + mail text PRIMARY KEY, + auth_code text NOT NULL, + date_iso text NOT NULL, + date_expiry text NOT NULL, + name_first text NOT NULL, + name_surname integer NOT NULL, + id_student text NOT NULL + )", ) .execute(&pool) .await?; @@ -107,20 +105,20 @@ pub async fn db_init(config: &Config) -> Result, Error> { sqlx::query( "CREATE TABLE IF NOT EXISTS accounts_ssh ( - user text primary key, - auth_code text not null, - email text not null - )", + user text PRIMARY KEY, + auth_code text NOT NULL, + email text NOT NULL + )", ) .execute(&pool) .await?; sqlx::query( "CREATE TABLE IF NOT EXISTS accounts_reset ( - user text primary key, - auth_code text not null, - date_expiry text not null - )", + user text PRIMARY KEY, + auth_code text NOT NULL, + date_expiry text NOT NULL + )", ) .execute(&pool) .await?; @@ -135,14 +133,14 @@ pub async fn db_init(config: &Config) -> Result, Error> { // this is for active use sqlx::query( "CREATE TABLE IF NOT EXISTS accounts ( - user text primary key, - uid integer not null, - discord text, - mail text not null, - student_id text not null, - enabled integer not null, - secure integer not null - )", + user text PRIMARY KEY, + uid integer NOT NULL, + discord text, + mail text NOT NULL, + student_id text NOT NULL, + enabled integer NOT NULL, + secure integer NOT NULL + )", ) .execute(&pool) .await?; @@ -271,9 +269,9 @@ pub fn random_string(len: usize) -> String { pub async fn get_wolves(db: &Pool) -> Vec { sqlx::query_as::<_, AccountWolves>( r#" - SELECT * - FROM accounts_wolves - "#, + SELECT * + FROM accounts_wolves + "#, ) .fetch_all(db) .await diff --git a/src/main.rs b/src/main.rs index a038760..262d918 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use skynet_ldap_backend::{ db_init, get_config, - methods::{account_new, account_recover, account_update, discord}, + methods::{account_new, account_recover, account_update}, State, }; @@ -34,9 +34,6 @@ async fn main() -> tide::Result<()> { app.at("/ldap/recover/ssh/request").post(account_recover::ssh::request); app.at("/ldap/recover/ssh/verify").post(account_recover::ssh::verify); - // for discord - app.at("/ldap/discord").get(discord::account::get); - app.listen(host_port).await?; Ok(()) } diff --git a/src/methods/account_recover.rs b/src/methods/account_recover.rs index 79148ec..12dab3b 100644 --- a/src/methods/account_recover.rs +++ b/src/methods/account_recover.rs @@ -231,47 +231,47 @@ pub mod password { // Create the html we want to send. let html = html! { - head { - title { "Hello from Skynet!" } - style type="text/css" { - "h2, h4 { font-family: Arial, Helvetica, sans-serif; }" - } + head { + title { "Hello from Skynet!" } + style type="text/css" { + "h2, h4 { font-family: Arial, Helvetica, sans-serif; }" } - div style="display: flex; flex-direction: column; align-items: center;" { - h2 { "Hello from Skynet!" } - // Substitute in the name of our recipient. - p { "Hi " (recipient) "," } - p { - "Here is your password reset link:" - br; - a href=(link_new) { (link_new) } - } - p { - "If did not request this please ignore." - } - p { - "UL Computer Society" - br; - "Skynet Team" - br; - a href=(discord) { (discord) } - } + } + div style="display: flex; flex-direction: column; align-items: center;" { + h2 { "Hello from Skynet!" } + // Substitute in the name of our recipient. + p { "Hi " (recipient) "," } + p { + "Here is your password reset link:" + br; + a href=(link_new) { (link_new) } } + p { + "If did not request this please ignore." + } + p { + "UL Computer Society" + br; + "Skynet Team" + br; + a href=(discord) { (discord) } + } + } }; let body_text = format!( r#" - Hi {recipient} - - Here is your password reset link: - {link_new} - - If did not request this please ignore. - - UL Computer Society - Skynet Team - {discord} - "# + Hi {recipient} + + Here is your password reset link: + {link_new} + + If did not request this please ignore. + + UL Computer Society + Skynet Team + {discord} + "# ); // Build the message. @@ -368,44 +368,44 @@ pub mod username { // Create the html we want to send. let html = html! { - head { - title { "Hello from Skynet!" } - style type="text/css" { - "h2, h4 { font-family: Arial, Helvetica, sans-serif; }" - } + head { + title { "Hello from Skynet!" } + style type="text/css" { + "h2, h4 { font-family: Arial, Helvetica, sans-serif; }" } - div style="display: flex; flex-direction: column; align-items: center;" { - h2 { "Hello from Skynet!" } - // Substitute in the name of our recipient. - p { "Hi there," } - p { - "You requested a username reminder: " (recipient) - } - p { - "If did not request this please ignore." - } - p { - "UL Computer Society" - br; - "Skynet Team" - br; - a href=(discord) { (discord) } - } + } + div style="display: flex; flex-direction: column; align-items: center;" { + h2 { "Hello from Skynet!" } + // Substitute in the name of our recipient. + p { "Hi there," } + p { + "You requested a username reminder: " (recipient) } + p { + "If did not request this please ignore." + } + p { + "UL Computer Society" + br; + "Skynet Team" + br; + a href=(discord) { (discord) } + } + } }; let body_text = format!( r#" - Hi there, - - You requested a username reminder: {recipient} - - If did not request this please ignore. - - UL Computer Society - Skynet Team - {discord} - "# + Hi there, + + You requested a username reminder: {recipient} + + If did not request this please ignore. + + UL Computer Society + Skynet Team + {discord} + "# ); // Build the message. @@ -485,10 +485,10 @@ pub mod ssh { // check if there is ane listing entry, use that auth if exists if let Ok(result) = sqlx::query_as::<_, AccountsSSH>( r#" - SELECT * - FROM accounts_ssh - WHERE user == ? - "#, + SELECT * + FROM accounts_ssh + WHERE user == ? + "#, ) .bind(&user) .fetch_one(db) @@ -501,9 +501,9 @@ pub mod ssh { let auth = random_string(50); if sqlx::query_as::<_, AccountsSSH>( " - INSERT OR REPLACE INTO accounts_ssh (user, auth_code, email) - VALUES (?1, ?2, ?3) - ", + INSERT OR REPLACE INTO accounts_ssh (user, auth_code, email) + VALUES (?1, ?2, ?3) + ", ) .bind(&user) .bind(&auth) @@ -537,10 +537,10 @@ pub mod ssh { let config = &req.state().config; let details = if let Ok(result) = sqlx::query_as::<_, AccountsSSH>( r#" - SELECT * - FROM accounts_ssh - WHERE user == ? - "#, + SELECT * + FROM accounts_ssh + WHERE user == ? + "#, ) .bind(&user) .fetch_one(db) @@ -596,9 +596,9 @@ pub mod ssh { // delete from tmp sqlx::query_as::<_, AccountsSSH>( r#" - DELETE FROM accounts_ssh - WHERE user == ? - "#, + DELETE FROM accounts_ssh + WHERE user == ? + "#, ) .bind(&user) .fetch_optional(db) diff --git a/src/methods/discord.rs b/src/methods/discord.rs deleted file mode 100644 index 581ff87..0000000 --- a/src/methods/discord.rs +++ /dev/null @@ -1,73 +0,0 @@ -use crate::{Accounts, State}; -use sqlx::{Pool, Sqlite}; -use tide::{ - prelude::{json, Deserialize}, - Request, -}; - -pub mod account { - use super::*; - use crate::methods::account_new::email::get_wolves_mail; - use serde::Serialize; - - #[derive(Debug, Deserialize)] - struct Auth { - auth: String, - } - pub async fn get(req: Request) -> tide::Result { - let config = &req.state().config; - - if let Ok(auth) = req.query::() { - if auth.auth != config.auth_discord { - return Ok(json!([]).into()); - } - } else { - return Ok(json!([]).into()); - }; - - let db = &req.state().db; - - let result = get_discord_users(db).await; - - Ok(json!(result).into()) - } - - #[derive(Debug, Deserialize, Serialize)] - pub struct DiscordResult { - discord: String, - id_wolves: String, - id_member: String, - } - - pub async fn get_discord_users(db: &Pool) -> Vec { - let results = sqlx::query_as::<_, Accounts>( - r#" - SELECT * - FROM accounts - WHERE discord IS NOT NULL AND enabled = 1 - "#, - ) - .fetch_all(db) - .await - .unwrap_or(vec![]); - - let mut result = vec![]; - - for item in results { - if let Some(discord) = item.discord { - let accounts = get_wolves_mail(db, &item.mail).await; - if !accounts.is_empty() { - let tmp = DiscordResult { - discord, - id_wolves: accounts[0].id_wolves.to_owned(), - id_member: accounts[0].id_member.to_owned(), - }; - - result.push(tmp); - } - } - } - - result - } -} diff --git a/src/methods/mod.rs b/src/methods/mod.rs index 2f22b18..8273b7b 100644 --- a/src/methods/mod.rs +++ b/src/methods/mod.rs @@ -1,4 +1,3 @@ pub mod account_new; pub mod account_recover; pub mod account_update; -pub mod discord;