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

@ -5,6 +5,19 @@ use sqlx::{Error, Pool, Sqlite};
use std::env;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
use tide::prelude::*;
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
pub struct AccountsPending {
user: String,
mail: String,
name_first : String,
name_second : String,
auth_code : String,
discord: Option<String>,
// will only last for a few hours
expiry: i64
}
pub async fn db_init(database: &str) -> Result<Pool<Sqlite>, Error> {
let pool = SqlitePoolOptions::new()
@ -12,21 +25,20 @@ pub async fn db_init(database: &str) -> Result<Pool<Sqlite>, Error> {
.connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true))
.await?;
/*
// https://store.steampowered.com/api/appdetails?appids=1258740
sqlx::query(
"CREATE TABLE IF NOT EXISTS store_details (
id integer primary key,
name text not null,
item_type text not null,
last_timestamp integer not null
"CREATE TABLE IF NOT EXISTS accounts_pending (
user text primary key,
mail text not null,
name_first text not null,
name_second text not null,
auth_code text not null,
discord text,
expiry integer not null
)",
)
.execute(&pool)
.await?;
*/
// set up indexes?
/*
sqlx::query("CREATE INDEX IF NOT EXISTS index_estimate ON bus_results (valid_estimate)")

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)
}
}