【问题标题】:Sonata-Project's Google Authenticator: Undefined Method "getTwoStepVerificationCode" from Application\Sonata\UserBundle\Entity\UserSonata-Project 的 Google Authenticator:来自 Application\Sonata\UserBundle\Entity\User 的未定义方法“getTwoStepVerificationCode”
【发布时间】:2025-12-25 06:55:16
【问题描述】:

伙计们!

我目前正在开发一个基于 Symfony 2.8 和一些 Sonata-Project 捆绑包的项目。目标是实现两因素身份验证(也称为两步验证)。我看到 Sonata-Project 有一个用于实现此类功能的包并安装了它。然后根据sonata's documentation 配置app/config/config.yml 文件,但是当我尝试从登录表单登录我的仪表板时,我这样做没有任何进一步的障碍(没有 2-Factor Authentication Prompt)。在这里,在 *.com 上,我看到了一些与我的问题(不完全)相关的答案,建议运行 php app/console sonata:user:two-step-verification *ADMIN_USER_NAME*。我试图在我的终端中运行这个命令,但是抛出了这个异常:

[Symfony\Component\Debug\Exception\UndefinedMethodException]
Attempted to call an undefined method named "getTwoStepVerificationCode" of class 
"Application\Sonata\UserBundle\Entity\User".

这里似乎有什么问题?为什么这种方法不存在?我该如何解决这个问题?

P.S.:我尝试将此 Google-Authenticator 包与 Soanata-Project 的 UserBundle 结合使用为it is "a feature" (if I may call it such) of the latter

【问题讨论】:

    标签: sonata-admin sonata symfony-sonata symfony-2.8 sonata-user-bundle


    【解决方案1】:

    您的应用程序是否正确扩展Sonata\UserBundle\Entity\BaseUser?因为如果不是,您将遇到此问题。检查你的班级Application\Sonata\UserBundle\Entity\User

    【讨论】:

    • Application\Sonata\UserBundle\Entity\User 确实缺少 getTwoStepVerificationCode 方法。如何设置它以及如何编辑实体,以便这样的列 (TwoStepVerificationCode) 存在于 DB 中的 Users 表中?
    • @EmilAvramov 您的用户实体必须扩展 Sonata\UserBundle\Entity\BaseUser 才能使 TwoStepAuthentication 工作。