Skip to content

Commit

Permalink
Add guard wrapper that accepts an argument (#58)
Browse files Browse the repository at this point in the history
Adds a guard wrapper that accepts an argument, allowing all checks to be
homogeneous.
  • Loading branch information
akrantz01 authored Jun 11, 2024
1 parent 0158f7e commit d093c6b
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ where
move |ctx| check(ctx).map(|_| ())
}

/// Create a [`async_graphql::Guard`] out of a check function that requires an argument
pub fn guard_where<F, A, R>(
check: F,
argument: A,
) -> impl Fn(&Context<'_>) -> Result<()> + Send + Sync + 'static
where
A: Copy + Send + Sync + 'static,
F: Fn(&Context<'_>, A) -> Result<R> + Send + Sync + 'static,
{
move |ctx| check(ctx, argument).map(|_| ())
}

/// An error raised when the user has invalid permissions
#[derive(Debug)]
pub struct Forbidden;
Expand Down Expand Up @@ -77,33 +89,27 @@ pub fn admin_only(ctx: &Context<'_>) -> Result<()> {
}

/// Ensure the user has the required role for the event
pub fn has_role(role: UserRole) -> impl Fn(&Context<'_>) -> Result<()> + Send + Sync + 'static {
move |ctx| {
is_event(ctx)?;
let user = is_authenticated(ctx)?;

if user.role == Some(role) {
Ok(())
} else {
Err(Forbidden.into())
}
pub fn has_role(ctx: &Context<'_>, role: UserRole) -> Result<()> {
is_event(ctx)?;
let user = is_authenticated(ctx)?;

if user.role == Some(role) {
Ok(())
} else {
Err(Forbidden.into())
}
}

/// Ensure the user has at least the required role for the event
pub fn has_at_least_role(
role: UserRole,
) -> impl Fn(&Context<'_>) -> Result<()> + Send + Sync + 'static {
move |ctx| {
is_event(ctx)?;
let user = is_authenticated(ctx)?;

if let Some(user_role) = user.role {
if user_role >= role {
return Ok(());
}
}
pub fn has_at_least_role(ctx: &Context<'_>, role: UserRole) -> Result<UserRole> {
is_event(ctx)?;
let user = is_authenticated(ctx)?;

Err(Forbidden.into())
if let Some(user_role) = user.role {
if user_role >= role {
return Ok(user_role);
}
}

Err(Forbidden.into())
}

0 comments on commit d093c6b

Please sign in to comment.