Skip to content

Commit ab6ee7d

Browse files
committed
[SecurityBundle] Add XML support for new request attribute matcher
1 parent ad254ad commit ab6ee7d

File tree

6 files changed

+25
-0
lines changed

6 files changed

+25
-0
lines changed

DependencyInjection/MainConfiguration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ private function addAccessControlSection(ArrayNodeDefinition $rootNode)
133133
->prototype('array')
134134
->fixXmlConfig('ip')
135135
->fixXmlConfig('method')
136+
->fixXmlConfig('attribute')
136137
->children()
137138
->scalarNode('request_matcher')->defaultNull()->end()
138139
->scalarNode('requires_channel')->defaultNull()->end()
@@ -148,6 +149,7 @@ private function addAccessControlSection(ArrayNodeDefinition $rootNode)
148149
->prototype('scalar')->end()
149150
->end()
150151
->arrayNode('attributes')
152+
->useAttributeAsKey('key')
151153
->prototype('scalar')->end()
152154
->end()
153155
->scalarNode('route')->defaultNull()->end()

Resources/config/schema/security-1.0.xsd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@
378378
<xsd:element name="method" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
379379
<xsd:element name="role" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
380380
<xsd:element name="allow-if" type="xsd:string" minOccurs="0" maxOccurs="1" />
381+
<xsd:element name="attribute" type="rule_attribute" minOccurs="0" maxOccurs="1" />
381382
</xsd:choice>
382383
<xsd:attribute name="requires-channel" type="xsd:string" />
383384
<xsd:attribute name="path" type="xsd:string" />
@@ -386,6 +387,7 @@
386387
<xsd:attribute name="role" type="xsd:string" />
387388
<xsd:attribute name="methods" type="xsd:string" />
388389
<xsd:attribute name="allow-if" type="xsd:string" />
390+
<xsd:attribute name="route" type="xsd:string" />
389391
</xsd:complexType>
390392

391393
<xsd:complexType name="role">
@@ -397,4 +399,12 @@
397399
</xsd:simpleContent>
398400
</xsd:complexType>
399401

402+
<xsd:complexType name="rule_attribute">
403+
<xsd:simpleContent>
404+
<xsd:extension base="xsd:string">
405+
<xsd:attribute name="key" type="xsd:string" use="required" />
406+
</xsd:extension>
407+
</xsd:simpleContent>
408+
</xsd:complexType>
409+
400410
</xsd:schema>

Tests/DependencyInjection/CompleteConfigurationTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\ContainerBuilder;
2020
use Symfony\Component\DependencyInjection\Definition;
2121
use Symfony\Component\DependencyInjection\Reference;
22+
use Symfony\Component\HttpFoundation\RequestMatcher\AttributesRequestMatcher;
2223
use Symfony\Component\HttpFoundation\RequestMatcher\HostRequestMatcher;
2324
use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher;
2425
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
@@ -326,8 +327,15 @@ public function testAccess()
326327
$this->assertEquals('IS_AUTHENTICATED_ANONYMOUSLY', $attributes[0]);
327328
$expression = $container->getDefinition((string) $attributes[1])->getArgument(0);
328329
$this->assertEquals("token.getUserIdentifier() matches '/^admin/'", $expression);
330+
} elseif (4 === $i) {
331+
$this->assertEquals(['ROLE_ADMIN'], $attributes);
332+
$def = $container->getDefinition((string) $requestMatcher->getArgument(0)[0]);
333+
$this->assertSame(AttributesRequestMatcher::class, $def->getClass());
334+
$this->assertSame(['_controller' => 'AdminController::index', '_route' => 'admin'], $def->getArgument(0));
329335
}
330336
}
337+
338+
$this->assertCount(4, $matcherIds);
331339
}
332340

333341
public function testMerge()

Tests/DependencyInjection/Fixtures/php/container1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
['path' => '/blog/524', 'role' => 'ROLE_USER', 'requires_channel' => 'https', 'methods' => ['get', 'POST'], 'port' => 8000],
9797
['path' => '/blog/.*', 'role' => 'IS_AUTHENTICATED_ANONYMOUSLY'],
9898
['path' => '/blog/524', 'role' => 'IS_AUTHENTICATED_ANONYMOUSLY', 'allow_if' => "token.getUserIdentifier() matches '/^admin/'"],
99+
['role' => 'ROLE_ADMIN', 'attributes' => ['_controller' => 'AdminController::index'], 'route' => 'admin'],
99100
],
100101

101102
'role_hierarchy' => [

Tests/DependencyInjection/Fixtures/xml/container1.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,8 @@
7676
<rule path="/blog/524" role="ROLE_USER" requires-channel="https" methods="get,POST" port="8000" />
7777
<rule role='IS_AUTHENTICATED_ANONYMOUSLY' path="/blog/.*" />
7878
<rule role='IS_AUTHENTICATED_ANONYMOUSLY' allow-if="token.getUserIdentifier() matches '/^admin/'" path="/blog/524" />
79+
<rule role="ROLE_ADMIN" route="admin">
80+
<attribute key="_controller">AdminController::index</attribute>
81+
</rule>
7982
</config>
8083
</srv:container>

Tests/DependencyInjection/Fixtures/yml/container1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,4 @@ security:
8383
path: /blog/.*
8484
role: IS_AUTHENTICATED_ANONYMOUSLY
8585
- { path: /blog/524, role: IS_AUTHENTICATED_ANONYMOUSLY, allow_if: "token.getUserIdentifier() matches '/^admin/'" }
86+
- { role: ROLE_ADMIN, attributes: { _controller: 'AdminController::index' }, route: 'admin' }

0 commit comments

Comments
 (0)