Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcd31fba

Browse files
committed
Add Access Control component with strategies and voters
Introduce the new Access Control component in Symfony, providing core access decision-making and voting mechanisms. This includes support for affirmative, unanimous, and consensus strategies, along with role-based and expression-based voters. Tests and examples included to validate behavior.
1 parent4dcb217 commitcd31fba

File tree

45 files changed

+1814
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1814
-0
lines changed

‎composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"symfony/polyfill-uuid":"^1.15"
5959
},
6060
"replace": {
61+
"symfony/access-control":"self.version",
6162
"symfony/asset":"self.version",
6263
"symfony/asset-mapper":"self.version",
6364
"symfony/browser-kit":"self.version",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Testsexport-ignore
2+
/phpunit.xml.distexport-ignore
3+
/.git*export-ignore
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
4+
Tests/Fixtures/var/
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AccessControl;
4+
5+
useSymfony\Component\AccessControl\Event\AccessDecisionEvent;
6+
useSymfony\Component\AccessControl\Event\VoteEvent;
7+
useSymfony\Component\AccessControl\Exception\InvalidStrategyException;
8+
useSymfony\Component\AccessControl\Strategy\AffirmativeStrategy;
9+
useSymfony\Component\AccessControl\Strategy\StrategyInterface;
10+
useSymfony\Contracts\EventDispatcher\EventDispatcherInterface;
11+
12+
/**
13+
* @experimental
14+
*/
15+
class AccessControlManagerimplements AccessControlManagerInterface
16+
{
17+
privatereadonlystring$defaultStrategy;
18+
19+
/**
20+
* @var array<string, StrategyInterface>
21+
*/
22+
privatereadonlyarray$strategies;
23+
24+
/**
25+
* @var array<string, array<array-key, bool>>
26+
*/
27+
privatearray$votersCacheAttributes = [];
28+
/**
29+
* @var array<string, array<array-key, bool>>
30+
*/
31+
privatemixed$votersCacheSubject = [];
32+
33+
/**
34+
* @param iterable<StrategyInterface> $strategies
35+
* @param iterable<VoterInterface> $voters
36+
*/
37+
publicfunction__construct(
38+
iterable$strategies,
39+
privatereadonlyiterable$voters,
40+
?string$defaultStrategy =null,
41+
privatereadonly ?EventDispatcherInterface$dispatcher =null
42+
) {
43+
$namedStrategies = [];
44+
foreach ($strategiesas$strategy) {
45+
$namedStrategies[$strategy->getName()] =$strategy;
46+
}
47+
if (\count($namedStrategies) ===0) {
48+
$namedStrategies['affirmative'] =newAffirmativeStrategy();
49+
$defaultStrategy ='affirmative';
50+
}
51+
if ($defaultStrategy ===null) {
52+
$defaultStrategy =array_key_first($namedStrategies);
53+
assert($defaultStrategy !==null,'The default strategy cannot be null.');
54+
}
55+
$this->defaultStrategy =$defaultStrategy;
56+
$this->strategies =$namedStrategies;
57+
}
58+
59+
publicfunctiondecide(AccessRequest$accessRequest, ?string$strategy =null):AccessDecision
60+
{
61+
$strategy =$strategy ??$this->defaultStrategy;
62+
if (!isset($this->strategies[$strategy])) {
63+
thrownewInvalidStrategyException(sprintf('Strategy "%s" is not registered. Valid strategies are: %s',$strategy,implode(',',array_keys($this->strategies))));
64+
}
65+
$votes = [];
66+
foreach ($this->getVoters($accessRequest)as$voter) {
67+
$vote =$voter->vote($accessRequest);
68+
$votes[] =$vote;
69+
$this->dispatcher?->dispatch(newVoteEvent($voter,$accessRequest,$vote));
70+
}
71+
72+
$accessDecision =$this->strategies[$strategy]->evaluate($accessRequest,$votes);
73+
if ($accessDecision->decision !== DecisionVote::ACCESS_ABSTAIN) {
74+
$this->dispatcher?->dispatch(newAccessDecisionEvent($accessRequest,$accessDecision));
75+
return$accessDecision;
76+
}
77+
78+
$accessDecision = AccessDecision::deny($accessRequest,$votes,$accessDecision->reason);
79+
if ($accessRequest->allowIfAllAbstainOrTie) {
80+
$accessDecision = AccessDecision::grant($accessRequest,$votes,$accessDecision->reason);
81+
}
82+
83+
$this->dispatcher?->dispatch(newAccessDecisionEvent($accessRequest,$accessDecision));
84+
85+
return$accessDecision;
86+
}
87+
88+
/**
89+
* @return iterable<VoterInterface>
90+
*/
91+
privatefunctiongetVoters(AccessRequest$accessRequest):iterable
92+
{
93+
$keyAttribute =\is_object($accessRequest->attribute) ?$accessRequest->attribute::class :get_debug_type($accessRequest->attribute);
94+
$keySubject =\is_object($accessRequest->subject) ?$accessRequest->subject::class :get_debug_type($accessRequest->subject);
95+
foreach ($this->votersas$key =>$voter) {
96+
if (!isset($this->votersCacheAttributes[$keyAttribute][$key])) {
97+
$this->votersCacheAttributes[$keyAttribute][$key] =$voter->supportsAttribute($accessRequest->attribute);
98+
}
99+
if (!$this->votersCacheAttributes[$keyAttribute][$key]) {
100+
continue;
101+
}
102+
103+
if (!isset($this->votersCacheSubject[$keySubject][$key])) {
104+
$this->votersCacheSubject[$keySubject][$key] =$voter->supportsSubject($accessRequest->subject);
105+
}
106+
if (!$this->votersCacheSubject[$keySubject][$key]) {
107+
continue;
108+
}
109+
yield$voter;
110+
}
111+
}
112+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AccessControl;
4+
5+
/**
6+
* @experimental
7+
*/
8+
interface AccessControlManagerInterface
9+
{
10+
publicfunctiondecide(AccessRequest$accessRequest, ?string$strategy =null):AccessDecision;
11+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AccessControl;
4+
5+
/**
6+
* @experimental
7+
*/
8+
readonlyclass AccessDecision
9+
{
10+
/**
11+
* @param iterable<VoterOutcome> $votes
12+
*/
13+
publicfunction__construct(
14+
publicAccessRequest$accessRequest,
15+
publicDecisionVote$decision,
16+
publiciterable$votes,
17+
public ?string$reason =null,
18+
) {
19+
}
20+
21+
/**
22+
* @param iterable<VoterOutcome> $votes
23+
*/
24+
publicstaticfunctiongrant(AccessRequest$accessRequest,iterable$votes, ?string$reason =null):self
25+
{
26+
returnnewself($accessRequest, DecisionVote::ACCESS_GRANTED,$votes,$reason);
27+
}
28+
29+
/**
30+
* @param iterable<VoterOutcome> $votes
31+
*/
32+
publicstaticfunctiondeny(AccessRequest$accessRequest,iterable$votes, ?string$reason =null):self
33+
{
34+
returnnewself($accessRequest, DecisionVote::ACCESS_DENIED,$votes,$reason);
35+
}
36+
37+
publicstaticfunctionabstain(AccessRequest$accessRequest,iterable$votes, ?string$reason =null):self
38+
{
39+
returnnewself($accessRequest, DecisionVote::ACCESS_ABSTAIN,$votes,$reason);
40+
}
41+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AccessControl;
4+
5+
useSymfony\Component\ExpressionLanguage\Expression;
6+
useSymfony\Component\Security\Core\Authentication\Token\TokenInterface;
7+
8+
/**
9+
* @experimental
10+
*/
11+
readonlyclass AccessRequest
12+
{
13+
/**
14+
* @param array<array-key, mixed> $metadata
15+
*/
16+
publicfunction__construct(
17+
publicmixed$attribute,
18+
publicmixed$subject =null,
19+
publicarray$metadata = [],
20+
publicbool$allowIfAllAbstainOrTie =false,
21+
) {
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\AccessControl\Attribute;
13+
14+
/**
15+
* @experimental
16+
*/
17+
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)]
18+
readonlyclass AccessPolicy
19+
{
20+
/**
21+
* @param array<array-key, mixed> $
22+
*/
23+
publicfunction__construct(
24+
publicmixed$attribute,
25+
publicmixed$subject =null,
26+
public ?string$strategy =null,
27+
publicarray$metadata = [],
28+
publicbool$allowIfAllAbstain =false,
29+
publicstring$message ='Access Denied.',
30+
public ?int$statusCode =null,
31+
public ?int$exceptionCode =null,
32+
) {
33+
}
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\AccessControl\Attribute;
13+
14+
/**
15+
* @experimental
16+
*/
17+
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)]
18+
readonlyclass All
19+
{
20+
/**
21+
* @param list<AccessPolicy> $accessPolicies
22+
*/
23+
publicfunction__construct(
24+
publicarray$accessPolicies,
25+
publicstring$message ='Access Denied.',
26+
public ?int$statusCode =null,
27+
public ?int$exceptionCode =null,
28+
) {
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\AccessControl\Attribute;
13+
14+
/**
15+
* @experimental
16+
*/
17+
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION)]
18+
readonlyclass AtLeastOneOf
19+
{
20+
/**
21+
* @param list<AccessPolicy> $accessPolicies
22+
*/
23+
publicfunction__construct(
24+
publicarray$accessPolicies,
25+
) {
26+
}
27+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
7.3
5+
---
6+
7+
* Add the component as experimental
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AccessControl;
4+
5+
/**
6+
* @experimental
7+
*/
8+
enum DecisionVote:string
9+
{
10+
caseACCESS_GRANTED ='ACCESS_GRANTED';
11+
caseACCESS_DENIED='ACCESS_DENIED';
12+
caseACCESS_ABSTAIN='ACCESS_ABSTAIN';
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AccessControl\Event;
4+
5+
useSymfony\Component\AccessControl\AccessRequest;
6+
useSymfony\Component\AccessControl\AccessDecision;
7+
useSymfony\Component\AccessControl\VoterOutcome;
8+
useSymfony\Component\Security\Core\Authorization\Voter\VoterInterface;
9+
useSymfony\Contracts\EventDispatcher\Event;
10+
11+
/**
12+
* @experimental
13+
*/
14+
finalclass AccessDecisionEventextends Event
15+
{
16+
publicfunction__construct(
17+
publicreadonlyAccessRequest$accessRequest,
18+
publicreadonlyAccessDecision$accessDecision
19+
) {
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AccessControl\Event;
4+
5+
useSymfony\Component\AccessControl\AccessRequest;
6+
useSymfony\Component\AccessControl\VoterInterface;
7+
useSymfony\Component\AccessControl\VoterOutcome;
8+
useSymfony\Contracts\EventDispatcher\Event;
9+
10+
/**
11+
* @experimental
12+
*/
13+
finalclass VoteEventextends Event
14+
{
15+
publicfunction__construct(
16+
publicreadonlyVoterInterface$voter,
17+
publicreadonlyAccessRequest$accessRequest,
18+
publicreadonlyVoterOutcome$voterOutcome
19+
) {
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespaceSymfony\Component\AccessControl\Exception;
4+
5+
/**
6+
* @experimental
7+
*/
8+
class InvalidStrategyExceptionextends \RuntimeException
9+
{
10+
11+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp