2

所以基本上,我正在尝试在 Symfony 中使用 FOSUserBundle 实现 FOSFacebookBundle。我遵循了 FOSFacebookBundle 2.0 分支的自述文件,但现在我遇到了这个错误。任何想法 ?

错误 :

Catchable Fatal Error: Argument 2 passed to FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider::__construct()
must implement interface Symfony\Component\Security\Core\User\UserProviderInterface,
instance of FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider given,
called in /var/www/Symfony/app/cache/dev/appDevDebugProjectContainer.php on line 2434
and defined in /var/www/Symfony/vendor/bundles/FOS/FacebookBundle/Security/Authentication/Provider/FacebookProvider.php line 37

security.yml(安全部分):

factories:
    - "%kernel.root_dir%/../vendor/bundles/FOS/FacebookBundle/Resources/config/security_factories.xml"

providers:
    chain_provider:
        providers: [fos_userbundle, fos_facebook]
    fos_userbundle:
        id: fos_user.user_manager
    fos_facebook:
        id: fos_facebook.auth 

encoders:
    "FOS\UserBundle\Model\UserInterface": sha512

firewalls:
    main:
        pattern:        .*
        fos_facebook:
            provider: fos_facebook 
            app_url: "to_do"
            server_url: "to_do"
            default_target_path: /
        form_login:
            provider:       fos_userbundle
            login_path:     /login
            use_forward:    false
            check_path:     /login_check
            failure_path:   null
            default_target_path: /
        logout:
            path:   /logout
            target: /
        anonymous:    true

access_control:
    - { path: /admin/.*, role: ROLE_ADMIN }
    - { path: /video/.*, role: ROLE_USER }
    - { path: /$, role: IS_AUTHENTICATED_ANONYMOUSLY }

config.yml 的相关部分:

fos_user:
    db_driver:     orm
    firewall_name: main
    use_listener: false
    user_class:   Projectname\UserBundle\Entity\User
    registration:
        form:
            type: projectname_user_registration
    service:
        user_manager: projectname_user.my_user_manager

fos_facebook:
    file:   %kernel.root_dir%/../vendor/facebook/src/base_facebook.php
    alias:  facebook
    app_id: %projectname_bundle.facebook.app_id%
    secret: %projectname_bundle.facebook.secret%
    cookie: true
    permissions: %projectname_bundle.facebook.perms%

MyUserManager.php(已修改,因此我可以使用电子邮件地址作为 FOSUserbundle 的用户名。不知道它是否真的相关,但错误告诉我传递给 FacebookProvider 构造函数的第二个参数有问题,并且这个参数应该是成为用户管理器):

<?php

namespace ProjectName\UserBundle\Model;

use FOS\UserBundle\Entity\UserManager;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;

class MyUserManager extends UserManager
{
    public function loadUserByUsername($username)
    {
        $user = $this->findUserByUsernameOrEmail($username);

        if (!$user) {
            throw new UsernameNotFoundException(sprintf('No user with email "%s" was found.', $username));
        }

        return $user;
    }
}
4

1 回答 1

0

FacebookProvider ( FOS\FacebookBundle\Security\Authentication\Provider\FacebookProvider )实现了AuthenticationProviderInterface而不是UserProviderInterface。它不是一个用户提供者,而是一个身份验证提供者,而 fos_facebook 需要一个用户提供者。

因此,您应该定义一个自定义用户提供程序在此处进行了说明(安装步骤 6 和“使用 FOS\UserBundle 的示例自定义用户提供程序”)

1) 安全性.yml

添加这个:

# application/config/config.yml
security:
    providers:
        ...
        # choose the provider name freely
        my_fos_facebook_provider:
            id: my.facebook.user
        ...
firewalls:
    main:
        pattern:      .*
        fos_facebook:
            ...
            provider: my_fos_facebook_provider
            ...

2) 配置.yml

添加这个:

services:
    my.facebook.user:
        class: Acme\MyBundle\Security\User\Provider\FacebookProvider
        arguments:
            facebook: "@fos_facebook.api"
            userManager: "@fos_user.user_manager"
            validator: "@validator"
            container: "@service_container"

3) FacebookProvider.php

在 Acme/MyBundle/Security/User/Provider/ 中创建文件 FacebookProvider.php

Readme.md末尾的代码

4) 用户.php

通过添加一些与 FB 相关的字段(ID、名字、姓氏等)来编辑您的用户实体

Readme.md末尾的代码

于 2012-05-18T07:20:13.067 回答