Symfony:无法从会话中检索Facebook用户

时间:2016-04-06 08:25:44

标签: php

我正在尝试将FOSFacebookBundle与FOSUserBundle集成,我收到此错误。 “无法从会话中检索到Facebook用户” 我在互联网上搜索了这个问题,但我没解决。 伙计们!任何帮助? 在这里我的providerFacebook:

<?php

namespace Utilisateurs\UtilisateursBundle\Security\User\Provider;

use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Facebook;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use \BaseFacebook;
use \FacebookApiException;

class FacebookProvider implements UserProviderInterface
{
/**
 * @var \Facebook
 */
protected $facebook;
protected $userManager;
protected $validator;
protected $container;

public function __construct(BaseFacebook $facebook, $userManager, $validator, $container)
{
    $this->facebook = $facebook;

    // Add this to not have the error "the ssl certificate is invalid."
    Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
    Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;

    $this->userManager = $userManager;
    $this->validator = $validator;
    $this->container = $container;
}

public function supportsClass($class)
{
    return $this->userManager->supportsClass($class);
}

public function findUserByFbId($fbId)
{
    return $this->userManager->findUserBy(array('facebookId' => $fbId));
}

public function findUserByUsername($username)
{
    return $this->userManager->findUserBy(array('username' => $username));
}

public function connectExistingAccount()
{

    try {
        $fbdata = $this->facebook->api('/me');
    } catch (FacebookApiException $e) {
        $fbdata = null;
        return false;
    }

    $alreadyExistingAccount = $this->findUserByFbId($fbdata['id']);

    if (!empty($alreadyExistingAccount)) {
        return false;
    }

    if (!empty($fbdata)) {

        $currentUserObj = $this->container->get('security.context')->getToken()->getUser();

        $user = $this->findUserByUsername($currentUserObj->getUsername());

        if (empty($user)) {
            return false;
        }

        $user->setFBData($fbdata);

        if (count($this->validator->validate($user, 'Facebook'))) {
            // TODO: the user was found obviously, but doesnt match our expectations, do something smart
            throw new UsernameNotFoundException('The facebook user could not be stored');
        }
        $this->userManager->updateUser($user);

        return true;
    }

    return false;

}

public function loadUserByUsername($username)
{
    $user = $this->findUserByFbId($username);

    try {
        $fbdata = $this->facebook->api('/me');
    } catch (FacebookApiException $e) {
        $fbdata = null;
    }

    if (!empty($fbdata)) {
        if (empty($user)) {
            $user = $this->userManager->createUser();
            $user->setEnabled(true);
            $user->setPassword('');
        }

        if($user->getUsername() == '' || $user->getUsername() == null)
        {
            $user->setUsername($username . '@facebook.com');
        }

        $user->setFBData($fbdata);

        if (count($this->validator->validate($user, 'Facebook'))) {
            // TODO: the user was found obviously, but doesnt match our expectations, do something smart
            throw new UsernameNotFoundException('The facebook user could not be stored');
        }
        $this->userManager->updateUser($user);
    }

    if (empty($user)) {

        // TODO: the user was found obviously, but doesnt match our expectations, do something smart
        throw new UsernameNotFoundException('The facebook user could not be stored');

    }

    return $user;
}

public function refreshUser(UserInterface $user)
{
    if (!$this->supportsClass(get_class($user)) || !$user->getFacebookId()) {
        throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
    }

    return $this->loadUserByUsername($user->getFacebookId());
}

这是我的文件security.yml:

安全性:

# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
    chainprovider:
                  chain:
                      providers: [fos_userbundle, fos_facebook_provider]

    fos_facebook_provider:
        id: fos_facebook.user.login
    fos_userbundle:
        id: fos_user.user_provider.username

encoders:
    FOS\UserBundle\Model\UserInterface: sha512

firewalls:
    main:
            pattern: ^/

            fos_facebook:
                  app_url: "http://apps.facebook.com/this-is-my-awesome-app"
                  server_url: "http://localhost/partie1/web/app_dev.php"
                  login_path: /user/login
                  check_path: /facebook/login_check
                  provider: fos_facebook_provider
                  default_target_path: /


            form_login:
                  provider: fos_userbundle
                  csrf_provider: form.csrf_provider
                  login_path:     /user/login
                  use_forward:    false
                  check_path:     /user/login_check
                  failure_path:   null
            logout:
                path: /user/logout
            anonymous: ~
            remember_me:
                  key:      mySuperDuperKey
                  lifetime: 4147200
                  path:     /
                  domain:   ~



access_control:
    - { path: ^/user/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/secured, role: ROLE_USER }

0 个答案:

没有答案