Skip to content

Commit 556abe0

Browse files
committed
Adding a class to make it easier to set custom authentication error messages
1 parent 1d5557f commit 556abe0

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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\Exception;
13+
use Exception;
14+
15+
/**
16+
* An authentication exception where you can control the message shown to the user
17+
*
18+
* Be sure that the message passed to this exception is something that
19+
* can be shown safely to your user. In other words, avoid catching
20+
* other exceptions and passing their message directly to this class.
21+
*
22+
* @author Ryan Weaver <ryan@knpuniversity.com>
23+
*/
24+
class SafeMessageAuthenticationException extends AuthenticationException
25+
{
26+
private $messageKey;
27+
28+
private $messageData = array();
29+
30+
public function __construct($message = "", array $messageData = array(), $code = 0, Exception $previous = null)
31+
{
32+
parent::__construct($message, $code, $previous);
33+
34+
$this->setSafeMessage($message, $messageData);
35+
}
36+
37+
38+
/**
39+
* Set a message that will be shown to the user.
40+
*
41+
* @param string $messageKey The message or message key
42+
* @param array $messageData Data to be passed into the translator
43+
*/
44+
public function setSafeMessage($messageKey, array $messageData = array())
45+
{
46+
$this->messageKey = $messageKey;
47+
$this->messageData = $messageData;
48+
}
49+
50+
public function getMessageKey()
51+
{
52+
return null !== $this->messageKey ? $this->messageKey : parent::getMessageKey();
53+
}
54+
55+
public function getMessageData()
56+
{
57+
return $this->messageData;
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function serialize()
64+
{
65+
return serialize(array(
66+
parent::serialize(),
67+
$this->messageKey,
68+
$this->messageData,
69+
));
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function unserialize($str)
76+
{
77+
list($parentData, $this->messageKey, $this->messageData) = unserialize($str);
78+
79+
parent::unserialize($parentData);
80+
}
81+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Tests\Exception;
13+
14+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
15+
use Symfony\Component\Security\Core\Exception\SafeMessageAuthenticationException;
16+
17+
class SafeMessageAuthenticationExceptionTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testConstructWithSAfeMessage()
20+
{
21+
$e = new SafeMessageAuthenticationException('SAFE MESSAGE', array('foo' => true));
22+
23+
$this->assertEquals('SAFE MESSAGE', $e->getMessageKey());
24+
$this->assertEquals(array('foo' => true), $e->getMessageData());
25+
$this->assertEquals('SAFE MESSAGE', $e->getMessage());
26+
}
27+
28+
/**
29+
* @dataProvider provideKeysAndData
30+
*/
31+
public function testMessageKeyAndData($key, array $data, $expectedKey, array $expectedData)
32+
{
33+
$e = new SafeMessageAuthenticationException();
34+
$e->setSafeMessage($key, $data);
35+
36+
$this->assertEquals($expectedKey, $e->getMessageKey());
37+
$this->assertEquals($expectedData, $e->getMessageData());
38+
}
39+
40+
public function provideKeysAndData()
41+
{
42+
// the parent exception
43+
$authenticationException = new AuthenticationException();
44+
45+
$tests = array();
46+
// null key means the parent is used
47+
$tests[] = array(null, array(), $authenticationException->getMessageKey(), array());
48+
$tests[] = array('', array(), '', array());
49+
$tests[] = array('MESSAGE', array('bar' => false), 'MESSAGE', array('bar' => false));
50+
51+
return $tests;
52+
}
53+
}

0 commit comments

Comments
 (0)