Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit 43d8751

Browse files
minor #21088 Rename DebugAccessDecisionManager to TraceableAccessDecisionManager (Jean85)
This PR was squashed before being merged into the 3.3-dev branch (closes #21088). Discussion ---------- Rename DebugAccessDecisionManager to TraceableAccessDecisionManager | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21085 | License | MIT [EDIT] No longer WIP, test passing. Also, test added to preserve BC with the SecurityBundle. Commits ------- c5e0e59 Rename DebugAccessDecisionManager to TraceableAccessDecisionManager
2 parents d8ef128 + 866bd6f commit 43d8751

File tree

3 files changed

+118
-84
lines changed

3 files changed

+118
-84
lines changed

Core/Authorization/DebugAccessDecisionManager.php

Lines changed: 6 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -11,88 +11,12 @@
1111

1212
namespace Symfony\Component\Security\Core\Authorization;
1313

14-
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15-
16-
/**
17-
* Decorates the original AccessDecisionManager class to log information
18-
* about the security voters and the decisions made by them.
14+
/*
15+
* @internal
1916
*
20-
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
17+
* @deprecated The DebugAccessDecisionManager class has been renamed and is deprecated since version 3.3 and will be removed in 4.0. Use the TraceableAccessDecisionManager class instead.
2118
*
22-
* @internal
19+
* This is a placeholder for the old class, that got renamed; this is not a BC break since the class is internal, this
20+
* placeholder is here just to help backward compatibility with older SecurityBundle versions.
2321
*/
24-
class DebugAccessDecisionManager implements AccessDecisionManagerInterface
25-
{
26-
private $manager;
27-
private $strategy;
28-
private $voters = array();
29-
private $decisionLog = array();
30-
31-
public function __construct(AccessDecisionManagerInterface $manager)
32-
{
33-
$this->manager = $manager;
34-
35-
if ($this->manager instanceof AccessDecisionManager) {
36-
// The strategy is stored in a private property of the decorated service
37-
$reflection = new \ReflectionProperty(AccessDecisionManager::class, 'strategy');
38-
$reflection->setAccessible(true);
39-
$this->strategy = $reflection->getValue($manager);
40-
}
41-
}
42-
43-
/**
44-
* {@inheritdoc}
45-
*/
46-
public function decide(TokenInterface $token, array $attributes, $object = null)
47-
{
48-
$result = $this->manager->decide($token, $attributes, $object);
49-
50-
$this->decisionLog[] = array(
51-
'attributes' => $attributes,
52-
'object' => $object,
53-
'result' => $result,
54-
);
55-
56-
return $result;
57-
}
58-
59-
/**
60-
* {@inheritdoc}
61-
*/
62-
public function setVoters(array $voters)
63-
{
64-
if (!method_exists($this->manager, 'setVoters')) {
65-
return;
66-
}
67-
68-
$this->voters = $voters;
69-
$this->manager->setVoters($voters);
70-
}
71-
72-
/**
73-
* @return string
74-
*/
75-
public function getStrategy()
76-
{
77-
// The $strategy property is misleading because it stores the name of its
78-
// method (e.g. 'decideAffirmative') instead of the original strategy name
79-
// (e.g. 'affirmative')
80-
return null === $this->strategy ? '-' : strtolower(substr($this->strategy, 6));
81-
}
82-
83-
/**
84-
* @return array
85-
*/
86-
public function getVoters()
87-
{
88-
return $this->voters;
89-
}
90-
91-
/**
92-
* @return array
93-
*/
94-
public function getDecisionLog()
95-
{
96-
return $this->decisionLog;
97-
}
98-
}
22+
class_exists(TraceableAccessDecisionManager::class);
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
namespace Symfony\Component\Security\Core\Authorization;
13+
14+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15+
16+
/**
17+
* Decorates the original AccessDecisionManager class to log information
18+
* about the security voters and the decisions made by them.
19+
*
20+
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
21+
*
22+
* @internal
23+
*/
24+
class TraceableAccessDecisionManager implements AccessDecisionManagerInterface
25+
{
26+
private $manager;
27+
private $strategy;
28+
private $voters = array();
29+
private $decisionLog = array();
30+
31+
public function __construct(AccessDecisionManagerInterface $manager)
32+
{
33+
$this->manager = $manager;
34+
35+
if ($this->manager instanceof AccessDecisionManager) {
36+
// The strategy is stored in a private property of the decorated service
37+
$reflection = new \ReflectionProperty(AccessDecisionManager::class, 'strategy');
38+
$reflection->setAccessible(true);
39+
$this->strategy = $reflection->getValue($manager);
40+
}
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function decide(TokenInterface $token, array $attributes, $object = null)
47+
{
48+
$result = $this->manager->decide($token, $attributes, $object);
49+
50+
$this->decisionLog[] = array(
51+
'attributes' => $attributes,
52+
'object' => $object,
53+
'result' => $result,
54+
);
55+
56+
return $result;
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function setVoters(array $voters)
63+
{
64+
if (!method_exists($this->manager, 'setVoters')) {
65+
return;
66+
}
67+
68+
$this->voters = $voters;
69+
$this->manager->setVoters($voters);
70+
}
71+
72+
/**
73+
* @return string
74+
*/
75+
public function getStrategy()
76+
{
77+
// The $strategy property is misleading because it stores the name of its
78+
// method (e.g. 'decideAffirmative') instead of the original strategy name
79+
// (e.g. 'affirmative')
80+
return null === $this->strategy ? '-' : strtolower(substr($this->strategy, 6));
81+
}
82+
83+
/**
84+
* @return array
85+
*/
86+
public function getVoters()
87+
{
88+
return $this->voters;
89+
}
90+
91+
/**
92+
* @return array
93+
*/
94+
public function getDecisionLog()
95+
{
96+
return $this->decisionLog;
97+
}
98+
}
99+
100+
class_alias(TraceableAccessDecisionManager::class, DebugAccessDecisionManager::class);

Core/Tests/Authorization/DebugAccessDecisionManagerTest.php renamed to Core/Tests/Authorization/TraceableAccessDecisionManagerTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313

1414
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
1515
use Symfony\Component\Security\Core\Authorization\DebugAccessDecisionManager;
16+
use Symfony\Component\Security\Core\Authorization\TraceableAccessDecisionManager;
1617
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1718

18-
class DebugAccessDecisionManagerTest extends \PHPUnit_Framework_TestCase
19+
class TraceableAccessDecisionManagerTest extends \PHPUnit_Framework_TestCase
1920
{
2021
/**
2122
* @dataProvider provideObjectsAndLogs
2223
*/
2324
public function testDecideLog($expectedLog, $object)
2425
{
25-
$adm = new DebugAccessDecisionManager(new AccessDecisionManager());
26+
$adm = new TraceableAccessDecisionManager(new AccessDecisionManager());
2627
$adm->decide($this->getMockBuilder(TokenInterface::class)->getMock(), array('ATTRIBUTE_1'), $object);
2728

2829
$this->assertSame($expectedLog, $adm->getDecisionLog());
@@ -40,4 +41,13 @@ public function provideObjectsAndLogs()
4041
yield array(array(array('attributes' => array('ATTRIBUTE_1'), 'object' => $x = array(), 'result' => false)), $x);
4142
yield array(array(array('attributes' => array('ATTRIBUTE_1'), 'object' => $object, 'result' => false)), $object);
4243
}
44+
45+
public function testDebugAccessDecisionManagerAliasExistsForBC()
46+
{
47+
$adm = new TraceableAccessDecisionManager(new AccessDecisionManager());
48+
49+
if (!$adm instanceof DebugAccessDecisionManager) {
50+
$this->fail('For BC, TraceableAccessDecisionManager must be an instance of DebugAccessDecisionManager');
51+
}
52+
}
4353
}

0 commit comments

Comments
 (0)