scheckbuch can't see logbuch

This commit is contained in:
2023-10-24 10:16:26 +02:00
parent c27b83e9fd
commit 1f35be88ad
3 changed files with 38 additions and 2 deletions

View File

@ -59,6 +59,7 @@ pub enum LoginError {
NotAnAdmin,
NotACox,
NotATech,
GuestNotAllowed,
NoPasswordSet(User),
DeserializationError,
}
@ -464,6 +465,39 @@ impl<'r> FromRequest<'r> for AdminUser {
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NonGuestUser {
pub(crate) user: User,
}
impl TryFrom<User> for NonGuestUser {
type Error = LoginError;
fn try_from(user: User) -> Result<Self, Self::Error> {
if user.is_guest {
Err(LoginError::GuestNotAllowed)
} else {
Ok(NonGuestUser { user })
}
}
}
#[async_trait]
impl<'r> FromRequest<'r> for NonGuestUser {
type Error = LoginError;
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
match User::from_request(req).await {
Outcome::Success(user) => match user.try_into() {
Ok(user) => Outcome::Success(user),
Err(_) => Outcome::Failure((Status::Unauthorized, LoginError::NotAnAdmin)),
},
Outcome::Failure(f) => Outcome::Failure(f),
Outcome::Forward(f) => Outcome::Forward(f),
}
}
}
#[cfg(test)]
mod test {
use crate::testdb;