切换后Symfony2用户开关缺少ROLE_PREVIOUS_ADMIN

时间:2013-06-11 06:58:41

标签: php symfony

当我以管理员身份切换到用户site.loc/?_switch_user=user@email.com时,我没有获得角色ROLE_PREVIOUS_ADMIN。 我不确定我应该向你展示哪些代码..

Symfony版本:symfony2 2.2.2

user.php的

/**
 * @see \Serializable::serialize()
 */
public function serialize()
{
    return serialize(array(
        $this->email
    ));
}

/**
 * @see \Serializable::unserialize()
 */
public function unserialize($serialized)
{
    list (
        $this->email
    ) = unserialize($serialized);
}

public function isAccountNonExpired()
{
    return true;
}

public function isAccountNonLocked()
{
    return true;
}

public function isCredentialsNonExpired()
{
    return true;
}

public function isEnabled()
{
    return $this->isActive;
}

/**
 * @inheritDoc
 */
public function eraseCredentials()
{
}

public function isEqualTo(UserInterface $user)
{
    return
        md5($user->getUsername()) == md5($this->getUsername()) &&
        md5(serialize($user->getGroups())) == md5(serialize($this->getGroups()));
}

1 个答案:

答案 0 :(得分:2)

您应该为User类实施EquatableInterface

class User implements UserInterface, EquatableInterface, \Serializable {

    // ...

    public function isEqualTo(UserInterface $user) {
        return $this->username === $user->getUsername();
    }

    public function serialize() {
        return serialize(array(
            $this->id,
            $this->username,
        ));
    }

    public function unserialize($serialized) {
        list($this->id, $this->username) = unserialize($serialized);
    }

    // ...
}