Skip to content

Commit af25c3b

Browse files
committed
Adding a class to make it easier to set custom authentication error messages
1 parent 4b94274 commit af25c3b

File tree

2 files changed

+105
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)