Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Define create_user_if_not_exists option as true by default #277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DependencyInjection/Security/Factory/FacebookFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct()
$this->addOption('display', 'page');
$this->addOption('app_url');
$this->addOption('server_url');
$this->addOption('create_user_if_not_exists', false);
$this->addOption('create_user_if_not_exists', true);
$this->addOption('redirect_to_facebook_login', true);
}

Expand Down
66 changes: 41 additions & 25 deletions Resources/doc/2-integration-with-fosuserbundle.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ to the provider id in the "provider" section in ```config.yml```:
use Symfony\Component\Security\Core\User\UserInterface;
use \BaseFacebook;
use \FacebookApiException;
use FOS\FacebookBundle\Security\User\UserManagerInterface;

class FacebookProvider implements UserProviderInterface
class FacebookProvider implements UserProviderInterface, UserManagerInterface
{
/**
* @var \Facebook
Expand Down Expand Up @@ -63,34 +64,20 @@ to the provider id in the "provider" section in ```config.yml```:
{
$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('');
}

// TODO use http://developers.facebook.com/docs/api/realtime
$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)) {
throw new UsernameNotFoundException('The user is not authenticated on facebook');
}

return $user;
return $this->updateUserData($user);
}

function createUserFromUid($uid)
{
$user = $this->userManager->createUser();
$user->setEnabled(true);
$user->setPassword('');

return $this->updateUserData($user);
}

public function refreshUser(UserInterface $user)
Expand All @@ -101,6 +88,35 @@ to the provider id in the "provider" section in ```config.yml```:

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

/**
* @param $user
* @throws \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
* @return UserInterface
*/
private function updateUserData($user)
{
try {
$fbdata = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
$fbdata = null;
}

if (empty($fbdata)) {
throw new UsernameNotFoundException('The user is not authenticated on facebook');
}

// TODO use http://developers.facebook.com/docs/api/realtime
$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 $user;
}
}

Finally, one also needs to add getFacebookId() and setFBData() methods to the User model.
Expand Down