dongnaigu2052 2015-02-20 09:48
浏览 33
已采纳

Symfony2:UsernamePasswordToken在方法getRoles中返回User对象

I constantly get this error after authorization:

UndefinedMethodException in RoleHierarchy.php line 43: Attempted to call method "getRole" on class "path\to\User". Did you mean to call e.g. "getRoles"?

User (without other fields and methods):

class User implements UserInterface {
    /**
     * @ORM\ManyToMany(targetEntity = "path\to\Role", inversedBy = "users")
     * @ORM\JoinTable(name = "user_roles", joinColumns = {@ORM\JoinColumn(name = "userid", referencedColumName = "id")}, inverseJoinColumns = {@ORM\JoinColumn(name = "roleid", referencedColumnName = "id")})
     */
    protected $roles;

    public function getRoles() { return $this->roles->toArray(); }
}

Role (without other fields and methods):

class Role implements RoleInterface {
    /**
     * @ORM\Column(name = "role", type = "string", unique = true)
     */
    protected $role;

    /**
     * @ORM\ManyToMany(targetEntity = "path\to\User", mappedBy = "roles")
     */
    protected $users;

    public function getRole() { return $this->role; }
}

security.yml:

security:
  encoders:
    path\to\User: bcrypt
  role_hierarchy:
    ROLE_ADMIN: ROLE_USER
    ROLE_SUPER_ADMIN: [ROLE_ADMIN]
  providers:
    main_provider:
      entity: { class: path\to\User, property: username }
  firewalls:
    dev:
      pattern: ^/(_(profiler|wdt)|css|images|js)/
      security: false
    main_signin:
      pattern: ^/signin$
      anonymous: ~
    main:
      pattern: ^/
      form_login:
        provider: main_provider
        login_path: security_signin
        check_path: security_signcheck
        username_parameter: signin[username]
        password_parameter: signin[password]
        target_path_parameter: signin[targetpath]
        default_target_path: main_index
        remember_me: true
      remember_me:
        key: "%secret%"
        lifetime: 86400
        path: /
        remember_me_parameter: signin[rememberme]
      logout:
        path: security_signout
        target: security_signin
  access_control:
    - { path: ^/signin$ roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: ROLE_USER }

I tried to add a method getRole to the User to see what happens, and got this:

UndefinedMethodException in RoleHierarchy.php line 43: Attempted to call method "getRole" on class "DateTime".

For some reason UsernamePasswordToken->getRoles() does not return an array of roles but another object is returned. I don't know what I'm doing wrong... I tried to clear a dev cache but it didn't help.

  • 写回答

1条回答 默认 最新

  • dongpao1905 2015-07-20 11:31
    关注

    I jast added Serializable interface to the User, and error disappeared.

    class User implements UserInterface, \Serializable {
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c