fix: should allow folks to register

This commit is contained in:
silver 2023-09-27 23:47:37 +01:00
parent f60345493c
commit 9db8a238d2
7 changed files with 136 additions and 217 deletions

View file

@ -135,8 +135,7 @@ struct RecordCSV {
impl From<RecordCSV> for AccountWolves { impl From<RecordCSV> for AccountWolves {
fn from(input: RecordCSV) -> Self { fn from(input: RecordCSV) -> Self {
AccountWolves { AccountWolves {
id_wolves: "".to_string(), id_wolves: input.mem_id,
id_member: input.mem_id,
id_student: if input.id_student.is_empty() { None } else { Some(input.id_student) }, id_student: if input.id_student.is_empty() { None } else { Some(input.id_student) },
email: input.email, email: input.email,
expiry: input.expiry, expiry: input.expiry,
@ -169,12 +168,11 @@ fn get_csv(config: &Config) -> Result<Vec<RecordCSV>, Box<dyn std::error::Error>
async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves) { async fn update_account(db: &Pool<Sqlite>, account: &AccountWolves) {
sqlx::query_as::<_, AccountWolves>( sqlx::query_as::<_, AccountWolves>(
" "
INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_member, id_student, email, expiry, name_first, name_second) INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_student, email, expiry, name_first, name_second)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7) VALUES (?1, ?2, ?3, ?4, ?5, ?6)
", ",
) )
.bind(&account.id_wolves) .bind(&account.id_wolves)
.bind(&account.id_member)
.bind(&account.id_student) .bind(&account.id_student)
.bind(&account.email) .bind(&account.email)
.bind(&account.expiry) .bind(&account.expiry)

View file

@ -1,4 +1,5 @@
pub mod methods; pub mod methods;
use chrono::{Datelike, SecondsFormat, Utc}; use chrono::{Datelike, SecondsFormat, Utc};
use dotenvy::dotenv; use dotenvy::dotenv;
use ldap3::{LdapConn, Mod}; use ldap3::{LdapConn, Mod};
@ -17,7 +18,6 @@ use tide::prelude::*;
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)] #[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
pub struct AccountWolves { pub struct AccountWolves {
pub id_wolves: String, pub id_wolves: String,
pub id_member: String,
pub id_student: Option<String>, pub id_student: Option<String>,
pub email: String, pub email: String,
pub expiry: String, pub expiry: String,
@ -71,14 +71,12 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_wolves ( "CREATE TABLE IF NOT EXISTS accounts_wolves (
id_wolves text DEFAULT '', id_wolves text PRIMARY KEY,
id_member text DEFAULT '',
id_student text, id_student text,
email text not null, email text NOT NULL,
expiry text not null, expiry text NOT NULL,
name_first text, name_first text,
name_second text, name_second text
PRIMARY KEY (id_wolves, id_member)
)", )",
) )
.execute(&pool) .execute(&pool)
@ -86,13 +84,13 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_new ( "CREATE TABLE IF NOT EXISTS accounts_new (
mail text primary key, mail text PRIMARY KEY,
auth_code text not null, auth_code text NOT NULL,
date_iso text not null, date_iso text NOT NULL,
date_expiry text not null, date_expiry text NOT NULL,
name_first text not null, name_first text NOT NULL,
name_surname integer not null, name_surname integer NOT NULL,
id_student text not null id_student text NOT NULL
)", )",
) )
.execute(&pool) .execute(&pool)
@ -107,9 +105,9 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_ssh ( "CREATE TABLE IF NOT EXISTS accounts_ssh (
user text primary key, user text PRIMARY KEY,
auth_code text not null, auth_code text NOT NULL,
email text not null email text NOT NULL
)", )",
) )
.execute(&pool) .execute(&pool)
@ -117,9 +115,9 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts_reset ( "CREATE TABLE IF NOT EXISTS accounts_reset (
user text primary key, user text PRIMARY KEY,
auth_code text not null, auth_code text NOT NULL,
date_expiry text not null date_expiry text NOT NULL
)", )",
) )
.execute(&pool) .execute(&pool)
@ -135,13 +133,13 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
// this is for active use // this is for active use
sqlx::query( sqlx::query(
"CREATE TABLE IF NOT EXISTS accounts ( "CREATE TABLE IF NOT EXISTS accounts (
user text primary key, user text PRIMARY KEY,
uid integer not null, uid integer NOT NULL,
discord text, discord text,
mail text not null, mail text NOT NULL,
student_id text not null, student_id text NOT NULL,
enabled integer not null, enabled integer NOT NULL,
secure integer not null secure integer NOT NULL
)", )",
) )
.execute(&pool) .execute(&pool)

View file

@ -1,6 +1,6 @@
use skynet_ldap_backend::{ use skynet_ldap_backend::{
db_init, get_config, db_init, get_config,
methods::{account_new, account_recover, account_update, discord}, methods::{account_new, account_recover, account_update},
State, State,
}; };
@ -34,9 +34,6 @@ async fn main() -> tide::Result<()> {
app.at("/ldap/recover/ssh/request").post(account_recover::ssh::request); app.at("/ldap/recover/ssh/request").post(account_recover::ssh::request);
app.at("/ldap/recover/ssh/verify").post(account_recover::ssh::verify); app.at("/ldap/recover/ssh/verify").post(account_recover::ssh::verify);
// for discord
app.at("/ldap/discord").get(discord::account::get);
app.listen(host_port).await?; app.listen(host_port).await?;
Ok(()) Ok(())
} }

View file

@ -1,73 +0,0 @@
use crate::{Accounts, State};
use sqlx::{Pool, Sqlite};
use tide::{
prelude::{json, Deserialize},
Request,
};
pub mod account {
use super::*;
use crate::methods::account_new::email::get_wolves_mail;
use serde::Serialize;
#[derive(Debug, Deserialize)]
struct Auth {
auth: String,
}
pub async fn get(req: Request<State>) -> tide::Result {
let config = &req.state().config;
if let Ok(auth) = req.query::<Auth>() {
if auth.auth != config.auth_discord {
return Ok(json!([]).into());
}
} else {
return Ok(json!([]).into());
};
let db = &req.state().db;
let result = get_discord_users(db).await;
Ok(json!(result).into())
}
#[derive(Debug, Deserialize, Serialize)]
pub struct DiscordResult {
discord: String,
id_wolves: String,
id_member: String,
}
pub async fn get_discord_users(db: &Pool<Sqlite>) -> Vec<DiscordResult> {
let results = sqlx::query_as::<_, Accounts>(
r#"
SELECT *
FROM accounts
WHERE discord IS NOT NULL AND enabled = 1
"#,
)
.fetch_all(db)
.await
.unwrap_or(vec![]);
let mut result = vec![];
for item in results {
if let Some(discord) = item.discord {
let accounts = get_wolves_mail(db, &item.mail).await;
if !accounts.is_empty() {
let tmp = DiscordResult {
discord,
id_wolves: accounts[0].id_wolves.to_owned(),
id_member: accounts[0].id_member.to_owned(),
};
result.push(tmp);
}
}
}
result
}
}

View file

@ -1,4 +1,3 @@
pub mod account_new; pub mod account_new;
pub mod account_recover; pub mod account_recover;
pub mod account_update; pub mod account_update;
pub mod discord;