Skip to content

Commit

Permalink
Debugbar is enabled only for admin users (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix authored Dec 25, 2024
1 parent 7e5f601 commit 13d2d5d
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/Http/Middleware/DebugbarEnable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Middleware;

use Barryvdh\Debugbar\Facades\Debugbar;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class DebugbarEnable
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, \Closure $next): Response
{
if (Auth::check() && Auth::user()->hasRole('admin')) {
Debugbar::enable();
} else {
Debugbar::disable();
}

return $next($request);
}
}
4 changes: 4 additions & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
]);

$middleware->appendToGroup('web', [
\App\Http\Middleware\DebugbarEnable::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
Expand Down
72 changes: 72 additions & 0 deletions tests/Feature/DebugbarEnableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Tests\Feature;

use App\Http\Middleware\DebugbarEnable;
use Barryvdh\Debugbar\Facades\Debugbar;
use Illuminate\Http\Request;
use Tests\TestCase;

class DebugbarEnableTest extends TestCase
{
/**
* Test that the Debugbar is enabled for a logged in user.
*
* @see App\Http\Middleware\DebugbarEnable::handle()
*/
public function testDebugbarIsEnabledForLoggedInUser()
{
$this->actingAs($this->adminUser());

// Create a dummy request
$request = Request::create('/test', 'GET');

$middleware = new DebugbarEnable;
$middleware->handle($request, function ($request) {
$this->assertTrue(Debugbar::isEnabled());
$this->assertTrue(Debugbar::hasCollector('time'));

return response('OK');
});
}

/**
* Test that the Debugbar is disabled for a basic user.
*
* @see App\Http\Middleware\DebugbarEnable::handle()
*/
public function testDebugbarIsDisabledForBasicUser()
{
$this->actingAs($this->basicUser());

// Create a dummy request
$request = Request::create('/test', 'GET');

$middleware = new DebugbarEnable;
$middleware->handle($request, function ($request) {
$this->assertFalse(Debugbar::isEnabled());
$this->assertFalse(Debugbar::hasCollector('time'));

return response('OK');
});
}

/**
* Test that the Debugbar is disabled for a guest user.
*
* @see App\Http\Middleware\DebugbarEnable::handle()
*/
public function testDebugbarIsDisabledForGuestUser()
{
// Create a dummy request
$request = Request::create('/test', 'GET');

$middleware = new DebugbarEnable;
$middleware->handle($request, function ($request) {
$this->assertFalse(Debugbar::isEnabled());
$this->assertFalse(Debugbar::hasCollector('time'));

return response('OK');
});
}
}

0 comments on commit 13d2d5d

Please sign in to comment.