2

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

Symfony 版本:symfony2 2.2.2

用户.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()));
}
4

1 回答 1

2

您应该EquatableInterfaceUser类实现:

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);
    }

    // ...
}
于 2013-11-22T12:22:54.883 回答