clean repo
This commit is contained in:
148
src/rest/mod.rs
148
src/rest/mod.rs
@ -1,148 +0,0 @@
|
||||
mod restday;
|
||||
mod restreg;
|
||||
mod restuser;
|
||||
|
||||
use std::ops::Deref;
|
||||
|
||||
use chrono::{Datelike, Duration, Local, NaiveDate};
|
||||
use rocket::{
|
||||
form::{self, Form, ValueField},
|
||||
fs::FileServer,
|
||||
http::{Cookie, CookieJar},
|
||||
request::FlashMessage,
|
||||
response::{Flash, Redirect},
|
||||
Build, Rocket, State,
|
||||
};
|
||||
use rocket_dyn_templates::{tera, Template};
|
||||
use sea_orm::{Database, DatabaseConnection};
|
||||
use sha3::{Digest, Sha3_256};
|
||||
|
||||
use super::models::{all::DayWithTrips, day, user};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct NaiveDateForm(NaiveDate);
|
||||
|
||||
impl<'v> rocket::form::FromFormField<'v> for NaiveDateForm {
|
||||
fn from_value(field: ValueField<'v>) -> form::Result<'v, NaiveDateForm> {
|
||||
let naivedate = chrono::NaiveDate::parse_from_str(field.value, "%Y-%m-%d").unwrap(); //TODO:
|
||||
//fixme
|
||||
Ok(NaiveDateForm(naivedate))
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for NaiveDateForm {
|
||||
type Target = NaiveDate;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/?<all>")]
|
||||
async fn index(
|
||||
db: &State<DatabaseConnection>,
|
||||
user: user::Model,
|
||||
all: Option<String>,
|
||||
flash: Option<FlashMessage<'_>>,
|
||||
) -> Template {
|
||||
let mut data = Vec::new();
|
||||
|
||||
let mut show_next_n_days = 6;
|
||||
if all.is_some() && user.is_cox {
|
||||
let end_of_year = NaiveDate::from_ymd_opt(Local::now().year(), 12, 31).unwrap();
|
||||
show_next_n_days = end_of_year
|
||||
.signed_duration_since(Local::now().date_naive())
|
||||
.num_days();
|
||||
}
|
||||
|
||||
for i in 0..show_next_n_days {
|
||||
let date = (Local::now() + Duration::days(i)).date_naive();
|
||||
let day = day::Model::find_or_create_day(date, db.inner()).await;
|
||||
data.push(DayWithTrips::new(day, db.inner()).await);
|
||||
}
|
||||
|
||||
let mut context = tera::Context::new();
|
||||
|
||||
if let Some(msg) = flash {
|
||||
context.insert("flash", &msg.into_inner());
|
||||
}
|
||||
context.insert("data", &data);
|
||||
context.insert("user", &user);
|
||||
Template::render("index", context.into_json())
|
||||
}
|
||||
|
||||
#[get("/name")]
|
||||
fn name(flash: Option<FlashMessage<'_>>) -> Template {
|
||||
let mut context = tera::Context::new();
|
||||
|
||||
if let Some(msg) = flash {
|
||||
context.insert("flash", &msg.into_inner());
|
||||
}
|
||||
Template::render("name", context.into_json())
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct NameForm {
|
||||
name: String,
|
||||
pw: Option<String>,
|
||||
}
|
||||
|
||||
#[put("/name", data = "<name>")]
|
||||
async fn savename(
|
||||
name: Form<NameForm>,
|
||||
cookies: &CookieJar<'_>,
|
||||
db: &State<DatabaseConnection>,
|
||||
) -> Flash<Redirect> {
|
||||
let user = user::Model::find_or_create_user(&name.name, db.inner()).await;
|
||||
if let Some(pw) = user.pw {
|
||||
match &name.pw {
|
||||
Some(entered_pw) => {
|
||||
let mut hasher = Sha3_256::new();
|
||||
hasher.update(entered_pw);
|
||||
let entered_pw = hasher.finalize();
|
||||
|
||||
if hex::encode(entered_pw) == pw {
|
||||
log::info!("{} hat sich erfolgreich eingeloggt (mit PW)", name.name);
|
||||
cookies.add_private(Cookie::new("name", name.name.clone()));
|
||||
Flash::success(Redirect::to("/"), "Erfolgreich eingeloggt")
|
||||
} else {
|
||||
log::warn!("Somebody tried to login as {} with a WRONG pw", name.name);
|
||||
Flash::error(Redirect::to("/name"), "Falsches Passwort")
|
||||
}
|
||||
}
|
||||
None => {
|
||||
log::warn!(
|
||||
"Somebody tried to login as {}, w/o specifying a pw",
|
||||
name.name
|
||||
);
|
||||
Flash::error(Redirect::to("/name"), "Benutzer besitzt hat Passwort, du hast jedoch keines eingegeben. Bitte nochmal probieren")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log::info!("{} hat sich erfolgreich eingeloggt (ohne PW)", name.name);
|
||||
cookies.add_private(Cookie::new("name", name.name.clone()));
|
||||
Flash::success(Redirect::to("/"), "Name erfolgreich ausgewählt")
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/logout")]
|
||||
fn logout(cookies: &CookieJar) -> Redirect {
|
||||
cookies.remove_private(Cookie::new("name", ""));
|
||||
Redirect::to("/")
|
||||
}
|
||||
|
||||
#[catch(401)] //unauthorized
|
||||
fn unauthorized_error() -> Redirect {
|
||||
Redirect::to("/name")
|
||||
}
|
||||
|
||||
pub async fn start() -> Rocket<Build> {
|
||||
rocket::build()
|
||||
.attach(Template::fairing())
|
||||
.manage(Database::connect("sqlite://db.sqlite").await.unwrap())
|
||||
.mount("/public", FileServer::from("static/"))
|
||||
.mount("/", routes![index, name, savename, logout])
|
||||
.mount("/day", restday::routes())
|
||||
.mount("/register", restreg::routes())
|
||||
.mount("/user", restuser::routes())
|
||||
.register("/", catchers![unauthorized_error])
|
||||
}
|
Reference in New Issue
Block a user