Пример #1
0
 /**
  *
  * @return integer
  * @author fabriceb
  * @since May 27, 2009 fabriceb
  */
 public function getCurrentFacebookUid()
 {
     $sfGuardUser = $this->getGuardUser();
     if ($sfGuardUser && sfFacebook::getFacebookClient()->get_loggedin_user() == sfFacebook::getGuardAdapter()->getUserFacebookUid($sfGuardUser)) {
         return sfFacebook::getFacebookClient()->get_loggedin_user();
     }
     return null;
 }
 /**
  * Sign in with the Facebook account
  * @author fabriceb
  * @since 2009-05-17
  *
  */
 public function executeFacebookSignin()
 {
     $user = $this->getUser();
     // first check if user is already logged and not yet Facebook connected
     if ($user->isAuthenticated() && !sfFacebook::getGuardAdapter()->getUserFacebookUid($user->getGuardUser()) && sfFacebook::getFacebookClient()->get_loggedin_user()) {
         $sfGuardUser = $user->getGuardUser();
         sfFacebook::getGuardAdapter()->setUserFacebookUid($sfGuardUser, sfFacebook::getFacebookClient()->get_loggedin_user());
         $sfGuardUser->save();
     } else {
         $create_automatically = !sfConfig::get('app_facebook_redirect_after_connect', false);
         $sfGuardUser = sfFacebook::getSfGuardUserByFacebookSession($create_automatically);
     }
     if ($sfGuardUser) {
         $this->getContext()->getUser()->signIn($sfGuardUser);
         $referer = $user->getAttribute('referer', $this->getRequest()->getReferer());
         $user->getAttributeHolder()->remove('referer');
         $signin_url = sfConfig::get('app_sf_guard_plugin_success_signin_url', $referer);
         $forward = $this->getRequestParameter('forward');
         $signin_url = $forward != '' ? $forward : $signin_url;
         $this->redirect('' != $signin_url ? $signin_url : '@homepage');
     }
     // check if user forgot to activate the account
     $sfGuardUser = sfFacebook::getSfGuardUserByFacebookSession($create_automatically, false);
     // the user does not exist even in unactivated mode
     if (!$sfGuardUser) {
         if ($this->getRequest()->isXmlHttpRequest()) {
             $this->getResponse()->setHeaderOnly(true);
             $this->getResponse()->setStatusCode(401);
             return sfView::NONE;
         }
         if (!$user->hasAttribute('referer')) {
             $user->setAttribute('referer', $this->getRequest()->getUri());
         }
         $redirect_url = sfConfig::get('app_facebook_redirect_after_connect_url');
     } else {
         $this->getUser()->setFlash('error', 'Your account is not activated');
         $redirect_url = sfConfig::get('sf_login_module') . '/' . sfConfig::get('sf_login_action');
     }
     return $this->redirect($redirect_url);
 }
Пример #3
0
 /**
  * Register new accounts with Facebook to facilitate friend linking.
  * Note: this is an optional step, and only makes sense if you have
  * a site with an existing userbase that you want to tie into
  * Facebook Connect.
  *
  * See http://wiki.developers.facebook.com/index.php/Friend_Linking
  * for more details.
  *
  * @param $sfGuardUsers  array of sfGuardUsers
  * @return integer the number of users registered
  * @since 2009-05-17 fabriceb cleaned the function and adapted to sfGuardUser
  */
 public static function registerUsers($sfGuardUsers)
 {
     $accounts = array();
     $hashed_users = array();
     foreach ($sfGuardUsers as $sfGuardUser) {
         $email = sfFacebook::getGuardAdapter()->getUserEmail($sfGuardUser);
         $email_hash = self::getEmailHash($email);
         if ($email_hash != '') {
             array_push($accounts, array('account_id' => $sfGuardUser->getId(), 'email_hash' => $email_hash));
             $hashed_users[$email_hash] = $sfGuardUser;
         }
     }
     if (count($accounts) == 0) {
         return 0;
     }
     $facebook = sfFacebook::getFacebookClient();
     $session_key = $facebook->api_client->session_key;
     $facebook->api_client->session_key = null;
     $result = false;
     try {
         $ret = $facebook->api_client->call_method('facebook.connect.registerUsers', array('accounts' => json_encode($accounts)));
         // On success, return the set of email hashes registered
         // An email hash will be registered even if the email does not match a Facebook account
         $result = count($ret);
         foreach ($ret as $email_hash) {
             sfFacebook::getGuardAdapter()->setUserEmailHash($hashed_users[$email_hash], $email_hash);
             $hashed_users[$email_hash]->getProfile()->save();
         }
     } catch (Exception $e) {
         error_log("Exception thrown while calling facebook.connect.registerUsers: " . $e->getMessage());
     }
     $facebook->api_client->session_key = $session_key;
     return $result;
 }