feat: added a route to return discord usernames.

Closes #19
This commit is contained in:
silver 2023-08-27 20:33:43 +01:00
parent ce471ad39a
commit 92fad0dc4b
8 changed files with 352 additions and 122 deletions

View file

@ -1,4 +1,4 @@
use crate::{methods::account_new::email::get_wolves_mail, update_group, Config, State};
use crate::{methods::account_new::email::get_wolves_mail, update_group, Accounts, Config, State};
use ldap3::{exop::PasswordModify, LdapConn, Mod, Scope, SearchEntry};
use sqlx::{Pool, Sqlite};
use std::collections::{HashMap, HashSet};
@ -77,7 +77,7 @@ pub async fn submit(mut req: Request<State>) -> tide::Result {
// if password is not being updated then just update the required field
let mods = vec![
// the value we are updating
Mod::Replace(field, HashSet::from([value])),
Mod::Replace(field.clone(), HashSet::from([value.clone()])),
];
ldap.modify(&dn, mods)?.success()?;
@ -88,7 +88,7 @@ pub async fn submit(mut req: Request<State>) -> tide::Result {
} else {
// password is going to be updated, even if the old value is not starting with "{SSHA512}"
pw_keep_same = false;
value
value.clone()
};
// changing teh password because of an explicit request or upgrading teh security.
@ -108,6 +108,14 @@ pub async fn submit(mut req: Request<State>) -> tide::Result {
ldap.unbind()?;
// if its mail or discord update the local db
if &field == "skDiscord" {
update_local_db(db, "discord", &value).await.ok();
}
if &field == "mail" {
update_local_db(db, "mail", &value).await.ok();
}
Ok(json!({"result": "success", "success": result}).into())
}
@ -152,3 +160,8 @@ async fn activate_group(db: &Pool<Sqlite>, config: &Config, user: &str, mail: &s
}
}
}
async fn update_local_db(db: &Pool<Sqlite>, field: &str, value: &str) -> Result<Option<Accounts>, sqlx::Error> {
let query = format!("INSERT OR REPLACE INTO accounts ({field}) VALUES (?1)");
sqlx::query_as::<_, Accounts>(&query).bind(value.to_owned()).fetch_optional(db).await
}

55
src/methods/discord.rs Normal file
View file

@ -0,0 +1,55 @@
use crate::{Accounts, State};
use sqlx::{Pool, Sqlite};
use tide::{
prelude::{json, Deserialize},
Request,
};
pub mod account {
use super::*;
#[derive(Debug, Deserialize)]
struct Auth {
auth: String,
}
pub async fn get(req: Request<State>) -> tide::Result {
let config = &req.state().config;
if let Ok(auth) = req.query::<Auth>() {
if auth.auth != config.auth_discord {
return Ok(json!([]).into());
}
} else {
return Ok(json!([]).into());
};
let db = &req.state().db;
let result = get_wolves_mail(db).await;
Ok(json!(result).into())
}
pub async fn get_wolves_mail(db: &Pool<Sqlite>) -> Vec<String> {
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 {
result.push(discord);
}
}
result
}
}

View file

@ -1,3 +1,4 @@
pub mod account_new;
pub mod account_recover;
pub mod account_update;
pub mod discord;