backbone-bucket 0.4.3

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Auth configuration for StoredFile handlers
//!
//! Generated by backbone-schema. Do not edit manually.
//!
//! This module provides:
//! - Permission definitions for CRUD operations
//! - Role-based guards
//! - Auth middleware configuration

use std::sync::Arc;
use super::AuthContextExt;

use backbone_auth::middleware::AuthContext;
use axum::{
    extract::{Request, State},
    http::StatusCode,
    middleware::Next,
    response::Response,
};

// ==========================================================================
// Permission Constants
// ==========================================================================

/// Permission identifiers for StoredFile operations
pub mod stored_files {
    /// Permission to list stored_file entities
    pub const LIST: &str = "stored_file:list";
    /// Permission to read stored_file entity details
    pub const READ: &str = "stored_file:read";
    /// Permission to create stored_file entities
    pub const CREATE: &str = "stored_file:create";
    /// Permission to update stored_file entities
    pub const UPDATE: &str = "stored_file:update";
    /// Permission to delete stored_file entities
    pub const DELETE: &str = "stored_file:delete";
    /// Permission to restore deleted stored_file entities
    pub const RESTORE: &str = "stored_file:restore";
    /// Permission to view stored_file trash
    pub const TRASH: &str = "stored_file:trash";
    /// Permission to empty stored_file trash (permanent delete)
    pub const EMPTY_TRASH: &str = "stored_file:empty_trash";
    /// Permission to bulk create stored_file entities
    pub const BULK_CREATE: &str = "stored_file:bulk_create";
    /// Permission to upsert stored_file entities
    pub const UPSERT: &str = "stored_file:upsert";

    // State transition permissions
    /// Permission to execute complete_upload transition
    pub const TRANSITION_COMPLETE_UPLOAD: &str = "stored_file:transition:complete_upload";
    /// Permission to execute mark_safe transition
    pub const TRANSITION_MARK_SAFE: &str = "stored_file:transition:mark_safe";
    /// Permission to execute quarantine transition
    pub const TRANSITION_QUARANTINE: &str = "stored_file:transition:quarantine";
    /// Permission to execute mark_safe_by_admin transition
    pub const TRANSITION_MARK_SAFE_BY_ADMIN: &str = "stored_file:transition:mark_safe_by_admin";
    /// Permission to execute soft_delete transition
    pub const TRANSITION_SOFT_DELETE: &str = "stored_file:transition:soft_delete";
    /// Permission to execute restore transition
    pub const TRANSITION_RESTORE: &str = "stored_file:transition:restore";
    /// Permission to execute purge transition
    pub const TRANSITION_PURGE: &str = "stored_file:transition:purge";

    /// Get all permissions for this entity
    pub fn all() -> Vec<&'static str> {
        vec![
            LIST,
            READ,
            CREATE,
            UPDATE,
            DELETE,
            RESTORE,
            TRASH,
            EMPTY_TRASH,
            BULK_CREATE,
            UPSERT,
            TRANSITION_COMPLETE_UPLOAD,
            TRANSITION_MARK_SAFE,
            TRANSITION_QUARANTINE,
            TRANSITION_MARK_SAFE_BY_ADMIN,
            TRANSITION_SOFT_DELETE,
            TRANSITION_RESTORE,
            TRANSITION_PURGE,
        ]
    }
}

// ==========================================================================
// Permission Checking
// ==========================================================================

/// Check if auth context has a specific permission
pub fn has_permission(auth: &AuthContext, permission: &str) -> bool {
    auth.has_permission(permission)
}

/// Check if auth context has a specific role
pub fn has_role(auth: &AuthContext, role: &str) -> bool {
    auth.has_role(role)
}

/// Check if auth context has all of the required roles
pub fn has_all_roles(auth: &AuthContext, roles: &[&str]) -> bool {
    auth.has_all_roles(roles)
}

/// Check if auth context has any of the required permissions
pub fn has_any_permission(auth: &AuthContext, permissions: &[&str]) -> bool {
    permissions.iter().any(|p| auth.has_permission(p))
}

// ==========================================================================
// Permission Guards (Axum Middleware)
// ==========================================================================

/// Note: Axum middleware should be implemented as async fn, not impl Fn
/// This avoids E0562: `impl Trait` is not allowed in the return type of `Fn` trait bounds

// ==========================================================================
// StoredFile Specific Guards
// ==========================================================================

/// Guard that requires list permission for stored_file
pub async fn require_list_stored_file(
    auth: axum::extract::Extension<AuthContext>,
    request: Request,
    next: Next,
) -> Result<Response, StatusCode> {
    if has_permission(&auth, stored_files::LIST) {
        Ok(next.run(request).await)
    } else {
        Err(StatusCode::FORBIDDEN)
    }
}

/// Guard that requires read permission for stored_file
pub async fn require_read_stored_file(
    auth: axum::extract::Extension<AuthContext>,
    request: Request,
    next: Next,
) -> Result<Response, StatusCode> {
    if has_permission(&auth, stored_files::READ) {
        Ok(next.run(request).await)
    } else {
        Err(StatusCode::FORBIDDEN)
    }
}

/// Guard that requires create permission for stored_file
pub async fn require_create_stored_file(
    auth: axum::extract::Extension<AuthContext>,
    request: Request,
    next: Next,
) -> Result<Response, StatusCode> {
    if has_permission(&auth, stored_files::CREATE) {
        Ok(next.run(request).await)
    } else {
        Err(StatusCode::FORBIDDEN)
    }
}

/// Guard that requires update permission for stored_file
pub async fn require_update_stored_file(
    auth: axum::extract::Extension<AuthContext>,
    request: Request,
    next: Next,
) -> Result<Response, StatusCode> {
    if has_permission(&auth, stored_files::UPDATE) {
        Ok(next.run(request).await)
    } else {
        Err(StatusCode::FORBIDDEN)
    }
}

/// Guard that requires delete permission for stored_file
pub async fn require_delete_stored_file(
    auth: axum::extract::Extension<AuthContext>,
    request: Request,
    next: Next,
) -> Result<Response, StatusCode> {
    if has_permission(&auth, stored_files::DELETE) {
        Ok(next.run(request).await)
    } else {
        Err(StatusCode::FORBIDDEN)
    }
}

/// Guard that requires restore permission for stored_file
pub async fn require_restore_stored_file(
    auth: axum::extract::Extension<AuthContext>,
    request: Request,
    next: Next,
) -> Result<Response, StatusCode> {
    if has_permission(&auth, stored_files::RESTORE) {
        Ok(next.run(request).await)
    } else {
        Err(StatusCode::FORBIDDEN)
    }
}

/// Guard that requires view trash permission for stored_file
pub async fn require_trash_stored_file(
    auth: axum::extract::Extension<AuthContext>,
    request: Request,
    next: Next,
) -> Result<Response, StatusCode> {
    if has_permission(&auth, stored_files::TRASH) {
        Ok(next.run(request).await)
    } else {
        Err(StatusCode::FORBIDDEN)
    }
}

/// Guard that requires empty trash permission for stored_file
pub async fn require_empty_trash_stored_file(
    auth: axum::extract::Extension<AuthContext>,
    request: Request,
    next: Next,
) -> Result<Response, StatusCode> {
    if has_permission(&auth, stored_files::EMPTY_TRASH) {
        Ok(next.run(request).await)
    } else {
        Err(StatusCode::FORBIDDEN)
    }
}