summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabien Potencier <fabien.potencier@gmail.com>2015-09-28 13:14:38 +0200
committerFabien Potencier <fabien.potencier@gmail.com>2015-09-28 13:14:38 +0200
commit16223cbf326eee2a9fff59f765c218ff028e9330 (patch)
treeece9d42f9b716a90d1696b40e2e5a1bb65fac37f
parent085fdff5afa56b088fe298d08940afd2900d7d29 (diff)
parent123c8df26a95bfb86c1dacea02778b1aa8432fbe (diff)
downloadsymfony-security-16223cbf326eee2a9fff59f765c218ff028e9330.zip
symfony-security-16223cbf326eee2a9fff59f765c218ff028e9330.tar.gz
symfony-security-16223cbf326eee2a9fff59f765c218ff028e9330.tar.bz2
Merge branch '2.8'
* 2.8: [Finder] simplified code Fix tests in 2.8 [Validator] Sync polish translation file Adding a class to make it easier to set custom authentication error messages Readd the correct tests
-rw-r--r--Core/Exception/CustomUserMessageAuthenticationException.php79
-rw-r--r--Core/Tests/Authorization/Voter/AbstractVoterTest.php107
-rw-r--r--Core/Tests/Exception/CustomUserMessageAuthenticationExceptionTest.php26
3 files changed, 149 insertions, 63 deletions
diff --git a/Core/Exception/CustomUserMessageAuthenticationException.php b/Core/Exception/CustomUserMessageAuthenticationException.php
new file mode 100644
index 0000000..9f5071f
--- /dev/null
+++ b/Core/Exception/CustomUserMessageAuthenticationException.php
@@ -0,0 +1,79 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Core\Exception;
+
+/**
+ * An authentication exception where you can control the message shown to the user.
+ *
+ * Be sure that the message passed to this exception is something that
+ * can be shown safely to your user. In other words, avoid catching
+ * other exceptions and passing their message directly to this class.
+ *
+ * @author Ryan Weaver <ryan@knpuniversity.com>
+ */
+class CustomUserMessageAuthenticationException extends AuthenticationException
+{
+ private $messageKey;
+
+ private $messageData = array();
+
+ public function __construct($message = '', array $messageData = array(), $code = 0, \Exception $previous = null)
+ {
+ parent::__construct($message, $code, $previous);
+
+ $this->setSafeMessage($message, $messageData);
+ }
+
+ /**
+ * Set a message that will be shown to the user.
+ *
+ * @param string $messageKey The message or message key
+ * @param array $messageData Data to be passed into the translator
+ */
+ public function setSafeMessage($messageKey, array $messageData = array())
+ {
+ $this->messageKey = $messageKey;
+ $this->messageData = $messageData;
+ }
+
+ public function getMessageKey()
+ {
+ return $this->messageKey;
+ }
+
+ public function getMessageData()
+ {
+ return $this->messageData;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function serialize()
+ {
+ return serialize(array(
+ parent::serialize(),
+ $this->messageKey,
+ $this->messageData,
+ ));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function unserialize($str)
+ {
+ list($parentData, $this->messageKey, $this->messageData) = unserialize($str);
+
+ parent::unserialize($parentData);
+ }
+}
diff --git a/Core/Tests/Authorization/Voter/AbstractVoterTest.php b/Core/Tests/Authorization/Voter/AbstractVoterTest.php
index ea72e75..0fddd88 100644
--- a/Core/Tests/Authorization/Voter/AbstractVoterTest.php
+++ b/Core/Tests/Authorization/Voter/AbstractVoterTest.php
@@ -13,42 +13,55 @@ namespace Symfony\Component\Security\Core\Tests\Authorization\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter;
+use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
-/**
- * @author Roman Marintšenko <inoryy@gmail.com>
- */
class AbstractVoterTest extends \PHPUnit_Framework_TestCase
{
- private $token;
+ protected $token;
protected function setUp()
{
- $tokenMock = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
- $tokenMock
- ->expects($this->any())
- ->method('getUser')
- ->will($this->returnValue('user'));
+ $this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
+ }
+
+ public function getTests()
+ {
+ return array(
+ array(array('EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if attribute and class are supported and attribute grants access'),
+ array(array('CREATE'), VoterInterface::ACCESS_DENIED, new \stdClass(), 'ACCESS_DENIED if attribute and class are supported and attribute does not grant access'),
+
+ array(array('DELETE', 'EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if one attribute is supported and grants access'),
+ array(array('DELETE', 'CREATE'), VoterInterface::ACCESS_DENIED, new \stdClass(), 'ACCESS_DENIED if one attribute is supported and denies access'),
+
+ array(array('CREATE', 'EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if one attribute grants access'),
+
+ array(array('DELETE'), VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if no attribute is supported'),
+
+ array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, $this, 'ACCESS_ABSTAIN if class is not supported'),
- $this->token = $tokenMock;
+ array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, null, 'ACCESS_ABSTAIN if object is null'),
+
+ array(array(), VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if no attributes were provided'),
+ );
}
/**
- * @dataProvider getData
+ * @dataProvider getTests
*/
- public function testVote($expectedVote, $object, $attributes, $message)
+ public function testVote(array $attributes, $expectedVote, $object, $message)
{
- $voter = new VoterFixture();
+ $voter = new AbstractVoterTest_Voter();
$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
}
/**
- * @dataProvider getData
+ * @dataProvider getTests
* @group legacy
*/
- public function testVoteUsingDeprecatedIsGranted($expectedVote, $object, $attributes, $message)
+ public function testVoteLegacy(array $attributes, $expectedVote, $object, $message)
{
- $voter = new DeprecatedVoterFixture();
+ $voter = new AbstractVoterTest_LegacyVoter();
$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message);
}
@@ -59,86 +72,54 @@ class AbstractVoterTest extends \PHPUnit_Framework_TestCase
*/
public function testNoOverriddenMethodsThrowsException()
{
- $voter = new DeprecatedVoterNothingImplementedFixture();
- $voter->vote($this->token, new ObjectFixture(), array('foo'));
- }
-
- public function getData()
- {
- return array(
- array(AbstractVoter::ACCESS_ABSTAIN, null, array(), 'ACCESS_ABSTAIN for null objects'),
- array(AbstractVoter::ACCESS_ABSTAIN, new UnsupportedObjectFixture(), array(), 'ACCESS_ABSTAIN for objects with unsupported class'),
- array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array(), 'ACCESS_ABSTAIN for no attributes'),
- array(AbstractVoter::ACCESS_ABSTAIN, new ObjectFixture(), array('foobar'), 'ACCESS_ABSTAIN for unsupported attributes'),
- array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foo'), 'ACCESS_GRANTED if attribute grants access'),
- array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('bar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'),
- array(AbstractVoter::ACCESS_GRANTED, new ObjectFixture(), array('foobar', 'foo'), 'ACCESS_GRANTED if *at least one* attribute grants access'),
- array(AbstractVoter::ACCESS_DENIED, new ObjectFixture(), array('bar', 'baz'), 'ACCESS_DENIED for if no attribute grants access'),
- );
+ $voter = new AbstractVoterTest_NothingImplementedVoter();
+ $voter->vote($this->token, new \stdClass(), array('EDIT'));
}
}
-class VoterFixture extends AbstractVoter
+class AbstractVoterTest_Voter extends AbstractVoter
{
- protected function getSupportedClasses()
- {
- return array(
- 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
- );
- }
-
- protected function getSupportedAttributes()
+ protected function voteOnAttribute($attribute, $object, TokenInterface $token)
{
- return array('foo', 'bar', 'baz');
+ return 'EDIT' === $attribute;
}
- protected function voteOnAttribute($attribute, $object, TokenInterface $token)
+ protected function supports($attribute, $class)
{
- return $attribute === 'foo';
+ return $this->isClassInstanceOf($class, 'stdClass')
+ && in_array($attribute, array('EDIT', 'CREATE'));
}
}
-class DeprecatedVoterFixture extends AbstractVoter
+class AbstractVoterTest_LegacyVoter extends AbstractVoter
{
protected function getSupportedClasses()
{
- return array(
- 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
- );
+ return array('AbstractVoterTest_Object');
}
protected function getSupportedAttributes()
{
- return array('foo', 'bar', 'baz');
+ return array('EDIT', 'CREATE');
}
protected function isGranted($attribute, $object, $user = null)
{
- return $attribute === 'foo';
+ return 'EDIT' === $attribute;
}
}
-class DeprecatedVoterNothingImplementedFixture extends AbstractVoter
+class AbstractVoterTest_NothingImplementedVoter extends AbstractVoter
{
protected function getSupportedClasses()
{
- return array(
- 'Symfony\Component\Security\Core\Tests\Authorization\Voter\ObjectFixture',
- );
+ return array('AbstractVoterTest_Object');
}
protected function getSupportedAttributes()
{
- return array('foo', 'bar', 'baz');
+ return array('EDIT', 'CREATE');
}
// this is a bad voter that hasn't overridden isGranted or voteOnAttribute
}
-
-class ObjectFixture
-{
-}
-
-class UnsupportedObjectFixture
-{
-}
diff --git a/Core/Tests/Exception/CustomUserMessageAuthenticationExceptionTest.php b/Core/Tests/Exception/CustomUserMessageAuthenticationExceptionTest.php
new file mode 100644
index 0000000..408dd2a
--- /dev/null
+++ b/Core/Tests/Exception/CustomUserMessageAuthenticationExceptionTest.php
@@ -0,0 +1,26 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Core\Tests\Exception;
+
+use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
+
+class CustomUserMessageAuthenticationExceptionTest extends \PHPUnit_Framework_TestCase
+{
+ public function testConstructWithSAfeMessage()
+ {
+ $e = new CustomUserMessageAuthenticationException('SAFE MESSAGE', array('foo' => true));
+
+ $this->assertEquals('SAFE MESSAGE', $e->getMessageKey());
+ $this->assertEquals(array('foo' => true), $e->getMessageData());
+ $this->assertEquals('SAFE MESSAGE', $e->getMessage());
+ }
+}