我正在研究Symfony 2.0.16
我的UserProvider中有getRoles方法
public function getRoles()
{
/**
* @var \Doctrine\Common\Collections\ArrayCollection $rol
*/
return $this->rol->toArray();
}
Run Code Online (Sandbox Code Playgroud)
我的Rol实体有角色接口
class Rol implements \Symfony\Component\Security\Core\Role\RoleInterface
//...
public function getRole()
{
return $this->getName();
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试登录时,我收到以下错误
致命错误:在第57行的C:\ Users\julian\Code\parqueadero\vendor\symfony\src\Symfony\Bundle\SecurityBundle\DataCollector\SecurityDataCollector.php中的非对象上调用成员函数getRole()
读取类SecurityDataCollector,Closure抛出错误
array_map(function ($role){ return $role->getRole();}, $token->getRoles()
Run Code Online (Sandbox Code Playgroud)
现在我把它改成了
array_map(function ($role){ var_dump($role); return $role->getRole();}, $token->getRoles()
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,$role是一个对象Rol,但我无法理解为什么我得到错误.
rkm*_*max 14
我发现解决方案的问题是PHP 5.4中的一个bug(php我用的)序列化方法 github用户yoannch提出这个解决方案,是serialize/unserialize用json_encode/json_decode方法覆盖方法
class User implements \Serializable
//...
/**
* Serializes the content of the current User object
* @return string
*/
public function serialize()
{
return \json_encode(
array($this->username, $this->password, $this->salt,
$this->rol, $this->id));
}
/**
* Unserializes the given string in the current User object
* @param serialized
*/
public function unserialize($serialized)
{
list($this->username, $this->password, $this->salt,
$this->rol, $this->id) = \json_decode(
$serialized);
}
Run Code Online (Sandbox Code Playgroud)
只需要更改正确的名称属性
Mic*_*elB 11
我有同样的问题(Windows,PHP 5.4.5),更新到5.4.7,它仍然无法正常工作.尽管如此,我提出了一种需要较少维护的解决方法(当您按照上述文章中的描述覆盖序列化功能时,在添加/删除字段时,您必须使它们保持最新).到目前为止它对我有用,我希望我可能忘记的解决方法没有其他问题.只需改变用户的getRoles()功能:
/**
* @inheritDoc
*/
public function getRoles()
{
$roles = array();
foreach ($this->userRoles as $role) {
$roles[] = $role->getRole();
}
return $roles;
}
Run Code Online (Sandbox Code Playgroud)
请注意,$role->getRole()将角色名称返回为String(例如ROLE_ADMIN).
解决这个问题很简单.与您User和Role对象的循环引用相关的所有问题.所以你不必序列化User::$roles和Role::$users字段.
看看Symfony\Component\Security\Core\Authentication\Token\AbstractToken::__construct()和Symfony\Component\Security\Core\Authentication\Token\AbstractToken::serialize().
您如何看待,Symfony通过UserInterface::getRoles()在序列化之前调用来获取用户的角色.并序列化User和Roles单独.
您必须实现\Serializable接口User和Role实体.
/**
* Acme\Bundle\UserBundle\Entity\User
*
* @ORM\Table(name="`user`")
* @ORM\Entity(repositoryClass="Acme\Bundle\UserBundle\Entity\UserRepository")
*/
class User implements AdvancedUserInterface, EquatableInterface, \Serializable
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $username
*
* @ORM\Column(type="string", length=30, unique=true)
*/
private $username;
/**
* @var string $email
*
* @ORM\Column(type="string", length=100, unique=true)
*/
private $email;
/**
* @var string $salt
*
* @ORM\Column(type="string", length=40)
*/
private $salt;
/**
* @var string $password
*
* @ORM\Column(type="string", length=128)
*/
private $password;
/**
* @var boolean $isActive
*
* @ORM\Column(type="boolean")
*/
private $isActive;
/**
* User's roles. (Owning Side)
*
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
*/
private $roles;
// .....
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
/*
* ! Don't serialize $roles field !
*/
return \serialize(array(
$this->id,
$this->username,
$this->email,
$this->salt,
$this->password,
$this->isActive
));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->email,
$this->salt,
$this->password,
$this->isActive
) = \unserialize($serialized);
}
}
Run Code Online (Sandbox Code Playgroud)
/**
* Acme\Bundle\UserBundle\Entity\Role
*
* @ORM\Table(name="role")
* @ORM\Entity
*
*/
class Role implements RoleInterface, \Serializable
{
/**
* @var integer $id
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $role
*
* @ORM\Column(name="role", type="string", length=20, unique=true)
*/
private $role;
/**
* Users in group (Inverse Side)
*
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*/
private $users;
// .....
/**
* @see \Serializable::serialize()
*/
public function serialize()
{
/*
* ! Don't serialize $users field !
*/
return \serialize(array(
$this->id,
$this->role
));
}
/**
* @see \Serializable::unserialize()
*/
public function unserialize($serialized)
{
list(
$this->id,
$this->role
) = \unserialize($serialized);
}
}
Run Code Online (Sandbox Code Playgroud)
所有都将正确序列化/反序列化.
请访问https://github.com/symfony/symfony/issues/3691查看讨论
另见:http: //docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/entities-in-session.html#serializing-entity-into-the-session
| 归档时间: |
|
| 查看次数: |
8079 次 |
| 最近记录: |