fmt: fmt and clippy

This commit is contained in:
silver 2023-06-04 22:06:34 +01:00
parent f7ac2fa951
commit 2e3578bae7
4 changed files with 151 additions and 130 deletions

View file

@ -1,22 +1,22 @@
pub mod methods;
use dotenv::dotenv;
use ldap3::{LdapConn, Scope, SearchEntry};
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::{Error, Pool, Sqlite};
use std::env;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
use ldap3::{LdapConn, Scope, SearchEntry};
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,
name_first: String,
name_second: String,
auth_code: String,
// will only last for a few hours
expiry: i64
expiry: i64,
}
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
@ -24,7 +24,7 @@ pub struct Accounts {
user: String,
uid_number: i64,
discord: Option<String>,
enabled: bool
enabled: bool,
}
pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
@ -55,12 +55,16 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
discord text,
enabled integer not null
)",
).execute(&pool).await?;
)
.execute(&pool)
.await?;
sqlx::query("CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid_number)").execute(&pool).await?;
sqlx::query("CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid_number)")
.execute(&pool)
.await?;
update_accounts(&pool, config).await;
Ok(pool)
}
@ -118,16 +122,13 @@ pub fn get_config() -> Config {
config
}
async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
let mut ldap = LdapConn::new(&config.ldap_host).unwrap();
ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw).unwrap().success().unwrap();
if let Ok(x) = ldap.search("ou=users,dc=skynet,dc=ie", Scope::OneLevel, "(objectClass=*)", vec!["uid", "uidNumber", "skDiscord", "skMemberOf"]) {
if let Ok((rs, _res)) = x.success() {
for entry in rs {
let tmp = SearchEntry::construct(entry);
@ -137,7 +138,7 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
discord: None,
enabled: false,
};
// pull out the required info
if tmp.attrs.contains_key("uid") && !tmp.attrs["uid"].is_empty() {
tmp_account.user = tmp.attrs["uid"][0].clone();
@ -148,27 +149,26 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
if tmp.attrs.contains_key("skDiscord") && !tmp.attrs["skDiscord"].is_empty() {
tmp_account.user = tmp.attrs["skDiscord"][0].clone();
}
if tmp.attrs.contains_key("skMemberOf") && !tmp.attrs["skMemberOf"].is_empty() && tmp.attrs["skMemberOf"].contains(&String::from("cn=skynet-users,ou=groups,dc=skynet,dc=ie")){
if tmp.attrs.contains_key("skMemberOf") && !tmp.attrs["skMemberOf"].is_empty() && tmp.attrs["skMemberOf"].contains(&String::from("cn=skynet-users,ou=groups,dc=skynet,dc=ie")) {
tmp_account.enabled = true;
}
if tmp_account.user.len() > 0 {
if !tmp_account.user.is_empty() {
sqlx::query_as::<_, Accounts>(
"
"
INSERT OR REPLACE INTO accounts (user, uid_number, discord, enabled)
VALUES (?1, ?2, ?3, ?4)
",
)
.bind(&tmp_account.user)
.bind(&tmp_account.uid_number)
.bind(&tmp_account.discord)
.bind(&tmp_account.enabled)
.fetch_optional(pool)
.await
.ok();
.bind(&tmp_account.user)
.bind(tmp_account.uid_number)
.bind(&tmp_account.discord)
.bind(tmp_account.enabled)
.fetch_optional(pool)
.await
.ok();
}
}
}
}