10000 Use VarDumper in the Security data collector · symfony/symfony@6ca7a6d · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ca7a6d

Browse files
committed
Use VarDumper in the Security data collector
1 parent fade0ce commit 6ca7a6d

File tree

4 files changed

+36
-52
lines changed

4 files changed

+36
-52
lines changed

src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
2121
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
2222
use Symfony\Component\Security\Core\Authorization\DebugAccessDecisionManager;
23+
use Symfony\Component\VarDumper\Cloner\Data;
2324

2425
/**
2526
* SecurityDataCollector.
@@ -58,6 +59,7 @@ public function collect(Request $request, Response $response, \Exception $except
5859
$this->data = array(
5960
'enabled' => false,
6061
'authenticated' => false,
62+
'token' => null,
6163
'token_class' => null,
6264
'logout_url' => null,
6365
'user' => '',
@@ -69,6 +71,7 @@ public function collect(Request $request, Response $response, \Exception $except
6971
$this->data = array(
7072
'enabled' => true,
7173
'authenticated' => false,
74+
'token' => null,
7275
'token_class' => null,
7376
'logout_url' => null,
7477
'user' => '',
@@ -101,18 +104,24 @@ public function collect(Request $request, Response $response, \Exception $except
101104
$this->data = array(
102105
'enabled' => true,
103106
'authenticated' => $token->isAuthenticated(),
107+
'token' => $this->cloneVar($token),
104108
'token_class' => get_class($token),
105109
'logout_url' => $logoutUrl,
106110
'user' => $token->getUsername(),
107-
'roles' => array_map(function (RoleInterface $role) { return $role->getRole();}, $assignedRoles),
108-
'inherited_roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $inheritedRoles),
111+
'roles' => $this->cloneVar(array_map(function (RoleInterface $role) { return $role->getRole();}, $assignedRoles)),
112+
'inherited_roles' => $this->cloneVar(array_map(function (RoleInterface $role) { return $role->getRole(); }, $inheritedRoles)),
109113
'supports_role_hierarchy' => D7AE null !== $this->roleHierarchy,
110114
);
111115
}
112116

113117
// collect voters and access decision manager information
114118
if ($this->accessDecisionManager instanceof DebugAccessDecisionManager) {
115-
$this->data['access_decision_log'] = $this->accessDecisionManager->getDecisionLog();
119+
$this->data['access_decision_log'] = array_map(function ($decision) {
120+
$decision['object'] = $this->cloneVar($decision['object']);
121+
122+
return $decision;
123+
}, $this->accessDecisionManager->getDecisionLog());
124+
116125
$this->data['voter_strategy'] = $this->accessDecisionManager->getStrategy();
117126

118127
foreach ($this->accessDecisionManager->getVoters() as $voter) {
@@ -196,6 +205,16 @@ public function getTokenClass()
196205
return $this->data['token_class'];
197206
}
198207

208+
/**
209+
* Get the full security token class as Data object.
210+
*
211+
* @return Data
212+
*/
213+
public function getToken()
214+
{
215+
return $this->data['token'];
216+
}
217+
199218
/**
200219
* Get the provider key (i.e. the name of the active firewall).
201220
*

src/Symfony/Bundle/SecurityBundle/Resources/views/Collector/security.html.twig

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{% block page_title 'Security' %}
44

55
{% block toolbar %}
6-
{% if collector.tokenClass %}
6+
{% if collector.token %}
77
{% set is_authenticated = collector.enabled and collector.authenticated %}
88
{% set color_code = is_authenticated ? '' : 'yellow' %}
99
{% else %}
@@ -16,7 +16,7 @@
1616
{% endset %}
1717

1818
{% set text %}
19-
{% if collector.tokenClass %}
19+
{% if collector.token %}
2020
<div class="sf-toolbar-info-piece">
2121
<b>Logged in as</b>
2222
<span>{{ collector.user }}</span>
@@ -27,7 +27,7 @@
2727
<span class="sf-toolbar-status sf-toolbar-status-{{ is_authenticated ? 'green' : 'red' }}">{{ is_authenticated ? 'Yes' : 'No' }}</span>
2828
</div>
2929

30-
{% if collector.tokenClass != null %}
30+
{% if collector.token != null %}
3131
<div class="sf-toolbar-info-piece">
3232
<b>Token class</b>
3333
<span>{{ collector.tokenClass|abbr_class }}</span>
@@ -54,7 +54,7 @@
5454
{% endblock %}
5555

5656
{% block menu %}
57-
<span class="label {{ not collector.enabled or not collector.tokenClass ? 'disabled' }}">
57+
<span class="label {{ not collector.enabled or not collector.token ? 'disabled' }}">
5858
<span class="icon">{{ include('@Security/Collector/icon.svg') }}</span>
5959
<strong>Security</strong>
6060
</span>
@@ -63,7 +63,7 @@
6363
{% block panel %}
6464
<h2>Security Token</h2>
6565

66-
{% if collector.tokenClass %}
66+
{% if collector.token %}
6767
<div class="metrics">
6868
<div class="metric">
6969
<span class="value">{{ collector.user == 'anon.' ? 'Anonymous' : collector.user }}</span>
@@ -87,7 +87,7 @@
8787
<tr>
8888
<th>Roles</th>
8989
<td>
90-
{{ collector.roles is empty ? 'none' : collector.roles|yaml_encode }}
90+
{{ collector.roles is empty ? 'none' : profiler_dump(collector.roles, maxDepth=1) }}
9191

9292
{% if not collector.authenticated and collector.roles is empty %}
9393
<p class="help">User is not authenticated probably because they have no roles.</p>
@@ -98,14 +98,14 @@
9898
{% if collector.supportsRoleHierarchy %}
9999
<tr>
100100
<th>Inherited Roles</th>
101-
<td>{{ collector.inheritedRoles is empty ? 'none' : collector.inheritedRoles|yaml_encode }}</td>
101+
<td>{{ collector.inheritedRoles is empty ? 'none' : profiler_dump(collector.inheritedRoles, maxDepth=1) }}</td>
102102
</tr>
103103
{% endif %}
104104

105-
{% if collector.tokenClass %}
105+
{% if collector.token %}
106106
<tr>
107-
<th>Token class</th>
108-
<td>{{ collector.tokenClass }}</td>
107+
<th>Token</th>
108+
<td>{{ profiler_dump(collector.token) }}</td>
109109
</tr>
110110
{% endif %}
111111
</tbody>
@@ -152,7 +152,7 @@
152152
{% if collector.accessDecisionLog|default([]) is not empty %}
153153
<h2>Access decision log</h2>
154154

155-
<table class="decision-log">
155+
<table class="decision-log dump-inline">
156156
<col style="width: 30px">
157157
<col style="width: 120px">
158158
<col style="width: 25%">

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
<p>No GET parameters</p>
121121
</div>
122122
{% else %}
123-
{{ include('@WebProfiler/Profiler/bag.html.twig', { bag: collector.requestquery }, with_context = false) }}
123+
{{ include('@WebProfiler/Profiler/bag.html.twig', { bag: collector.requestquery, maxDepth: 1 }, with_context = false) }}
124124
{% endif %}
125125

126126
<h3>POST Parameters</h3>
@@ -130,7 +130,7 @@
130130
<p>No POST parameters</p>
131131
</div>
132132
{% else %}
133-
{{ include('@WebProfiler/Profiler/bag.html.twig', { bag: collector.requestrequest }, with_context = false) }}
133+
{{ include('@WebProfiler/Profiler/bag.html.twig', { bag: collector.requestrequest, maxDepth: 1 }, with_context = false) }}
134134
{% endif %}
135135

136136
<h3>Request Attributes</h3>

src/Symfony/Component/Security/Core/Authorization/DebugAccessDecisionManager.php

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function decide(TokenInterface $token, array $attributes, $object = null)
5050

5151
$this->decisionLog[] = array(
5252
'attributes' => $attributes,
53-
'object' => $this->getStringRepresentation($object),
53+
'object' => $object,
5454
'result' => $result,
5555
);
5656

@@ -96,39 +96,4 @@ public function getDecisionLog()
9696
{
9797
return $this->decisionLog;
9898
}
99-
100-
/**
101-
* @param mixed $object
102-
*
103-
* @return string
104-
*/
105-
private function getStringRepresentation($object)
106-
{
107-
if (null === $object) {
108-
return 'NULL';
109-
}
110-
111-
if (!is_object($object)) {
112-
if (is_bool($object)) {
113-
return sprintf('%s (%s)', gettype($object), $object ? 'true' : 'false');
114-
}
115-
if (is_scalar($object)) {
116-
return sprintf('%s (%s)', gettype($object), $object);
117-
}
118-
119-
return gettype($object);
120-
}
121-
122-
$objectClass = class_exists('Doctrine\Common\Util\ClassUtils') ? ClassUtils::getClass($object) : get_class($object);
123-
124-
if (method_exists($object, 'getId')) {
125-
$objectAsString = sprintf('ID: %s', $object->getId());
126-
} elseif (method_exists($object, '__toString')) {
127-
$objectAsString = (string) $object;
128-
} else {
129-
$objectAsString = sprintf('object hash: %s', spl_object_hash($object));
130-
}
131-
132-
return sprintf('%s (%s)', $objectClass, $objectAsString);
133-
}
13499
}

0 commit comments

Comments
 (0)
0