Skip to content

Commit

Permalink
Backward compatibility layer for Symfony 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
JJarrie authored and nicolas-grekas committed Jun 10, 2019
1 parent 034f688 commit 7b59842
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 20 deletions.
@@ -0,0 +1,27 @@
<?php

namespace SensioLabs\Connect\Security\Authentication\Token;

use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;

if (method_exists(AbstractToken::class, '__serialize')) {
abstract class AbstractConnectToken extends AbstractToken
{
use SerializationConnectTokenTrait;
}
} else {
abstract class AbstractConnectToken extends AbstractToken
{
public function serialize()
{
return serialize(array($this->apiUser, $this->accessToken, $this->providerKey, $this->scope, parent::serialize()));
}

public function unserialize($str)
{
list($this->apiUser, $this->accessToken, $this->providerKey, $this->scope, $parentStr) = unserialize($str);

parent::unserialize($parentStr);
}
}
}
Expand Up @@ -22,7 +22,7 @@
*
* @author Marc Weistroff <marc.weistroff@sensiolabs.com>
*/
class ConnectToken extends AbstractToken
class ConnectToken extends AbstractConnectToken
{
private $accessToken;
private $providerKey;
Expand Down Expand Up @@ -53,6 +53,10 @@ public function getRoles()
return $this->getUserRoles($user);
}

if (method_exists(AbstractToken::class, 'getRoleNames')) {
return parent::getRoleNames();
}

return parent::getRoles();
}

Expand Down Expand Up @@ -96,31 +100,36 @@ public function getCredentials()
return $this->accessToken;
}

public function serialize()
private function getUserRoles(UserInterface $user)
{
return serialize(array($this->apiUser, $this->accessToken, $this->providerKey, $this->scope, parent::serialize()));
$callBackMethod = 'getObjectUserRole';

if (method_exists(AbstractToken::class, 'getRoleNames')) {
$callBackMethod = 'getStringUserRole';
}

return array_map([$this, $callBackMethod], $user->getRoles());
}

public function unserialize($str)
private function getStringUserRole($role)
{
list($this->apiUser, $this->accessToken, $this->providerKey, $this->scope, $parentStr) = unserialize($str);
if (!is_string($role) && !($role instanceof Role)) {
throw new \InvalidArgumentException(sprintf('$roles must be an array of strings, or Role instances, but got %s.', gettype($role)));
}

parent::unserialize($parentStr);
return (string) $role;
}

private function getUserRoles(UserInterface $user)
private function getObjectUserRole($role)
{
$roles = array();
foreach ($user->getRoles() as $role) {
if (is_string($role)) {
$role = new Role($role);
} elseif (!$role instanceof RoleInterface) {
throw new \InvalidArgumentException(sprintf('$roles must be an array of strings, or RoleInterface instances, but got %s.', gettype($role)));
}

$roles[] = $role;
if (is_string($role)) {
return new Role($role);
}

if (!$role instanceof RoleInterface) {
throw new \InvalidArgumentException(sprintf('$roles must be an array of strings, or RoleInterface instances, but got %s.', gettype($role)));
}

return $roles;
return $role;
}
}
@@ -0,0 +1,17 @@
<?php

namespace SensioLabs\Connect\Security\Authentication\Token;

trait SerializationConnectTokenTrait
{
public function __serialize(): array
{
return array($this->apiUser, $this->accessToken, $this->providerKey, $this->scope, parent::__serialize());
}

public function __unserialize(array $data): void
{
list($this->apiUser, $this->accessToken, $this->providerKey, $this->scope) = $data;
parent::__serialize($data);
}
}
Expand Up @@ -28,8 +28,8 @@ public function testGetRolesWithStringUser()
$roles = $token->getRoles();

$this->assertCount(2, $roles);
$this->assertEquals('ROLE_USER', $roles[0]->getRole());
$this->assertEquals('ROLE_ADMIN', $roles[1]->getRole());
$this->assertEquals('ROLE_USER', is_string($roles[0]) ? $roles[0] : $roles[0]->getRole());
$this->assertEquals('ROLE_ADMIN', is_string($roles[1]) ? $roles[1] : $roles[1]->getRole());
}

public function testGetRolesWithUserInterfaceUser()
Expand All @@ -39,6 +39,6 @@ public function testGetRolesWithUserInterfaceUser()
$roles = $token->getRoles();

$this->assertCount(1, $roles);
$this->assertEquals('ROLE_SINGLE', $roles[0]->getRole());
$this->assertEquals('ROLE_SINGLE', is_string($roles[0]) ? $roles[0] : $roles[0]->getRole());
}
}

0 comments on commit 7b59842

Please sign in to comment.