Test for user system and comments
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::telemetry::spawn_blocking_with_tracing;
|
||||
use anyhow::Context;
|
||||
use argon2::{
|
||||
@@ -8,6 +6,7 @@ use argon2::{
|
||||
};
|
||||
use secrecy::{ExposeSecret, SecretString};
|
||||
use sqlx::PgPool;
|
||||
use std::fmt::Display;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct Credentials {
|
||||
|
||||
@@ -6,7 +6,7 @@ mod posts;
|
||||
mod subscribers;
|
||||
|
||||
use crate::{
|
||||
authentication::{AuthenticatedUser, Role},
|
||||
authentication::AuthenticatedUser,
|
||||
routes::{AppError, error_chain_fmt},
|
||||
session_state::TypedSession,
|
||||
templates::{HtmlTemplate, MessageTemplate},
|
||||
@@ -81,11 +81,10 @@ pub async fn require_admin(
|
||||
request: Request,
|
||||
next: Next,
|
||||
) -> Result<Response, AppError> {
|
||||
if let Role::Admin = session
|
||||
.get_role()
|
||||
if session
|
||||
.has_admin_permissions()
|
||||
.await
|
||||
.context("Error retrieving user role in session.")?
|
||||
.ok_or(anyhow::anyhow!("Could not find user role in session."))?
|
||||
{
|
||||
Ok(next.run(request).await)
|
||||
} else {
|
||||
|
||||
@@ -62,7 +62,10 @@ impl TryFrom<CreateUserForm> for NewUser {
|
||||
anyhow::bail!("Password mismatch.");
|
||||
}
|
||||
|
||||
let role = value.admin.map(|_| Role::Admin).unwrap_or(Role::Writer);
|
||||
let role = match value.admin {
|
||||
Some(true) => Role::Admin,
|
||||
_ => Role::Writer,
|
||||
};
|
||||
let password_hash = crate::authentication::compute_pasword_hash(value.password)
|
||||
.context("Failed to hash password.")?;
|
||||
Ok(Self {
|
||||
|
||||
@@ -42,6 +42,15 @@ impl TypedSession {
|
||||
self.0.get(Self::ROLE_KEY).await
|
||||
}
|
||||
|
||||
pub async fn has_admin_permissions(&self) -> Result<bool> {
|
||||
let role = self.0.get(Self::ROLE_KEY).await?;
|
||||
if let Some(Role::Admin) = role {
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn clear(&self) {
|
||||
self.0.clear().await;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user