Skip to content

Commit 89f549c

Browse files
committed
Rework AuthCollector
Seperated from the request, option to show name
1 parent 856ef37 commit 89f549c

File tree

4 files changed

+43
-40
lines changed

4 files changed

+43
-40
lines changed

src/Barryvdh/Debugbar/DataCollector/IlluminateAuthCollector.php renamed to src/Barryvdh/Debugbar/DataCollector/AuthCollector.php

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,34 @@
66
use DebugBar\DataCollector\Renderable;
77
use Illuminate\Auth\AuthManager;
88
use Illuminate\Auth\UserInterface;
9+
use Illuminate\Support\Contracts\ArrayableInterface;
910

1011
/**
1112
* Collector for Laravel's Auth provider
1213
*/
13-
class IlluminateAuthCollector extends DataCollector implements Renderable
14+
class AuthCollector extends DataCollector implements Renderable
1415
{
15-
/**
16-
* @var \Illuminate\Auth\Guard
17-
*/
16+
17+
/** @var \Illuminate\Auth\AuthManager */
1818
protected $auth;
19+
/** @var bool */
20+
protected $showName = false;
1921

2022
/**
2123
* @param \Illuminate\Auth\AuthManager $auth
2224
*/
2325
public function __construct(AuthManager $auth)
2426
{
25-
// Get the driver behind the AuthManager (i.e. the Guard instance)
26-
$this->auth = $auth->driver();
27+
$this->auth = $auth;
28+
}
29+
30+
/**
31+
* Set to show the users name/email
32+
* @param bool $showName
33+
*/
34+
public function setShowName($showName)
35+
{
36+
$this->showName = (bool) $showName;
2737
}
2838

2939
/**
@@ -49,8 +59,8 @@ protected function getUserInformation(UserInterface $user = null)
4959
// Defaults
5060
if (is_null($user)) {
5161
return array(
52-
'user' => 'Guest',
53-
'is_guest' => true,
62+
'name' => 'Guest',
63+
'user' => array('guest' => true),
5464
);
5565
}
5666

@@ -66,8 +76,8 @@ protected function getUserInformation(UserInterface $user = null)
6676
}
6777

6878
return array(
69-
'user' => $identifier,
70-
'is_guest' => false,
79+
'name' => $identifier,
80+
'user' => $user instanceof ArrayableInterface ? $user->toArray() : $user,
7181
);
7282
}
7383

@@ -84,13 +94,22 @@ public function getName()
8494
*/
8595
public function getWidgets()
8696
{
87-
return array(
88-
'user' => array(
97+
$widgets = array(
98+
'auth' => array(
99+
'icon' => 'lock',
100+
'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
101+
'map' => 'auth.user',
102+
'default' => '{}'
103+
)
104+
);
105+
if($this->showName){
106+
$widgets['auth.name'] = array(
89107
'icon' => 'user',
90108
'tooltip' => 'Auth status',
91-
'map' => 'auth.user',
109+
'map' => 'auth.name',
92110
'default' => '',
93-
),
94-
);
111+
);
112+
}
113+
return $widgets;
95114
}
96115
}

src/Barryvdh/Debugbar/DataCollector/SymfonyRequestCollector.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ class SymfonyRequestCollector extends DataCollector implements DataCollectorInte
3131
* @param \Symfony\Component\HttpFoundation\Request $request
3232
* @param \Symfony\Component\HttpFoundation\Request $response
3333
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
34-
* @param \Illuminate\Auth\AuthManager $auth
3534
*/
36-
public function __construct($request, $response, $session, AuthManager $auth = null)
35+
public function __construct($request, $response, $session)
3736
{
3837
$this->request = $request;
3938
$this->response = $response;
4039
$this->session = $session;
41-
$this->auth = $auth;
4240
}
4341

4442
/**
@@ -68,19 +66,6 @@ public function getWidgets()
6866
*/
6967
public function collect()
7068
{
71-
$user = null;
72-
if($this->auth){
73-
try{
74-
$user = $this->auth->user();
75-
if(is_null($user)){
76-
$user = 'Guest';
77-
}elseif($user instanceof ArrayableInterface){
78-
$user = $user->toArray();
79-
}
80-
}catch(\Exception $e){
81-
$user = 'Not available';
82-
}
83-
}
8469

8570
$request = $this->request;
8671
$response = $this->response;
@@ -102,7 +87,6 @@ public function collect()
10287
$statusCode = $response->getStatusCode();
10388

10489
$data = array(
105-
'auth' => $user ?: '?',
10690
'format' => $request->getRequestFormat(),
10791
'content_type' => $response->headers->get('Content-Type') ? $response->headers->get('Content-Type') : 'text/html',
10892
'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : '',

src/Barryvdh/Debugbar/LaravelDebugBar.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Barryvdh\Debugbar\DataCollector\SymfonyRequestCollector;
1818
use Barryvdh\Debugbar\DataCollector\FilesCollector;
1919
use Barryvdh\Debugbar\DataCollector\LogsCollector;
20-
use Barryvdh\Debugbar\DataCollector\IlluminateAuthCollector;
20+
use Barryvdh\Debugbar\DataCollector\AuthCollector;
2121
use Barryvdh\Debugbar\DataCollector\QueryCollector;
2222
use Barryvdh\Debugbar\Storage\FilesystemStorage;
2323

@@ -280,7 +280,9 @@ public function boot(){
280280

281281
if ($this->shouldCollect('auth', false)) {
282282
try{
283-
$this->addCollector(new IlluminateAuthCollector($app['auth']));
283+
$authCollector = new AuthCollector($app['auth']);
284+
$authCollector->setShowName($this->app['config']->get('laravel-debugbar::config.options.auth.show_name'));
285+
$this->addCollector($authCollector);
284286
}catch(\Exception $e){
285287
$this->addException(new Exception('Cannot add AuthCollector to Laravel Debugbar: '. $e->getMessage(), $e->getCode(), $e));
286288
}
@@ -321,12 +323,7 @@ public function modifyResponse($request, $response){
321323

322324
if($this->shouldCollect('symfony_request', true) and !$this->hasCollector('request')){
323325
try{
324-
$auth = $app['auth'];
325-
}catch(\Exception $e){
326-
$auth = null;
327-
}
328-
try{
329-
$this->addCollector(new SymfonyRequestCollector($request, $response, $sessionManager, $auth));
326+
$this->addCollector(new SymfonyRequestCollector($request, $response, $sessionManager));
330327
}catch(\Exception $e){
331328
$this->addException(new Exception('Cannot add SymfonyRequestCollector to Laravel Debugbar: '. $e->getMessage(), $e->getCode(), $e));
332329
}

src/config/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@
109109
*/
110110

111111
'options' => array(
112+
'auth' => array(
113+
'show_name' => false, // Also show the users name/email in the debugbar
114+
),
112115
'db' => array(
113116
'with_params' => true, // Render SQL with the parameters substituted
114117
'timeline' => false, // Add the queries to the timeline

0 commit comments

Comments
 (0)