Skip to content

Instantly share code, notes, and snippets.

@jenkoian
Last active February 3, 2016 13:58
Show Gist options
  • Save jenkoian/731628f51b1ea89002e9 to your computer and use it in GitHub Desktop.
Save jenkoian/731628f51b1ea89002e9 to your computer and use it in GitHub Desktop.
Legacy escape route User.php
<?php
namespace Acme\Authentication;
use Acme\Avatar\AvatarInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class User implements UserInterface
{
// ...
/**
* @var array
*/
private $roles;
/**
* @var array
*/
private $legacyRoles;
/**
* @param string $username
* @param null|string $firstName
* @param null|string $lastName
* @param null|AvatarInterface $avatar
* @param array $legacyRoles
*/
public function __construct(
$username,
$firstName = null,
$lastName = null,
AvatarInterface $avatar = null,
array $legacyRoles = array()
) {
$this->username = $username;
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->avatar = $avatar;
$this->legacyRoles = $legacyRoles;
$this->roles = array();
$this->mapRoles();
}
/**
* Map legacy roles to symfony roles. Legacy roles are in following format:
* ['moderator' => '1']
*/
private function mapRoles()
{
if (empty($this->legacyRoles)) {
return;
}
foreach ($this->legacyRoles as $roleName => $roleValue) {
if ($roleValue === '1') {
$this->roles[] = 'ROLE_' . strtoupper($roleName);
}
}
}
/**
* {@inheritdoc}
*/
public function getRoles()
{
return $this->roles;
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment