From b4395bce997e324ad6ba32f86e42ca1c4accf796 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Wed, 12 Jan 2022 08:39:33 +0900 Subject: [PATCH] Implement request guard to detect enabled sign-up strategy --- plume-models/src/signups.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/plume-models/src/signups.rs b/plume-models/src/signups.rs index 07bdff15..7a520eab 100644 --- a/plume-models/src/signups.rs +++ b/plume-models/src/signups.rs @@ -1,3 +1,5 @@ +use crate::CONFIG; +use rocket::request::{FromRequest, Outcome, Request}; use std::fmt; use std::str::FromStr; @@ -43,3 +45,28 @@ impl fmt::Display for StrategyError { } impl std::error::Error for StrategyError {} + +pub struct Password(); +pub struct Email(); + +impl<'a, 'r> FromRequest<'a, 'r> for Password { + type Error = (); + + fn from_request(_request: &'a Request<'r>) -> Outcome { + match matches!(CONFIG.signup, Strategy::Password) { + true => Outcome::Success(Self()), + false => Outcome::Forward(()), + } + } +} + +impl<'a, 'r> FromRequest<'a, 'r> for Email { + type Error = (); + + fn from_request(_request: &'a Request<'r>) -> Outcome { + match matches!(CONFIG.signup, Strategy::Email) { + true => Outcome::Success(Self()), + false => Outcome::Forward(()), + } + } +}