mirror of
https://git.42l.fr/neil/sncf.git
synced 2024-05-05 23:43:10 +02:00
implementing multiple database systems support!
This commit is contained in:
parent
cf26c8bd44
commit
8dd5486250
|
@ -4,11 +4,17 @@ version = "1.0.0"
|
||||||
authors = ["Association 42l <contact@noreply.example.org>"]
|
authors = ["Association 42l <contact@noreply.example.org>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = [ "diesel/postgres" ]
|
||||||
|
postgres = [ "diesel/postgres" ]
|
||||||
|
mysql = [ "diesel/mysql" ]
|
||||||
|
sqlite = [ "diesel/sqlite" ]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-rt = "1.0.0"
|
actix-rt = "1.0.0"
|
||||||
actix-web = "3.0.2"
|
actix-web = "3.0.2"
|
||||||
actix-files = "0.3.0"
|
actix-files = "0.3.0"
|
||||||
diesel = { version = "1.4", features = ["sqlite", "r2d2", "chrono"] }
|
diesel = { version = "1.4", features = ["r2d2", "chrono"] }
|
||||||
diesel_migrations = "1.4"
|
diesel_migrations = "1.4"
|
||||||
url = "2.1"
|
url = "2.1"
|
||||||
toml = "0.5"
|
toml = "0.5"
|
||||||
|
|
|
@ -6,7 +6,9 @@ listening_port = 8000
|
||||||
# includes protocol, FQDN and port, without the trailing slash.
|
# includes protocol, FQDN and port, without the trailing slash.
|
||||||
sncf_url = "http://localhost:8000"
|
sncf_url = "http://localhost:8000"
|
||||||
|
|
||||||
# path to the SQLite DB
|
# SQLite: path to the SQLite DB
|
||||||
|
# PostgreSQL: postgres://user:password@address:port/database
|
||||||
|
# MySQL: mysql://user:password@address:port/database
|
||||||
database_path = "./db/sncf.sqlite"
|
database_path = "./db/sncf.sqlite"
|
||||||
|
|
||||||
# IP address of the Nextcloud instance, including protocol and port
|
# IP address of the Nextcloud instance, including protocol and port
|
||||||
|
|
8
migrations/mysql/20200809180000_create_form/up.sql
Normal file
8
migrations/mysql/20200809180000_create_form/up.sql
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
CREATE TABLE form (
|
||||||
|
id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT UNIQUE,
|
||||||
|
created_at TIMESTAMP NOT NULL,
|
||||||
|
lastvisit_at TIMESTAMP NOT NULL,
|
||||||
|
token VARCHAR(128) NOT NULL UNIQUE,
|
||||||
|
nc_username VARCHAR(128) NOT NULL UNIQUE,
|
||||||
|
nc_password VARCHAR(128) NOT NULL
|
||||||
|
);
|
1
migrations/postgres/20200809180000_create_form/down.sql
Normal file
1
migrations/postgres/20200809180000_create_form/down.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
DELETE TABLE form;
|
8
migrations/postgres/20200809180000_create_form/up.sql
Normal file
8
migrations/postgres/20200809180000_create_form/up.sql
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
CREATE TABLE form (
|
||||||
|
id serial4 PRIMARY KEY UNIQUE NOT NULL,
|
||||||
|
created_at TIMESTAMP NOT NULL,
|
||||||
|
lastvisit_at TIMESTAMP NOT NULL,
|
||||||
|
token VARCHAR UNIQUE NOT NULL,
|
||||||
|
nc_username VARCHAR UNIQUE NOT NULL,
|
||||||
|
nc_password VARCHAR NOT NULL
|
||||||
|
);
|
1
migrations/sqlite/20200809180000_create_form/down.sql
Normal file
1
migrations/sqlite/20200809180000_create_form/down.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
DELETE TABLE form;
|
|
@ -264,7 +264,12 @@ pub fn gen_name() -> String {
|
||||||
// uppercasing gen_token because NC would probably refuse two
|
// uppercasing gen_token because NC would probably refuse two
|
||||||
// users with the same name but a different case
|
// users with the same name but a different case
|
||||||
// and that'd be a pain to debug
|
// and that'd be a pain to debug
|
||||||
format!("{}{}-{}", list_rand(&ADJ_LIST), list_rand(&NAME_LIST), gen_token(4).to_uppercase())
|
format!(
|
||||||
|
"{}{}-{}",
|
||||||
|
list_rand(&ADJ_LIST),
|
||||||
|
list_rand(&NAME_LIST),
|
||||||
|
gen_token(4).to_uppercase()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list_rand(list: &[String]) -> &String {
|
pub fn list_rand(list: &[String]) -> &String {
|
||||||
|
|
|
@ -5,7 +5,7 @@ use diesel::prelude::*;
|
||||||
use crate::database::schema::form;
|
use crate::database::schema::form;
|
||||||
use crate::database::schema::form::dsl::*;
|
use crate::database::schema::form::dsl::*;
|
||||||
use crate::database::structs::Form;
|
use crate::database::structs::Form;
|
||||||
use crate::SqliteConnection;
|
use crate::DbConn;
|
||||||
|
|
||||||
#[table_name = "form"]
|
#[table_name = "form"]
|
||||||
#[derive(Serialize, Insertable)]
|
#[derive(Serialize, Insertable)]
|
||||||
|
@ -22,7 +22,7 @@ impl Form {
|
||||||
// also updates lastvisit_at.
|
// also updates lastvisit_at.
|
||||||
pub fn get_from_token(
|
pub fn get_from_token(
|
||||||
i_token: &str,
|
i_token: &str,
|
||||||
conn: &SqliteConnection,
|
conn: &DbConn,
|
||||||
) -> Result<Option<Form>, diesel::result::Error> {
|
) -> Result<Option<Form>, diesel::result::Error> {
|
||||||
if let Some(formdata) = form
|
if let Some(formdata) = form
|
||||||
.filter(token.eq(i_token))
|
.filter(token.eq(i_token))
|
||||||
|
@ -38,10 +38,7 @@ impl Form {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_lastvisit(
|
pub fn update_lastvisit(&self, conn: &DbConn) -> Result<usize, diesel::result::Error> {
|
||||||
&self,
|
|
||||||
conn: &SqliteConnection,
|
|
||||||
) -> Result<usize, diesel::result::Error> {
|
|
||||||
diesel::update(form.find(self.id))
|
diesel::update(form.find(self.id))
|
||||||
.set(lastvisit_at.eq(Utc::now().naive_utc()))
|
.set(lastvisit_at.eq(Utc::now().naive_utc()))
|
||||||
.execute(conn)
|
.execute(conn)
|
||||||
|
@ -49,7 +46,7 @@ impl Form {
|
||||||
|
|
||||||
pub fn insert<'b>(
|
pub fn insert<'b>(
|
||||||
i_form: InsertableForm<'b>,
|
i_form: InsertableForm<'b>,
|
||||||
conn: &SqliteConnection,
|
conn: &DbConn,
|
||||||
) -> Result<InsertableForm<'b>, diesel::result::Error> {
|
) -> Result<InsertableForm<'b>, diesel::result::Error> {
|
||||||
match diesel::insert_into(form).values(&i_form).execute(conn) {
|
match diesel::insert_into(form).values(&i_form).execute(conn) {
|
||||||
Ok(_) => Ok(i_form),
|
Ok(_) => Ok(i_form),
|
||||||
|
|
|
@ -90,9 +90,14 @@ pub async fn forward(
|
||||||
r#"{{"id":{},"keyValuePairs":{{"isAnonymous":true}}}}"#,
|
r#"{{"id":{},"keyValuePairs":{{"isAnonymous":true}}}}"#,
|
||||||
form_id
|
form_id
|
||||||
);
|
);
|
||||||
let update_req = forge_from("/ocs/v2.php/apps/forms/api/v1/form/update", &req, &url, &client)
|
let update_req = forge_from(
|
||||||
.set_header("content-length", forged_body.len())
|
"/ocs/v2.php/apps/forms/api/v1/form/update",
|
||||||
.set_header("content-type", "application/json;charset=utf-8");
|
&req,
|
||||||
|
&url,
|
||||||
|
&client,
|
||||||
|
)
|
||||||
|
.set_header("content-length", forged_body.len())
|
||||||
|
.set_header("content-type", "application/json;charset=utf-8");
|
||||||
|
|
||||||
let res = update_req.send_body(forged_body).await.map_err(|e| {
|
let res = update_req.send_body(forged_body).await.map_err(|e| {
|
||||||
eprintln!("error_forward_isanon: {}", e);
|
eprintln!("error_forward_isanon: {}", e);
|
||||||
|
@ -104,14 +109,14 @@ pub async fn forward(
|
||||||
eprintln!("error_forward_clientresp_newform: {}", e);
|
eprintln!("error_forward_clientresp_newform: {}", e);
|
||||||
crash(get_lang(&req), "error_forward_clientresp_newform")
|
crash(get_lang(&req), "error_forward_clientresp_newform")
|
||||||
})?)
|
})?)
|
||||||
|
} else {
|
||||||
|
Ok(
|
||||||
|
client_resp.body(res.body().limit(PAYLOAD_LIMIT).await.map_err(|e| {
|
||||||
|
eprintln!("error_forward_clientresp_newform: {}", e);
|
||||||
|
crash(get_lang(&req), "error_forward_clientresp_std")
|
||||||
|
})?),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
Ok(client_resp.body(res.body().limit(PAYLOAD_LIMIT).await.map_err(|e| {
|
|
||||||
eprintln!("error_forward_clientresp_newform: {}", e);
|
|
||||||
crash(get_lang(&req), "error_forward_clientresp_std")
|
|
||||||
})?))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// check the response before returning it (unused)
|
// check the response before returning it (unused)
|
||||||
/*if check_response(route, &response_body) {
|
/*if check_response(route, &response_body) {
|
||||||
|
@ -202,14 +207,14 @@ pub async fn forward_register(
|
||||||
.as_str();
|
.as_str();
|
||||||
// sanitize the token beforehand, cookies are unsafe
|
// sanitize the token beforehand, cookies are unsafe
|
||||||
if check_token(&admin_token) {
|
if check_token(&admin_token) {
|
||||||
return Ok(web_redir(
|
return Ok(
|
||||||
&format!(
|
web_redir(&format!("{}/admin/{}", CONFIG.sncf_url, &admin_token))
|
||||||
"{}/admin/{}",
|
.await
|
||||||
CONFIG.sncf_url, &admin_token)
|
.map_err(|e| {
|
||||||
).await.map_err(|e| {
|
eprintln!("error_redirect (admin): {}", e);
|
||||||
eprintln!("error_redirect (admin): {}", e);
|
crash(get_lang(&req), "error_redirect")
|
||||||
crash(get_lang(&req), "error_redirect")
|
})?,
|
||||||
})?);
|
);
|
||||||
} else {
|
} else {
|
||||||
debug("Incorrect admin token given in cookies.");
|
debug("Incorrect admin token given in cookies.");
|
||||||
debug(&format!("Token: {:#?}", &admin_token));
|
debug(&format!("Token: {:#?}", &admin_token));
|
||||||
|
@ -265,7 +270,9 @@ pub async fn forward_register(
|
||||||
eprintln!("error_tplrender (TplLink): {}", e);
|
eprintln!("error_tplrender (TplLink): {}", e);
|
||||||
crash(lang.clone(), "error_tplrender")
|
crash(lang.clone(), "error_tplrender")
|
||||||
})?,
|
})?,
|
||||||
).await.map_err(|e| {
|
)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
eprintln!("error_tplrender_resp (TplLink): {}", e);
|
eprintln!("error_tplrender_resp (TplLink): {}", e);
|
||||||
crash(lang, "error_tplrender_resp")
|
crash(lang, "error_tplrender_resp")
|
||||||
})?)
|
})?)
|
||||||
|
@ -304,16 +311,20 @@ fn web_redir(location: &str) -> HttpResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn index(req: HttpRequest) -> Result<HttpResponse, TrainCrash> {
|
pub async fn index(req: HttpRequest) -> Result<HttpResponse, TrainCrash> {
|
||||||
Ok(HttpResponse::Ok().content_type("text/html").body(
|
Ok(HttpResponse::Ok()
|
||||||
TplIndex {
|
.content_type("text/html")
|
||||||
lang: &get_lang(&req),
|
.body(
|
||||||
}
|
TplIndex {
|
||||||
.render()
|
lang: &get_lang(&req),
|
||||||
|
}
|
||||||
|
.render()
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("error_tplrender (TplIndex): {}", e);
|
||||||
|
crash(get_lang(&req), "error_tplrender")
|
||||||
|
})?,
|
||||||
|
)
|
||||||
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
eprintln!("error_tplrender (TplIndex): {}", e);
|
|
||||||
crash(get_lang(&req), "error_tplrender")
|
|
||||||
})?,
|
|
||||||
).await.map_err(|e| {
|
|
||||||
eprintln!("error_tplrender_resp (TplIndex): {}", e);
|
eprintln!("error_tplrender_resp (TplIndex): {}", e);
|
||||||
crash(get_lang(&req), "error_tplrender_resp")
|
crash(get_lang(&req), "error_tplrender_resp")
|
||||||
})?)
|
})?)
|
||||||
|
|
|
@ -72,9 +72,7 @@ const BLOCKED_ROUTES: &[&str] = &[
|
||||||
];
|
];
|
||||||
|
|
||||||
// ...except if they are in this list
|
// ...except if they are in this list
|
||||||
const ALLOWED_ROUTES: &[&str] = &[
|
const ALLOWED_ROUTES: &[&str] = &["/ocs/v2.php/apps/forms/"];
|
||||||
"/ocs/v2.php/apps/forms/",
|
|
||||||
];
|
|
||||||
|
|
||||||
// checks if the accessed route is allowed for the user.
|
// checks if the accessed route is allowed for the user.
|
||||||
// if it returns true, redirects elsewhere
|
// if it returns true, redirects elsewhere
|
||||||
|
|
Loading…
Reference in a new issue