diff --git a/src/methods/account_new.rs b/src/methods/account_new.rs index 94473c3..eec2290 100644 --- a/src/methods/account_new.rs +++ b/src/methods/account_new.rs @@ -1,4 +1,4 @@ -use crate::{AccountsPending, get_now, State}; +use crate::{Accounts, AccountsPending, get_now, State}; use ldap3::exop::PasswordModify; use ldap3::{LdapConn, Mod, Scope, SearchEntry}; use std::collections::HashSet; @@ -178,7 +178,7 @@ pub async fn post_new_account_confirmation(mut req: Request) -> tide::Res } = &req.state(); // make sure to clear out the expired ones first - db_pending_clear_expired(db).await; + //db_pending_clear_expired(db).await; // search db for auth_code let results = sqlx::query_as::<_, AccountsPending>( @@ -200,8 +200,7 @@ pub async fn post_new_account_confirmation(mut req: Request) -> tide::Res let AccountsPending{ user, mail, name_first, name_second, auth_code, discord, expiry } = &results[0]; let dn = format!("uid={},ou=users,dc=skynet,dc=ie", user); - // TODO: find highrest uid number - let uid_number = "9990"; + let uid_number = get_max_uid_number(db).await.to_string(); let home_directory = format!("/home/{}", user); let password_tmp = create_random_string(50); let cn = format!("{} {}", name_first, name_second); @@ -221,7 +220,7 @@ pub async fn post_new_account_confirmation(mut req: Request) -> tide::Res ("cn", HashSet::from([cn.as_str()])), // posixaccount - ("uidNumber", HashSet::from([uid_number])), + ("uidNumber", HashSet::from([uid_number.as_str()])), ("gidNumber", HashSet::from(["1001"])), ("homedirectory", HashSet::from([home_directory.as_str()])), ("userpassword", HashSet::from([password_tmp.as_str()])), @@ -268,4 +267,21 @@ fn get_sk_created() -> String { let now = Utc::now(); format!("{}", now.format("%Y%m%d%H%M%SZ")) +} + +pub async fn get_max_uid_number(db: &Pool) -> i64 { + if let Ok(results) = sqlx::query_as::<_, Accounts>( + r#" + SELECT * + FROM accounts + ORDER BY uid_number DESC + LIMIT 1 + "#, + ).fetch_all(db).await { + if !results.is_empty() { + return results[0].uid_number + 1; + } + } + + 9999 } \ No newline at end of file