backbone-bucket 0.4.0

Bucket Bounded Context: File Storage Module for Backbone Framework
Documentation
//! Auth configuration for FileLock 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 FileLock operations
pub mod file_locks {
    /// Permission to list file_lock entities
    pub const LIST: &str = "file_lock:list";
    /// Permission to read file_lock entity details
    pub const READ: &str = "file_lock:read";
    /// Permission to create file_lock entities
    pub const CREATE: &str = "file_lock:create";
    /// Permission to update file_lock entities
    pub const UPDATE: &str = "file_lock:update";
    /// Permission to delete file_lock entities
    pub const DELETE: &str = "file_lock:delete";
    /// Permission to restore deleted file_lock entities
    pub const RESTORE: &str = "file_lock:restore";
    /// Permission to view file_lock trash
    pub const TRASH: &str = "file_lock:trash";
    /// Permission to empty file_lock trash (permanent delete)
    pub const EMPTY_TRASH: &str = "file_lock:empty_trash";
    /// Permission to bulk create file_lock entities
    pub const BULK_CREATE: &str = "file_lock:bulk_create";
    /// Permission to upsert file_lock entities
    pub const UPSERT: &str = "file_lock:upsert";

    // State transition permissions
    /// Permission to execute expire transition
    pub const TRANSITION_EXPIRE: &str = "file_lock:transition:expire";
    /// Permission to execute release transition
    pub const TRANSITION_RELEASE: &str = "file_lock:transition:release";
    /// Permission to execute refresh transition
    pub const TRANSITION_REFRESH: &str = "file_lock:transition:refresh";

    /// 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_EXPIRE,
            TRANSITION_RELEASE,
            TRANSITION_REFRESH,
        ]
    }
}

// ==========================================================================
// 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

// ==========================================================================
// FileLock Specific Guards
// ==========================================================================

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

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

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

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

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

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

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

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