backbone-bucket 0.2.0

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

    // State transition permissions
    /// Permission to execute resolve transition
    pub const TRANSITION_RESOLVE: &str = "file_comment:transition:resolve";
    /// Permission to execute edit transition
    pub const TRANSITION_EDIT: &str = "file_comment:transition:edit";
    /// Permission to execute delete_active transition
    pub const TRANSITION_DELETE_ACTIVE: &str = "file_comment:transition:delete_active";
    /// Permission to execute reopen transition
    pub const TRANSITION_REOPEN: &str = "file_comment:transition:reopen";
    /// Permission to execute delete_resolved transition
    pub const TRANSITION_DELETE_RESOLVED: &str = "file_comment:transition:delete_resolved";

    /// 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_RESOLVE,
            TRANSITION_EDIT,
            TRANSITION_DELETE_ACTIVE,
            TRANSITION_REOPEN,
            TRANSITION_DELETE_RESOLVED,
        ]
    }
}

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

// ==========================================================================
// FileComment Specific Guards
// ==========================================================================

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

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

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

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

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

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

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

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