forked from Ruderverein-Donau-Linz/rowt
push
This commit is contained in:
@ -18,6 +18,9 @@ pub struct Model {
|
||||
pub is_admin: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct AdminUser(Model);
|
||||
|
||||
impl Model {
|
||||
pub async fn find_or_create_user(name: &str, db: &DatabaseConnection) -> Model {
|
||||
let user = Entity::find()
|
||||
@ -42,6 +45,7 @@ impl Model {
|
||||
#[derive(Debug)]
|
||||
pub enum UserError {
|
||||
NoCookieSet,
|
||||
NoAdmin,
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
@ -61,6 +65,27 @@ impl<'r> FromRequest<'r> for Model {
|
||||
}
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for AdminUser {
|
||||
type Error = UserError;
|
||||
|
||||
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
|
||||
match req.cookies().get("name") {
|
||||
Some(name) => {
|
||||
let db = req.guard::<&'r State<DatabaseConnection>>();
|
||||
let name = name.value();
|
||||
let user = Model::find_or_create_user(name, db.await.unwrap().inner()).await;
|
||||
if user.is_admin {
|
||||
Outcome::Success(AdminUser(user))
|
||||
} else {
|
||||
Outcome::Failure((Status::Unauthorized, UserError::NoAdmin))
|
||||
}
|
||||
}
|
||||
None => Outcome::Failure((Status::Unauthorized, UserError::NoCookieSet)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
|
Reference in New Issue
Block a user