-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Debugbar is enabled only for admin users (#1046)
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} | ||
} |