fix: use modules to better organise teh code
This commit is contained in:
parent
263a5b76e1
commit
158292be05
2 changed files with 186 additions and 178 deletions
|
@ -1,6 +1,9 @@
|
|||
use skynet_ldap_backend::{
|
||||
db_init, get_config,
|
||||
methods::{account_new::post_new_account, account_update::post_update_ldap},
|
||||
methods::{
|
||||
account_new::post::{account, email},
|
||||
account_update::post_update_ldap,
|
||||
},
|
||||
State,
|
||||
};
|
||||
|
||||
|
@ -21,7 +24,8 @@ async fn main() -> tide::Result<()> {
|
|||
let mut app = tide::with_state(state);
|
||||
|
||||
app.at("/ldap/update").post(post_update_ldap);
|
||||
app.at("/ldap/new").post(post_new_account);
|
||||
app.at("/ldap/new/email").post(email::submit);
|
||||
app.at("/ldap/new/account").post(account::submit);
|
||||
|
||||
app.listen(host_port).await?;
|
||||
Ok(())
|
||||
|
|
|
@ -232,19 +232,21 @@ pub mod post {
|
|||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct LdapNewUser {
|
||||
pub mod account {
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct LdapNewUser {
|
||||
auth: String,
|
||||
user: String,
|
||||
pass: String,
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles initial detail entering page
|
||||
/// Verify users have access to said email
|
||||
/// Get users to set username and password.
|
||||
pub async fn post_new_account(mut req: Request<State>) -> tide::Result {
|
||||
/// Handles initial detail entering page
|
||||
/// Verify users have access to said email
|
||||
/// Get users to set username and password.
|
||||
pub async fn submit(mut req: Request<State>) -> tide::Result {
|
||||
let LdapNewUser {
|
||||
auth,
|
||||
user,
|
||||
|
@ -291,10 +293,10 @@ pub async fn post_new_account(mut req: Request<State>) -> tide::Result {
|
|||
account_verification_clear_pending(db, &auth).await?;
|
||||
|
||||
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>) -> Result<Vec<AccountsNew>, Error> {
|
||||
// clear the db of expired ones before checking for username and validating inputs
|
||||
async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Result<Vec<AccountsNew>, Error> {
|
||||
sqlx::query_as::<_, AccountsNew>(
|
||||
r#"
|
||||
DELETE
|
||||
|
@ -305,9 +307,9 @@ async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Result<Vec<AccountsNew
|
|||
.bind(get_now_iso(true))
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
fn is_valid_name(name: &str) -> Option<String> {
|
||||
fn is_valid_name(name: &str) -> Option<String> {
|
||||
// max length is 31 chars
|
||||
if name.len() >= 32 {
|
||||
return Some(String::from("Too long, max len 31"));
|
||||
|
@ -333,9 +335,9 @@ fn is_valid_name(name: &str) -> Option<String> {
|
|||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
async fn db_get_user(pool: &Pool<Sqlite>, auth: &str) -> Option<AccountsNew> {
|
||||
async fn db_get_user(pool: &Pool<Sqlite>, auth: &str) -> Option<AccountsNew> {
|
||||
if let Ok(res) = sqlx::query_as::<_, AccountsNew>(
|
||||
r#"
|
||||
SELECT *
|
||||
|
@ -353,9 +355,9 @@ async fn db_get_user(pool: &Pool<Sqlite>, auth: &str) -> Option<AccountsNew> {
|
|||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
async fn ldap_create_account(config: &Config, db: &Pool<Sqlite>, user: AccountsNew, username: &str, pass: &str) -> Result<(), ldap3::LdapError> {
|
||||
async fn ldap_create_account(config: &Config, db: &Pool<Sqlite>, user: AccountsNew, username: &str, pass: &str) -> Result<(), ldap3::LdapError> {
|
||||
let mut ldap = LdapConn::new(&config.ldap_host)?;
|
||||
ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw)?.success()?;
|
||||
|
||||
|
@ -412,16 +414,16 @@ async fn ldap_create_account(config: &Config, db: &Pool<Sqlite>, user: AccountsN
|
|||
ldap.unbind()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn get_sk_created() -> String {
|
||||
fn get_sk_created() -> String {
|
||||
use chrono::Utc;
|
||||
let now = Utc::now();
|
||||
|
||||
format!("{}", now.format("%Y%m%d%H%M%SZ"))
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_max_uid_number(db: &Pool<Sqlite>) -> i64 {
|
||||
async fn get_max_uid_number(db: &Pool<Sqlite>) -> i64 {
|
||||
if let Ok(results) = sqlx::query_as::<_, Accounts>(
|
||||
r#"
|
||||
SELECT *
|
||||
|
@ -439,9 +441,9 @@ async fn get_max_uid_number(db: &Pool<Sqlite>) -> i64 {
|
|||
}
|
||||
|
||||
9999
|
||||
}
|
||||
}
|
||||
|
||||
async fn account_verification_clear_pending(db: &Pool<Sqlite>, auth_code: &str) -> Result<Vec<AccountsNew>, Error> {
|
||||
async fn account_verification_clear_pending(db: &Pool<Sqlite>, auth_code: &str) -> Result<Vec<AccountsNew>, Error> {
|
||||
sqlx::query_as::<_, AccountsNew>(
|
||||
r#"
|
||||
DELETE FROM accounts_new
|
||||
|
@ -451,4 +453,6 @@ async fn account_verification_clear_pending(db: &Pool<Sqlite>, auth_code: &str)
|
|||
.bind(auth_code)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue