Skip to content

Instantly share code, notes, and snippets.

@daFish
Created June 20, 2012 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daFish/2959584 to your computer and use it in GitHub Desktop.
Save daFish/2959584 to your computer and use it in GitHub Desktop.
security.yml
<?php
// snip
/**
* Find user by email
*
* @param string $email
*
* @return mixed
*/
public function findUserByEmail($email)
{
return $this->userManager->findUserBy(array('email' => $email));
}
/**
* {@inheritDoc}
*/
public function loadUserByUsername($username)
{
$user = $this->findUserByFbId($username);
try {
$fbdata = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
$fbdata = null;
}
if (!empty($fbdata)) {
if (null === $user) {
$user = $this->findUserByEmail($fbdata['email']);
}
if (null === $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;
}
// <snip>
security:
providers:
my_facebook_provider:
id: my_user_bundle.facebook.user
firewalls:
// ...
public:
fos_facebook:
app_url: "https://developers.facebook.com/apps/<YOURAPPKEY>/"
server_url: "<SERVERURL>"
login_path: /login
check_path: /login_fb_check
default_target_path: /
provider: my_facebook_provider
logout:
handlers: ["fos_facebook.logout_handler"]
// ...
<?php
// <snip>
/**
* @param string $facebookId
*
* @return User
*/
public function setFacebookId($facebookId)
{
$this->facebookId = $facebookId;
if (!$this->username) {
$this->setUsername($facebookId);
$this->salt = '';
}
return $this;
}
// <snip>
/**
* @param array $fbdata
*/
public function setFBData($fbdata)
{
if (isset($fbdata['id'])) {
$this->setFacebookId($fbdata['id']);
$this->addRole('ROLE_FACEBOOK');
}
if (isset($fbdata['first_name'])) {
$this->setFirstname($fbdata['first_name']);
}
if (isset($fbdata['last_name'])) {
$this->setLastname($fbdata['last_name']);
}
if (isset($fbdata['email'])) {
$this->setEmail($fbdata['email']);
}
}
// <snip>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment