parent::handle($args);
if (common_is_real_login()) {
- $this->clientError(_('Already logged in.'));
+
+ // User is already logged in. Does she already have a linked Facebook acct?
+ $flink = Foreign_link::getByForeignID($this->fbuid, FACEBOOK_CONNECT_SERVICE);
+
+ if ($flink) {
+
+ // User already has a linked Facebook account and shouldn't be here
+ common_debug('There is already a local user (' . $flink->user_id .
+ ') linked with this Facebook (' . $this->fbuid . ').');
+
+ // We don't want these cookies
+ getFacebook()->clear_cookie_state();
+
+ $this->clientError(_('There is already a local user linked with this Facebook.'));
+
+ } else {
+
+ // User came from the Facebook connect settings tab, and
+ // probably just wants to link/relink their Facebook account
+ $this->connectUser();
+ }
+
} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$token = $this->trimmed('token');
}
$this->createNewUser();
} else if ($this->arg('connect')) {
- $this->connectUser();
+ $this->connectNewUser();
} else {
common_debug(print_r($this->args, true), __FILE__);
$this->showForm(_('Something weird happened.'),
303);
}
- function connectUser()
+ function connectNewUser()
{
$nickname = $this->trimmed('nickname');
$password = $this->trimmed('password');
$this->goHome($user->nickname);
}
+ function connectUser()
+ {
+ $user = common_current_user();
+
+ $result = $this->flinkUser($user->id, $this->fbuid);
+
+ if (!$result) {
+ $this->serverError(_('Error connecting user to Facebook.'));
+ return;
+ }
+
+ common_debug("Connected Facebook user $this->fbuid to local user $user->id");
+
+ // Return to Facebook connection settings tab
+ common_redirect(common_local_url('FBConnectSettings'), 303);
+ }
+
function tryLogin()
{
common_debug("Trying Facebook Login...");
if ($user) {
- $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
- _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
- $action->menuItem(common_local_url('profilesettings'),
- _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
- if (common_config('xmpp', 'enabled')) {
- $action->menuItem(common_local_url('imsettings'),
- _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
- } else {
- $action->menuItem(common_local_url('smssettings'),
- _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
- }
- $action->menuItem(common_local_url('invite'),
- _('Invite'),
- sprintf(_('Invite friends and colleagues to join you on %s'),
- common_config('site', 'name')),
- false, 'nav_invitecontact');
-
$flink = Foreign_link::getByUserId($user->id, FACEBOOK_CONNECT_SERVICE);
$fbuid = 0;
}
}
- // Need to override the Logout link to make it do FB stuff
+ $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
+ _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
+ $action->menuItem(common_local_url('profilesettings'),
+ _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
+ if (common_config('xmpp', 'enabled')) {
+ $action->menuItem(common_local_url('imsettings'),
+ _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
+ } else {
+ $action->menuItem(common_local_url('smssettings'),
+ _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
+ }
+ $action->menuItem(common_local_url('invite'),
+ _('Invite'),
+ sprintf(_('Invite friends and colleagues to join you on %s'),
+ common_config('site', 'name')),
+ false, 'nav_invitecontact');
- if ($fbuid > 0) {
+ // Need to override the Logout link to make it do FB stuff
+ if ($flink && $fbuid > 0) {
$logout_url = common_local_url('logout');
$title = _('Logout from the site');
return true;
}
- function onEndLogout($action)
+ function onStartLogout($action)
{
- try {
+ $user = common_current_user();
+
+ $flink = Foreign_link::getByUserId($user->id, FACEBOOK_CONNECT_SERVICE);
+
+ $action->logout();
+
+ if ($flink) {
$facebook = getFacebook();
- $fbuid = $facebook->get_loggedin_user();
- if ($fbuid > 0) {
- $facebook->logout(common_local_url('public'));
- }
+ try {
+ $fbuid = $facebook->get_loggedin_user();
- } catch (Exception $e) {
- common_log(LOG_WARNING, 'Could\'t logout of Facebook: ' .
- $e->getMessage());
+ if ($fbuid > 0) {
+ $facebook->logout(common_local_url('public'));
+ }
+
+ } catch (Exception $e) {
+ common_log(LOG_WARNING, 'Could\'t logout of Facebook: ' .
+ $e->getMessage());
+ }
}
+
+ return true;
}
}
function showContent()
{
$user = common_current_user();
-
$flink = Foreign_link::getByUserID($user->id, FACEBOOK_CONNECT_SERVICE);
+ $this->elementStart('form', array('method' => 'post',
+ 'id' => 'form_settings_facebook',
+ 'class' => 'form_settings',
+ 'action' =>
+ common_local_url('FBConnectSettings')));
+
if (!$flink) {
- $this->element('p', 'form_note',
+ $this->element('p', 'instructions',
_('There is no Facebook user connected to this account.'));
$this->element('fb:login-button', array('onlogin' => 'goto_login()',
'length' => 'long'));
- return;
- }
+ } else {
- $this->element('p', 'form_note',
- _('Connected Facebook user:'));
+ $this->element('p', 'form_note',
+ _('Connected Facebook user'));
+
+ $this->elementStart('p', array('class' => 'facebook-user-display'));
+ $this->elementStart('fb:profile-pic',
+ array('uid' => $flink->foreign_id,
+ 'size' => 'small',
+ 'linked' => 'true',
+ 'facebook-logo' => 'true'));
+ $this->elementEnd('fb:profile-pic');
+
+ $this->elementStart('fb:name', array('uid' => $flink->foreign_id,
+ 'useyou' => 'false'));
+ $this->elementEnd('fb:name');
+ $this->elementEnd('p');
- $this->elementStart('p', array('class' => 'facebook-user-display'));
- $this->elementStart('fb:profile-pic',
- array('uid' => $flink->foreign_id,
- 'size' => 'square',
- 'linked' => 'true',
- 'facebook-logo' => 'true'));
- $this->elementEnd('fb:profile-pic');
+ $this->hidden('token', common_session_token());
- $this->elementStart('fb:name', array('uid' => $flink->foreign_id));
- $this->elementEnd('fb:name');
- $this->elementEnd('p');
+ $this->elementStart('fieldset');
- $this->elementStart('form', array('method' => 'post',
- 'id' => 'form_settings_facebook',
- 'class' => 'form_settings',
- 'action' =>
- common_local_url('FBConnectSettings')));
+ $this->element('legend', null, _('Disconnect my account from Facebook'));
- $this->hidden('token', common_session_token());
+ if (!$user->password) {
- $this->elementStart('fieldset');
+ $this->elementStart('p', array('class' => 'form_guide'));
+ $this->text(_('Disconnecting your Faceboook ' .
+ 'would make it impossible to log in! Please '));
+ $this->element('a',
+ array('href' => common_local_url('passwordsettings')),
+ _('set a password'));
- $this->element('legend', null, _('Disconnect my account from Facebook'));
+ $this->text(_(' first.'));
+ $this->elementEnd('p');
+ } else {
- if (!$user->password) {
+ $note = 'Keep your %s account but disconnect from Facebook. ' .
+ 'You\'ll use your %s password to log in.';
- $this->elementStart('p', array('class' => 'form_guide'));
- $this->text(_('Disconnecting your Faceboook ' .
- 'would make it impossible to log in! Please '));
- $this->element('a',
- array('href' => common_local_url('passwordsettings')),
- _('set a password'));
+ $site = common_config('site', 'name');
- $this->text(_(' first.'));
- $this->elementEnd('p');
- } else {
- $this->submit('disconnect', _('Disconnect'));
- }
+ $this->element('p', 'instructions',
+ sprintf($note, $site, $site));
+
+ $this->submit('disconnect', _('Disconnect'));
+ }
+
+ $this->elementEnd('fieldset');
+ }
- $this->elementEnd('fieldset');
$this->elementEnd('form');
}
try {
- // XXX: not sure what exactly to do here
-
+ // Clear FB Connect cookies out
$facebook = getFacebook();
$facebook->clear_cookie_state();
$e->getMessage());
}
- $this->showForm(_('Facebook user disconnected.'), true);
+ $this->showForm(_('You have disconnected from Facebook.'), true);
} else {
$this->showForm(_('Not sure what you\'re trying to do.'));