skip native serialize among child and parent serializable objects
This commit is contained in:
parent
41000f1de0
commit
10256fc4fd
@ -134,10 +134,6 @@ abstract class AbstractToken implements TokenInterface
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param bool $isCalledFromOverridingMethod Must be set to true when called from an overriding method
|
||||
*
|
||||
* @return string|array Returns an array when $isCalledFromOverridingMethod is set to true
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
|
@ -59,7 +59,9 @@ class AnonymousToken extends AbstractToken
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize([$this->secret, parent::serialize()]);
|
||||
$serialized = [$this->secret, parent::serialize(true)];
|
||||
|
||||
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -76,8 +76,6 @@ class PreAuthenticatedToken extends AbstractToken
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param bool $isCalledFromOverridingMethod Must be set to true when called from an overriding method
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
|
@ -94,11 +94,9 @@ class RememberMeToken extends AbstractToken
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize([
|
||||
$this->secret,
|
||||
$this->providerKey,
|
||||
parent::serialize(),
|
||||
]);
|
||||
$serialized = [$this->secret, $this->providerKey, parent::serialize(true)];
|
||||
|
||||
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -91,7 +91,9 @@ class UsernamePasswordToken extends AbstractToken
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize([$this->credentials, $this->providerKey, parent::serialize()]);
|
||||
$serialized = [$this->credentials, $this->providerKey, parent::serialize(true)];
|
||||
|
||||
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,10 +44,9 @@ abstract class AccountStatusException extends AuthenticationException
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize([
|
||||
$this->user,
|
||||
parent::serialize(),
|
||||
]);
|
||||
$serialized = [$this->user, parent::serialize(true)];
|
||||
|
||||
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -38,15 +38,33 @@ class AuthenticationException extends \RuntimeException implements \Serializable
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize([
|
||||
$serialized = [
|
||||
$this->token,
|
||||
$this->code,
|
||||
$this->message,
|
||||
$this->file,
|
||||
$this->line,
|
||||
]);
|
||||
];
|
||||
|
||||
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected function doSerialize($serialized, $isCalledFromOverridingMethod)
|
||||
{
|
||||
if (null === $isCalledFromOverridingMethod) {
|
||||
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 3);
|
||||
$isCalledFromOverridingMethod = isset($trace[2]['function'], $trace[2]['object']) && 'serialize' === $trace[2]['function'] && $this === $trace[2]['object'];
|
||||
}
|
||||
|
||||
return $isCalledFromOverridingMethod ? $serialized : serialize($serialized);
|
||||
}
|
||||
|
||||
public function unserialize($str)
|
||||
@ -57,7 +75,7 @@ class AuthenticationException extends \RuntimeException implements \Serializable
|
||||
$this->message,
|
||||
$this->file,
|
||||
$this->line
|
||||
) = unserialize($str);
|
||||
) = \is_array($str) ? $str : unserialize($str);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -60,11 +60,9 @@ class CustomUserMessageAuthenticationException extends AuthenticationException
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize([
|
||||
parent::serialize(),
|
||||
$this->messageKey,
|
||||
$this->messageData,
|
||||
]);
|
||||
return serialize([parent::serialize(true), $this->messageKey, $this->messageData]);
|
||||
|
||||
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -54,10 +54,9 @@ class UsernameNotFoundException extends AuthenticationException
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize([
|
||||
$this->username,
|
||||
parent::serialize(),
|
||||
]);
|
||||
$serialized = [$this->username, parent::serialize(true)];
|
||||
|
||||
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,11 +44,13 @@ class ConcreteToken extends AbstractToken
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isCalledFromOverridingMethod Must be set to true when called from an overriding method
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize([$this->credentials, parent::serialize()]);
|
||||
$serialized = [$this->credentials, parent::serialize(true)];
|
||||
|
||||
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
|
||||
}
|
||||
|
||||
public function unserialize($serialized)
|
||||
|
@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\Security\Core\Tests\Exception;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
|
||||
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
|
||||
|
||||
class CustomUserMessageAuthenticationExceptionTest extends TestCase
|
||||
@ -24,4 +25,18 @@ class CustomUserMessageAuthenticationExceptionTest extends TestCase
|
||||
$this->assertEquals(['foo' => true], $e->getMessageData());
|
||||
$this->assertEquals('SAFE MESSAGE', $e->getMessage());
|
||||
}
|
||||
|
||||
public function testSharedSerializedData()
|
||||
{
|
||||
$token = new AnonymousToken('foo', 'bar');
|
||||
|
||||
$exception = new CustomUserMessageAuthenticationException();
|
||||
$exception->setToken($token);
|
||||
$exception->setSafeMessage('message', ['token' => $token]);
|
||||
|
||||
$processed = unserialize(serialize($exception));
|
||||
$this->assertEquals($token, $processed->getToken());
|
||||
$this->assertEquals($token, $processed->getMessageData()['token']);
|
||||
$this->assertSame($processed->getToken(), $processed->getMessageData()['token']);
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,9 @@ class PostAuthenticationGuardToken extends AbstractToken implements GuardTokenIn
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize([$this->providerKey, parent::serialize(true)]);
|
||||
$serialized = [$this->providerKey, parent::serialize(true)];
|
||||
|
||||
return $this->doSerialize($serialized, \func_num_args() ? \func_get_arg(0) : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user