PHP code example of phoenix1331 / laravel-auth-audit

1. Go to this page and download the library: Download phoenix1331/laravel-auth-audit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phoenix1331 / laravel-auth-audit example snippets


// routes/api.php
Route::post('/mcp', [McpController::class, 'invoke']); // one route, N tools

// McpController::invoke() dispatches based on request payload:
// - tools/list   -> no per-resource auth needed
// - invoices/get -> must verify the invoice belongs to the authenticated tenant
// - users/delete -> must verify admin role

#[WithoutAuthAudit('MCP gateway: per-tool authorisation enforced inside McpController::invoke() via ToolAuthService')]
public function invoke(Request $request): JsonResponse
{
    // authorisation happens per tool inside dispatch()
}

// config/auth-audit.php
'custom_signals' => [
    'App\\Http\\Controllers\\McpController::invoke',
],

// before - flagged
Route::get('/teams/{team}/orders/{order}', [OrderController::class, 'show']);
// $this->authorize() on $team proves the user owns the team
// but nothing proves the $order belongs to that team

// after - safe
Route::get('/teams/{team}/orders/{order}', [OrderController::class, 'show'])
    ->scopeBindings();

// before - flagged
$this->authorize('update', Order::class);

// after - safe
$this->authorize('update', $order); // pass the bound instance

// before - flagged
public function update(User $user): bool
{
    return $user->isAdmin(); // checks role, not which order is being accessed
}

// after - safe
public function update(User $user, Order $order): bool
{
    return $order->user_id === $user->id;
}

// before - flagged
public function show(Request $request, int $id): Response
{
    $order = Order::findOrFail($id); // any user can access any order
}

// after - safe (relationship-scoped retrieval)
public function show(Request $request, int $id): Response
{
    $order = $request->user()->orders()->findOrFail($id);
}

// before - flagged
Gate::allows('update', $order); // result discarded, no effect

// after - safe
abort_unless(Gate::allows('update', $order), 403);
// or
Gate::authorize('update', $order);

use Phoenix1331\LaravelAuthAudit\Attributes\WithoutAuthAudit;

// on a specific action
#[WithoutAuthAudit('Signature verified via Stripe webhook secret, not policy-gated')]
public function stripe(): void { ... }

// on an entire controller
#[WithoutAuthAudit('Public marketing pages')]
class MarketingController extends Controller { ... }

// with an expiry date - past the date, the bypass reverts to a flagged violation automatically
#[WithoutAuthAudit('Policy not written yet', expires: '2026-12-31')]
public function betaExport(): void { ... }

Route::get('/up', fn () => response()->json(['ok' => true]))
    ->name('health')
    ->withoutAuthAudit('Health check endpoint, no sensitive data, no auth 

// config/auth-audit.php
'custom_signals' => [
    'ensure.team.owner',
    'App\\Services\\TeamAuthService::authorize',
],
bash
php artisan auth-audit:run --min=90
bash
php artisan auth-audit:run --compare=auth-audit-baseline.json --min=80
bash
php artisan vendor:publish --tag=auth-audit-config