fix: should allow folks to register
This commit is contained in:
parent
f60345493c
commit
9db8a238d2
7 changed files with 136 additions and 217 deletions
|
@ -135,8 +135,7 @@ struct RecordCSV {
|
|||
impl From<RecordCSV> for AccountWolves {
|
||||
fn from(input: RecordCSV) -> Self {
|
||||
AccountWolves {
|
||||
id_wolves: "".to_string(),
|
||||
id_member: input.mem_id,
|
||||
id_wolves: input.mem_id,
|
||||
id_student: if input.id_student.is_empty() { None } else { Some(input.id_student) },
|
||||
email: input.email,
|
||||
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) {
|
||||
sqlx::query_as::<_, AccountWolves>(
|
||||
"
|
||||
INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_member, id_student, email, expiry, name_first, name_second)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
|
||||
INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_student, email, expiry, name_first, name_second)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6)
|
||||
",
|
||||
)
|
||||
.bind(&account.id_wolves)
|
||||
.bind(&account.id_member)
|
||||
.bind(&account.id_student)
|
||||
.bind(&account.email)
|
||||
.bind(&account.expiry)
|
||||
|
|
50
src/lib.rs
50
src/lib.rs
|
@ -1,4 +1,5 @@
|
|||
pub mod methods;
|
||||
|
||||
use chrono::{Datelike, SecondsFormat, Utc};
|
||||
use dotenvy::dotenv;
|
||||
use ldap3::{LdapConn, Mod};
|
||||
|
@ -17,7 +18,6 @@ use tide::prelude::*;
|
|||
#[derive(Debug, Deserialize, Serialize, sqlx::FromRow)]
|
||||
pub struct AccountWolves {
|
||||
pub id_wolves: String,
|
||||
pub id_member: String,
|
||||
pub id_student: Option<String>,
|
||||
pub email: String,
|
||||
pub expiry: String,
|
||||
|
@ -71,14 +71,12 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
|||
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS accounts_wolves (
|
||||
id_wolves text DEFAULT '',
|
||||
id_member text DEFAULT '',
|
||||
id_wolves text PRIMARY KEY,
|
||||
id_student text,
|
||||
email text not null,
|
||||
expiry text not null,
|
||||
email text NOT NULL,
|
||||
expiry text NOT NULL,
|
||||
name_first text,
|
||||
name_second text,
|
||||
PRIMARY KEY (id_wolves, id_member)
|
||||
name_second text
|
||||
)",
|
||||
)
|
||||
.execute(&pool)
|
||||
|
@ -86,13 +84,13 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
|||
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS accounts_new (
|
||||
mail text primary key,
|
||||
auth_code text not null,
|
||||
date_iso text not null,
|
||||
date_expiry text not null,
|
||||
name_first text not null,
|
||||
name_surname integer not null,
|
||||
id_student text not null
|
||||
mail text PRIMARY KEY,
|
||||
auth_code text NOT NULL,
|
||||
date_iso text NOT NULL,
|
||||
date_expiry text NOT NULL,
|
||||
name_first text NOT NULL,
|
||||
name_surname integer NOT NULL,
|
||||
id_student text NOT NULL
|
||||
)",
|
||||
)
|
||||
.execute(&pool)
|
||||
|
@ -107,9 +105,9 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
|||
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS accounts_ssh (
|
||||
user text primary key,
|
||||
auth_code text not null,
|
||||
email text not null
|
||||
user text PRIMARY KEY,
|
||||
auth_code text NOT NULL,
|
||||
email text NOT NULL
|
||||
)",
|
||||
)
|
||||
.execute(&pool)
|
||||
|
@ -117,9 +115,9 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
|||
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS accounts_reset (
|
||||
user text primary key,
|
||||
auth_code text not null,
|
||||
date_expiry text not null
|
||||
user text PRIMARY KEY,
|
||||
auth_code text NOT NULL,
|
||||
date_expiry text NOT NULL
|
||||
)",
|
||||
)
|
||||
.execute(&pool)
|
||||
|
@ -135,13 +133,13 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
|||
// this is for active use
|
||||
sqlx::query(
|
||||
"CREATE TABLE IF NOT EXISTS accounts (
|
||||
user text primary key,
|
||||
uid integer not null,
|
||||
user text PRIMARY KEY,
|
||||
uid integer NOT NULL,
|
||||
discord text,
|
||||
mail text not null,
|
||||
student_id text not null,
|
||||
enabled integer not null,
|
||||
secure integer not null
|
||||
mail text NOT NULL,
|
||||
student_id text NOT NULL,
|
||||
enabled integer NOT NULL,
|
||||
secure integer NOT NULL
|
||||
)",
|
||||
)
|
||||
.execute(&pool)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use skynet_ldap_backend::{
|
||||
db_init, get_config,
|
||||
methods::{account_new, account_recover, account_update, discord},
|
||||
methods::{account_new, account_recover, account_update},
|
||||
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/verify").post(account_recover::ssh::verify);
|
||||
|
||||
// for discord
|
||||
app.at("/ldap/discord").get(discord::account::get);
|
||||
|
||||
app.listen(host_port).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -1,4 +1,3 @@
|
|||
pub mod account_new;
|
||||
pub mod account_recover;
|
||||
pub mod account_update;
|
||||
pub mod discord;
|
||||
|
|
Loading…
Reference in a new issue