Ask your Symfony questions! Pay money and get answers fast! (more info)

Facebook Connect + sfDoctrineApplyPlugin, sfDoctrineGuardPlugin Symfony

  • SOLVED

I'm looking for a complete and working tutorial on how to use Facebook Connect for User Registration and Authentication of an Symfony 1.4 Application using sfDoctrineApplyPlugin and sfDoctrineGuardPlugin for Registration and Authentication.

http://www.symfony-project.org/plugins/sfFacebookConnectPlugin doesn't seem to be working and maintained

http://www.symfony-project.org/plugins/kdDoctrineGuardFacebookConnectPlugin doesn't seem to have a complete installation/implementation tutorial

Answers (1)

2011-09-27

Ben answers:

I have used sfFacebookConnectPlugin
http://www.symfony-project.org/plugins/sfFacebookConnectPlugin

which uses sfDoctrineGuard

There is a great tutorial here:

http://www.symfony-project.org/more-with-symfony/1_4/en/12-Developing-for-Facebook

Note that the plugin codebase is now located on Git Hub
http://github.com/fabriceb/sfFacebookConnectPlugin

If you want to replicate the email registration process of sfDoctrineApply, you can do so quite easily by just adding a couple of extra methods to the sfGuardAuth module (copy it from the plugin directory into your application directory and change the copied version)

I add the following for email verification:



/* paste these methods inside your sfGuardAuthActions class*/
public function sendActivationEmail(sfGuardUser $user){
try{

$from_address = sfConfig::get('app_email_from_address');
$from_name = sfConfig::get('app_email_from_name');
$from = array(
$from_address => $from_name
);

$variables = array(
'name' => (($user->getFirstName() != '')? $user->getFirstName() :$user->getUsername()),
'token'=> $this->getActivationToken($user),
'userId'=> $user->getId()
);

/* I have a partial which includes the email body, but it will just basically contain a welcome message and the authorisation link */
$htmlBody = $this->getPartial('sfGuardAuth', 'activationEmailHTML', $variables);
$textBody = $this->getPartial('sfGuardAuth', 'activationEmailText', $variables);

$mailer = $this->getMailer();
$subject = sfConfig::get('app_site_name').' - Activate Your Account';
$message = $mailer->compose($from, $user->email_address, $subject, $htmlBody);

$message->setBody($htmlBody, 'text/html');
$message->addPart($textBody, 'text/plain');

$mailer->send($message);
}
catch(Swift_RfcComplianceException $e){
/* in the unlikely event that an email address passes the symfony validation, but fails the swift mailer validation, notify the admin */
mail(sfConfig::get('email_admin_address'), sfConfig::get('app_site_name').' anomalous email address', 'user_id: '.$user->getId()." \n".$e->getMessage());
}
}

public function executeActivate(sfWebRequest $request){
$user = Doctrine::getTable('sfGuardUser')->find($request->getParameter('id'));
$token = $request->getParameter('token');
if($token == $this->getActivationToken($user))
{
$user->setIsActive(true);
$user->save();
$this->getUser()->signin($user);
/* fire off user sign in event */
$event = new sfEvent($this, 'user.activated', array('userId' => $user->getId()));
$this->dispatcher->notify($event);
}
else{
}
}

public function getActivationToken(sfGuardUser $user)
{
/*
you can use anything method you like to generate the token as long as it's reproducable
I include the last_login as a field in order to prevent the token being used after the user has already logged in
*/
$string = $user->getSalt().$user->getCreatedAt().$user->getLastLogin();
return md5($string);
}