Exemple #1
0
 /**
  * Sync the user back to the linked Facebook account.
  *
  * <h3>Usage:</h3>
  * <pre class="code">
  * $auth->syncUser($_POST['access-token']);
  * </pre>
  *
  * @param string $access_token A valid access token for the user to sync up.
  *
  * @return bool True or false on success.
  */
 public function syncUser($access_token)
 {
     try {
         $facebook = new \Facebook(['appId' => FACEBOOK_APP_ID, 'secret' => FACEBOOK_APP_SECRET]);
         $facebook->setAccessToken($access_token);
         /** @var array $user_profile The array of user data from Facebook */
         $user_profile = $facebook->api('/me');
     } catch (\Exception $e) {
         return false;
     }
     $user = $this->_usermodel;
     if (!$user->exists()) {
         // Some config options for new accounts only.
         $profiles = $user->get('external_profiles');
         if (!is_array($profiles)) {
             $profiles = [];
         }
         $profiles[] = [['type' => 'facebook', 'url' => $user_profile['link'], 'title' => 'Facebook Profile']];
         $user->set('external_profiles', $profiles);
         // Another component from the user-social component.
         // This needs to be unique, so do a little fudging if necessary.
         try {
             $user->set('username', $user_profile['username']);
         } catch (\ModelValidationException $e) {
             $user->set('username', $user_profile['username'] . '-' . \Core\random_hex(3));
         }
         // Sync the user avatar.
         $f = new \Core\Filestore\Backends\FileRemote('http://graph.facebook.com/' . $user_profile['id'] . '/picture?type=large');
         $dest = \Core\Filestore\Factory::File('public/user/avatar/' . $f->getBaseFilename());
         $f->copyTo($dest);
         $user->set('avatar', 'public/user/avatar/' . $dest->getBaseFilename());
     }
     // Get all user configs and load in anything possible.
     $user->set('first_name', $user_profile['first_name']);
     $user->set('last_name', $user_profile['last_name']);
     $user->set('gender', ucwords($user_profile['gender']));
     $user->set('facebook_id', $user_profile['id']);
     $user->set('facebook_link', $user_profile['link']);
     $user->set('facebook_access_token', $facebook->getAccessToken());
 }