2011-08-31 62 views
3

我在實施FOSFacebookBundle時遇到了大問題。FOSFacebookBundle不會撥打自​​定義提供商

我跟着文檔,並具有以下情況: *當用戶點擊登錄一個彈出窗口 *後,用戶同意該應用程序,FB按鈕被改變(註銷)

然而,我的定義提供沒有被調用(只有一個構造函數被調用) - 是的,我使用了一個noobish調試方法(使用類方法的名稱創建空文件:-))。

有人有什麼建議,爲什麼?有小費嗎?

編輯
經過一段時間的試圖解決這個問題,我覺得我迷路了。

再次,這是我的配置:

應用程序/配置/ config.yml:

fos_facebook: 
    file: %kernel.root_dir%/../vendor/facebook/src/base_facebook.php 
    alias: facebook 
    app_id: xxx 
    secret: xxx 
    cookie: true 
    permissions: [email, user_location] 

應用程序/配置/ routing.yml中:

_security_login: 
    pattern: /login 
    defaults: { _controller: TestBundle:Main:login } 

_security_check: 
    pattern: /login_check 
    defaults: { _controller: TestBundle:Main:loginCheck } 

_security_logout: 
    pattern: /logout 
    defaults: { _controller: TestBundle:Main:logout } 

應用程序/配置/安全.yml

security: 
    factories: 
     -"%kernel.root_dir%/../vendor/bundles/FOS/FacebookBundle/Resources/config/security_factories.xml" 
    providers: 
     my_fos_facebook_provider: 
      id: my.facebook.user 
     fos_userbundle: 
      id: fos_user.user_manager 
firewalls: 
     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 

     main: 
      pattern: ^/ 
      form_login: 
       provider: fos_userbundle 
       login_path: /login 
       check_path: /login_check 
      logout:  true 
      anonymous: true 

     public: 
      pattern: ^/.* 
      fos_facebook: 
       app_url: "http://www.facebook.com/apps/application.php?id=xxx" 
       server_url: "http://symfonytest.com.dev/app_dev.php/" 
       login_path: /login 
       check_path: /login_check 
       provider: my_fos_facebook_provider 
       default_target_path:/
      anonymous: true 
      logout: true 

我還將代碼轉換爲樹枝模板,如文檔中所示(也是從@Matt實現的代碼片段)。

回答

6

我和你有相同的工作流程,我的自定義用戶提供程序調用正確,一切工作正常。

您需要檢查的第一件事是:您是否有一個JavaScript腳本,通過彈出窗口成功登錄到Facebook後,將用戶重定向到login_check路由?這很重要,因爲在有效的身份驗證後調用login_check路由將觸發Symfony2的安全機制,該機制將調用FOSFacebookBundle特殊安全代碼,然後調用您自己的自定義用戶提供程序。我想你可能只是想念這小塊。

這裏的JavaScript代碼所對應的曲目,使之(使用jQuery)工作:

$(document).ready(function() { 
    Core.facebookInitialize(); 
}); 

var Core = { 
    /** 
    * Initialize facebook related things. This function will subscribe to the auth.login 
    * facebook event. When the event is raised, the function will redirect the user to 
    * the login check path. 
    */ 
    facebookInitialize = function() { 
     FB.Event.subscribe('auth.login', function(response) { 
      Core.performLoginCheck(); 
     }); 
    }; 

    /** 
    * Redirect user to the login check path. 
    */ 
    performLoginCheck = function() { 
     window.location = "http://localhost/app_dev.php/login_check"; 
    } 
} 

我把我在這裏的security.yml只是爲了幫助你檢查是否與自己的文件不同:

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

    providers: 
    acme.facebook_provider: 
     # This is our custom user provider service id. It is defined in config.yml under services 
     id: acme.user_provider 

    firewalls: 
    dev: 
     pattern: ^/(_(profiler|wdt)|css|images|js)/ 
     security: false 

    public: 
     pattern: ^/ 
     fos_facebook: 
     app_url: "http://www.facebook.com/apps/application.php?id=FACEBOOK_APP_ID" 
     server_url: "http://localhost/app_dev.php/" 
     default_target_path:/
     login_path: /login 
     check_path: /login_check 
     provider: acme.facebook_provider 
     anonymous: true 
     logout: true 

我的服務定義爲我們使用的定製用戶提供商:

services: 
    acme.user_provider: 
    class: Application\AcmeBundle\Security\User\Provider\UserProvider 
    arguments: 
     facebook:  "@fos_facebook.api" 
     entityManager: "@doctrine.orm.entity_manager" 
     validator:  "@validator" 

您還需要爲/login_check/login/logout路徑創建新路由。這些路由將被Symfony2掛鉤用於安全過程。這裏的行動在一個叫我的情況下MainController控制器實現的例子:

<?php 

namespace Application\AcmeBundle\Controller; 

use ...; 

class MainController extends Controller 
{ 
    /** 
    * This action is responsible of displaying the necessary informations for 
    * a user to perform login. In our case, this will be a button to connect 
    * to the facebook API. 
    * 
    * Important notice: This will be called ONLY when there is a problem with 
    * the login_check or by providing the link directly to the user. 
    * 
    * @Route("/{_locale}/login", name = "_security_login", defaults = {"_locale" = "en"}) 
    */ 
    public function loginAction() 
    { 
     if ($this->request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { 
      $error = $this->request->attributes->get(SecurityContext::AUTHENTICATION_ERROR); 
     } else { 
      $error = $this->request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR); 
     } 
     return $this->render('AcmeBundle:Main:login.html.twig', array(
      'error' => $error 
     )); 
    } 

    /** 
    * This action is responsible of checking if the credentials of the user 
    * are valid. This will not be called because this will be intercepted by the 
    * security component of Symfony. 
    * 
    * @Route("/{_locale}/login_check", name = "_security_check", defaults = {"_locale" = "en"}) 
    */ 
    public function loginCheckAction() 
    { 
     // Call intercepted by the Security Component of Symfony 
    } 

    /** 
    * This action is responsible of login out a user from the site. This will 
    * not be called because this will be intercepted by the security component 
    * of Symfony. 
    * 
    * @Route("/{_locale}/logout", name = "_security_logout", defaults = {"_locale" = "en"}) 
    */ 
    public function logoutAction() 
    { 
     return $this->redirect('index'); 
    } 
} 

希望這幫助,如果您有更多問題或誤解我從你的問題的時候,不要猶豫,發表評論。

問候,
馬特

+0

感謝您的答覆。我已經實現了你的答案的代碼,包括Javascript沒有任何運氣:(仍然是相同的情況發生。任何幫助讚賞! – users1184848

+0

順便說一句,我也可以從我的提供商的構造函數中的圖形API獲取用戶對象。我失去了我的希望:) – users1184848

+0

奇怪的是,cookie'fbsr_195918313779380'正在創建中。 – users1184848