Last active
February 3, 2016 13:58
-
-
Save jenkoian/731628f51b1ea89002e9 to your computer and use it in GitHub Desktop.
Legacy escape route User.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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