feat: now check local db for pending usernames
This commit is contained in:
parent
894b6d42e5
commit
e83adea4fe
2 changed files with 54 additions and 12 deletions
30
src/lib.rs
30
src/lib.rs
|
@ -5,6 +5,19 @@ use sqlx::{Error, Pool, Sqlite};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
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> {
|
pub async fn db_init(database: &str) -> Result<Pool<Sqlite>, Error> {
|
||||||
let pool = SqlitePoolOptions::new()
|
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))
|
.connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
/*
|
|
||||||
// https://store.steampowered.com/api/appdetails?appids=1258740
|
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"CREATE TABLE IF NOT EXISTS store_details (
|
"CREATE TABLE IF NOT EXISTS accounts_pending (
|
||||||
id integer primary key,
|
user text primary key,
|
||||||
name text not null,
|
mail text not null,
|
||||||
item_type text not null,
|
name_first text not null,
|
||||||
last_timestamp integer not null
|
name_second text not null,
|
||||||
|
auth_code text not null,
|
||||||
|
discord text,
|
||||||
|
expiry integer not null
|
||||||
)",
|
)",
|
||||||
)
|
)
|
||||||
.execute(&pool)
|
.execute(&pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
// set up indexes?
|
// set up indexes?
|
||||||
/*
|
/*
|
||||||
sqlx::query("CREATE INDEX IF NOT EXISTS index_estimate ON bus_results (valid_estimate)")
|
sqlx::query("CREATE INDEX IF NOT EXISTS index_estimate ON bus_results (valid_estimate)")
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
use crate::State;
|
use crate::{AccountsPending, get_now, State};
|
||||||
use ldap3::exop::PasswordModify;
|
use ldap3::exop::PasswordModify;
|
||||||
use ldap3::{LdapConn, Mod, Scope, SearchEntry};
|
use ldap3::{LdapConn, Mod, Scope, SearchEntry};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use tide::prelude::{json, Deserialize};
|
use tide::prelude::{json, Deserialize};
|
||||||
use tide::Request;
|
use tide::Request;
|
||||||
|
use sqlx::{Pool, Sqlite};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct LdapNewUser {
|
pub struct LdapNewUser {
|
||||||
|
@ -57,10 +58,39 @@ pub async fn post_new_account(mut req: Request<State>) -> tide::Result {
|
||||||
// done with ldap
|
// done with ldap
|
||||||
ldap.unbind()?;
|
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
|
// 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())
|
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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue