ezidam: guards to check if setup is complete or not
This commit is contained in:
parent
bb3e4dfd40
commit
c95800330a
4 changed files with 74 additions and 0 deletions
5
crates/ezidam/src/guards.rs
Normal file
5
crates/ezidam/src/guards.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
mod completed_setup;
|
||||||
|
mod need_setup;
|
||||||
|
|
||||||
|
pub use completed_setup::CompletedSetup;
|
||||||
|
pub use need_setup::NeedSetup;
|
||||||
34
crates/ezidam/src/guards/completed_setup.rs
Normal file
34
crates/ezidam/src/guards/completed_setup.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
use crate::database::Database;
|
||||||
|
use rocket::http::Status;
|
||||||
|
use rocket::request::{FromRequest, Outcome};
|
||||||
|
use rocket::Request;
|
||||||
|
use users::User;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
GetDatabase,
|
||||||
|
Request(users::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CompletedSetup;
|
||||||
|
|
||||||
|
#[rocket::async_trait]
|
||||||
|
impl<'r> FromRequest<'r> for CompletedSetup {
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
||||||
|
let db = match request.guard::<&Database>().await {
|
||||||
|
Outcome::Success(database) => database,
|
||||||
|
Outcome::Failure(e) => return Outcome::Failure((e.0, Error::GetDatabase)),
|
||||||
|
Outcome::Forward(f) => return Outcome::Forward(f),
|
||||||
|
};
|
||||||
|
|
||||||
|
match User::get_initial_admin(&**db).await {
|
||||||
|
Ok(initial_admin) => match initial_admin {
|
||||||
|
Some(_) => Outcome::Success(CompletedSetup),
|
||||||
|
None => Outcome::Forward(()),
|
||||||
|
},
|
||||||
|
Err(e) => Outcome::Failure((Status::InternalServerError, Error::Request(e))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
crates/ezidam/src/guards/need_setup.rs
Normal file
34
crates/ezidam/src/guards/need_setup.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
use crate::database::Database;
|
||||||
|
use rocket::http::Status;
|
||||||
|
use rocket::request::{FromRequest, Outcome};
|
||||||
|
use rocket::Request;
|
||||||
|
use users::User;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
GetDatabase,
|
||||||
|
Request(users::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct NeedSetup;
|
||||||
|
|
||||||
|
#[rocket::async_trait]
|
||||||
|
impl<'r> FromRequest<'r> for NeedSetup {
|
||||||
|
type Error = Error;
|
||||||
|
|
||||||
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
||||||
|
let db = match request.guard::<&Database>().await {
|
||||||
|
Outcome::Success(database) => database,
|
||||||
|
Outcome::Failure(e) => return Outcome::Failure((e.0, Error::GetDatabase)),
|
||||||
|
Outcome::Forward(f) => return Outcome::Forward(f),
|
||||||
|
};
|
||||||
|
|
||||||
|
match User::get_initial_admin(&**db).await {
|
||||||
|
Ok(initial_admin) => match initial_admin {
|
||||||
|
Some(_) => Outcome::Forward(()),
|
||||||
|
None => Outcome::Success(NeedSetup),
|
||||||
|
},
|
||||||
|
Err(e) => Outcome::Failure((Status::InternalServerError, Error::Request(e))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ use rocket::{Build, Rocket};
|
||||||
mod database;
|
mod database;
|
||||||
mod error;
|
mod error;
|
||||||
mod file_from_bytes;
|
mod file_from_bytes;
|
||||||
|
mod guards;
|
||||||
mod page;
|
mod page;
|
||||||
mod response_timer;
|
mod response_timer;
|
||||||
mod routes;
|
mod routes;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue