1

我已经成功切换到用户_switch_user=thomas

但是,以下内容在应该返回 true 时总是返回 false:

        $security = $this->get('security.context');
        $impersonated = $security->isGranted('ROLE_PREVIOUS_ADMIN');
        die(var_dump($impersonated)); // == false

为什么我不能在安全上下文中获得 ROLE_PREVIOUS_ADMIN?

编辑 我登录:

?_switch_user=john@doe.com

自从:

providers:
    main:
        entity:
            property: email

我的参数是默认值:

switch_user: true

我正确切换,这不是问题。

问题是在我模拟之后,用户没有 ROLE_PREVIOUS_ADMIN

4

2 回答 2

1

很可能您还没有实现EquatableInterfaceforUser类:

class User implements UserInterface, EquatableInterface, \Serializable {

    // ...

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

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

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

    // ...
}
于 2013-11-22T12:24:22.623 回答
0
  • 你可以打印转储User对象吗?什么是用户名值?
  • 检查里面config.yml那个模拟参数确实被调用了_switch_user

您可能会在没有实际切换用户的情况下打开页面。

于 2013-11-13T14:28:47.043 回答