feat: now check local db for pending usernames

This commit is contained in:
silver 2023-06-04 14:21:12 +01:00
parent 894b6d42e5
commit e83adea4fe
2 changed files with 54 additions and 12 deletions

View file

@ -1,9 +1,10 @@
use crate::State;
use crate::{AccountsPending, get_now, State};
use ldap3::exop::PasswordModify;
use ldap3::{LdapConn, Mod, Scope, SearchEntry};
use std::collections::HashSet;
use tide::prelude::{json, Deserialize};
use tide::Request;
use sqlx::{Pool, Sqlite};
#[derive(Debug, Deserialize)]
pub struct LdapNewUser {
@ -56,11 +57,40 @@ pub async fn post_new_account(mut req: Request<State>) -> tide::Result {
// done with ldap
ldap.unbind()?;
// setup the pool, going to need it for the rest of it
let pool = &req.state().db;
db_pending_clear_expired(pool).await;
// now check local
if let Ok(results) = sqlx::query_as::<_, AccountsPending>(
r#"
SELECT *
FROM accounts_pending
WHERE user == ?
"#,
).bind(user).fetch_all(pool).await {
if !results.is_empty(){
return Ok(json!({"result": "error", "error": "username not available"}).into())
}
}
Ok(json!({"result": "success"}).into())
}
// clear the db of expired ones before checking for username and validating inputs
async fn db_pending_clear_expired(pool: &Pool<Sqlite>){
let now = get_now();
if let Ok(results) = sqlx::query_as::<_, AccountsPending>(
r#"
DELETE
FROM accounts_pending
WHERE expiry < ?
"#,
).bind(now).fetch_all(pool).await {
println!("{:?}", results)
}
}