8000 Merge branch '2.7' · symfony/symfony@dc7237f · GitHub
[go: up one dir, main page]

Skip to content

Commit dc7237f

Browse files
committed
Merge branch '2.7'
* 2.7: remove short array syntax fix session restart on PHP 5.3 Avoid missing method when using __invoke Show the inherited roles in the web profiler
2 parents d4057bc + 7ea7035 commit dc7237f

File tree

9 files changed

+82
-45
lines changed

9 files changed

+82
-45
lines changed

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\DataCollector;
1313

14+
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
1415
use Symfony\Component\Security\Core\SecurityContextInterface;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
@@ -24,10 +25,12 @@
2425
class SecurityDataCollector extends DataCollector
2526
{
2627
private $context;
28+
private $roleHierarchy;
2729

28-
public function __construct(SecurityContextInterface $context = null)
30+
public function __construct(SecurityContextInterface $context = null, RoleHierarchyInterface $roleHierarchy = null)
2931
{
3032
$this->context = $context;
33+
$this->roleHierarchy = $roleHierarchy;
3134
}
3235

3336
/**
@@ -42,6 +45,8 @@ public function collect(Request $request, Response $response, \Exception $except
4245
'token_class' => null,
4346
'user' => '',
4447
'roles' => array(),
48+
'inherited_roles' => array(),
49+
'supports_role_hierarchy' => null !== $this->roleHierarchy,
4550
);
4651
} elseif (null === $token = $this->context->getToken()) {
4752
$this->data = array(
@@ -50,14 +55,28 @@ public function collect(Request $request, Response $response, \Exception $except
5055
'token_class' => null,
5156
'user' => '',
5257
'roles' => array(),
58+
'inherited_roles' => array(),
59+
'supports_role_hierarchy' => null !== $this->roleHierarchy,
5360
);
5461
} else {
62+
$inheritedRoles = array();
63+
$assignedRoles = $token->getRoles();
64+
if (null !== $this->roleHierarchy) {
65+
$allRoles = $this->roleHierarchy->getReachableRoles($assignedRoles);
66+
foreach ($allRoles as $role) {
67+
if (!in_array($role, $assignedRoles)) {
68+
$inheritedRoles[] = $role;
69+
}
70+
}
71+
}
5572
$this->data = array(
5673
'enabled' => true,
5774
'authenticated' => $token->isAuthenticated(),
5875
'token_class' => get_class($token),
5976
'user' => $token->getUsername(),
60-
'roles' => array_map(function ($role) { return $role->getRole();}, $token->getRoles()),
77+
'roles' => array_map(function ($role) { return $role->getRole();}, $assignedRoles),
78+
'inherited_roles' => array_map(function ($role) { return $role->getRole();}, $inheritedRoles),
79+
'supports_role_hierarchy' => null !== $this->roleHierarchy,
6180
);
6281
}
6382
}
@@ -92,6 +111,27 @@ public function getRoles()
92111
return $this->data['roles'];
93112
}
94113

114+
/**
115+
* Gets the inherited roles of the user.
116+
*
117+
* @return string The inherited roles
118+
*/
119+
public function getInheritedRoles()
120+
{
121+
return $this->data['inherited_roles'];
122+
}
123+
124+
/**
125+
* Checks if the data contains information about inherited roles. Still the inherited
126+
* roles can be an empty array.
127+
*
128+
* @return bool true if the profile was contains inherited role information.
129+
*/
130+
public function supportsRoleHierarchy()
131+
{
132+
return $this->data['supports_role_hierarchy'];
133+
}
134+
95135
/**
96136
* Checks if the user is authenticated or not.
97137
*

src/Symfony/Bundle/SecurityBundle/Resources/config/collectors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<service id="data_collector.security" class="%data_collector.security.class%" public="false">
1313
<tag name="data_collector" template="@Security/Collector/security.html.twig" id="security" />
1414
<argument type="service" id="security.context" on-invalid="ignore" />
15+
<argument type="service" id="security.role_hierarchy" />
1516
</service>
1617
</services>
1718
</container>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@
6767
<th>Roles</th>
6868
<td>{{ collector.roles|yaml_encode }}</td>
6969
</tr>
70+
{% if collector.supportsRoleHierarchy %}
71+
<tr>
72+
<th>Inherited Roles</th>
73+
<td>{{ collector.inheritedRoles|yaml_encode }}</td>
74+
</tr>
75+
{% endif %}
7076
{% if collector.tokenClass != null %}
7177
<tr>
7278
<th>Token class</th>

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ public function testFirewallWithInvalidUserProvider()
8080

8181
$container->loadFromExtension('security', array(
8282
'providers' => array(
83-
'my_foo' => array('foo' => []),
83+
'my_foo' => array('foo' => array()),
8484
),
8585

8686
'firewalls' => array(
8787
'some_firewall' => array(
8888
'pattern' => '/.*',
89-
'http_basic' => [],
89+
'http_basic' => array(),
9090
),
9191
),
9292
));

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
{% set request_handler %}
55
{% if collector.controller.class is defined %}
66
{% set link = collector.controller.file|file_link(collector.controller.line) %}
7-
<span class="sf-toolbar-info-class sf-toolbar-info-with-next-pointer">{{ collector.controller.class|abbr_class }}</span>
8-
<span class="sf-toolbar-info-method" onclick="{% if link %}window.location='{{link|e('js')}}';window.event.stopPropagation();return false;{% endif %}">
9-
{{ collector.controller.method }}
10-
</span>
7+
{% if collector.controller.method %}
8+
<span class="sf-toolbar-info-class sf-toolbar-info-with-next-pointer">{{ collector.controller.class|abbr_class }}</span>
9+
<span class="sf-toolbar-info-method" onclick="{% if link %}window.location='{{link|e('js')}}';window.event.stopPropagation();return false;{% endif %}">
10+
{{ collector.controller.method }}
11+
</span>
12+
{% else %}
13+
<span class="sf-toolbar-info-class" onclick="{% if link %}window.location='{{link|e('js')}}';window.event.stopPropagation();return false;{% endif %}">{{ collector.controller.class|abbr_class }}</span>
14+
{% endif %}
1115
{% else %}
1216
<span class="sf-toolbar-info-class">{{ collector.controller }}</span>
1317
{% endif %}

src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function setSessionData(array $array)
8989
*/
9090
public function start()
9191
{
92-
if ($this->started && !$this->closed) {
92+
if ($this->started) {
9393
return true;
9494
}
9595

src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ public function getSaveHandler()
126126
*/
127127
public function start()
128128
{
129-
if ($this->started && !$this->closed) {
129+
if ($this->started) {
130130
return true;
131131
}
132132

133133
if (PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status()) {
134134
throw new \RuntimeException('Failed to start the session: already started by PHP.');
135135
}
136136

137-
if (PHP_VERSION_ID < 50400 && isset($_SESSION) && session_id()) {
137+
if (PHP_VERSION_ID < 50400 && !$this->closed && isset($_SESSION) && session_id()) {
138138
// not 100% fool-proof, but is the most reliable way to determine if a session is active in PHP 5.3
139139
throw new \RuntimeException('Failed to start the session: already started by PHP ($_SESSION is set).');
140140
}
@@ -162,10 +162,6 @@ public function start()
162162
*/
163163
public function getId()
164164
{
165-
if (!$this->started && !$this->closed) {
166-
return ''; // returning empty is consistent with session_id() behaviour
167-
}
168-
169165
return $this->saveHandler->getId();
170166
}
171167

src/Symfony/Component/HttpFoundation/Session/Storage/PhpBridgeSessionStorage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct($handler = null, MetadataBag $metaBag = null)
3838
*/
3939
public function start()
4040
{
41-
if ($this->started && !$this->closed) {
41+
if ($this->started) {
4242
return true;
4343
}
4444

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,15 @@ public function testRegisterBagException()
8585
public function testGetId()
8686
{
8787
$storage = $this->getStorage();
88-
$this->assertEquals('', $storage->getId());
88+
$this->assertSame('', $storage->getId(), 'Empty ID before starting session');
8989

9090
$storage->start();
91-
$this->assertNotEquals('', $storage->getId());
91+
$id = $storage->getId();
92+
$this->assertInternalType('string', $id);
93+
$this->assertNotSame('', $id);
9294

9395
$storage->save();
94-
$this->assertNotEquals('', $storage->getId());
96+
$this->assertSame($id, $storage->getId(), 'ID stays after saving session');
9597
}
9698

9799
public function testRegenerate()
@@ -209,18 +211,20 @@ public function testSetSaveHandler54()
209211
/**
210212
* @expectedException \RuntimeException
211213
*/
212-
public function testStartedOutside53()
214+
public function testStartedOutside()
213215
{
214-
if (PHP_VERSION_ID >= 50400) {
215-
$this->markTestSkipped('Test skipped, for PHP 5.3 only.');
216-
}
217-
218216
$storage = $this->getStorage();
219217

220218
$this->assertFalse(isset($_SESSION));
219+
$this->assertFalse($storage->getSaveHandler()->isActive());
220+
$this->assertFalse($storage->isStarted());
221221

222222
session_start();
223223
$this->assertTrue(isset($_SESSION));
224+
if (PHP_VERSION_ID >= 50400) {
225+
// this only works in PHP >= 5.4 where session_status is available
226+
$this->assertTrue($storage->getSaveHandler()->isActive());
227+
}
224228
// PHP session might have started, but the storage driver has not, so false is correct here
225229
$this->assertFalse($storage->isStarted());
226230

@@ -229,29 +233,15 @@ public function testStartedOutside53()
229233
$storage->start();
230234
}
231235

232-
/**
233-
* @expectedException \RuntimeException
234-
*/
235-
public function testCanStartOutside54()
236+
public function testRestart()
236237
{
237-
if (PHP_VERSION_ID < 50400) {
238-
$this->markTestSkipped('Test skipped, for PHP 5.4 only.');
239-
}
240-
241238
$storage = $this->getStorage();
242-
243-
$this->assertFalse(isset($_SESSION));
244-
$this->assertFalse($storage->getSaveHandler()->isActive());
245-
$this->assertFalse($storage->isStarted());
246-
247-
session_start();
248-
$this->assertTrue(isset($_SESSION));
249-
$this->assertTrue($storage->getSaveHandler()->isActive());
250-
// PHP session might have started, but the storage driver has not, so false is correct here
251-
$this->assertFalse($storage->isStarted());
252-
253-
$key = $storage->getMetadataBag()->getStorageKey();
254-
$this->assertFalse(isset($_SESSION[$key]));
255239
$storage->start();
240+
$id = $storage->getId();
241+
$storage->getBag('attributes')->set('lucky', 7);
242+
$storage->save();
243+
$storage->start();
244+
$this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
245+
$this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
256246
}
257247
}

0 commit comments

Comments
 (0)
0