From 1e9c5b52b4d816b701ee4e9da1b27c1f1b9cbf31 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Thu, 28 May 2009 00:08:00 +0000 Subject: [PATCH] FB Connect plugin - better workflow for disconnecting and reconnecting Facebook accounts --- plugins/FBConnect/FBConnectAuth.php | 44 +++++++++- plugins/FBConnect/FBConnectPlugin.php | 66 ++++++++------- plugins/FBConnect/FBConnectSettings.php | 103 +++++++++++++----------- 3 files changed, 135 insertions(+), 78 deletions(-) diff --git a/plugins/FBConnect/FBConnectAuth.php b/plugins/FBConnect/FBConnectAuth.php index 233eb83ab3..4699ce636c 100644 --- a/plugins/FBConnect/FBConnectAuth.php +++ b/plugins/FBConnect/FBConnectAuth.php @@ -62,7 +62,28 @@ class FBConnectauthAction extends Action 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'); @@ -78,7 +99,7 @@ class FBConnectauthAction extends Action } $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.'), @@ -259,7 +280,7 @@ class FBConnectauthAction extends Action 303); } - function connectUser() + function connectNewUser() { $nickname = $this->trimmed('nickname'); $password = $this->trimmed('password'); @@ -290,6 +311,23 @@ class FBConnectauthAction extends Action $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..."); diff --git a/plugins/FBConnect/FBConnectPlugin.php b/plugins/FBConnect/FBConnectPlugin.php index ad5e47e478..c85ef432d4 100644 --- a/plugins/FBConnect/FBConnectPlugin.php +++ b/plugins/FBConnect/FBConnectPlugin.php @@ -143,23 +143,6 @@ class FBConnectPlugin extends Plugin 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; @@ -195,9 +178,25 @@ class FBConnectPlugin extends Plugin } } - // 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'); @@ -258,21 +257,32 @@ class FBConnectPlugin extends Plugin 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(); + + if ($fbuid > 0) { + $facebook->logout(common_local_url('public')); + } + + } catch (Exception $e) { + common_log(LOG_WARNING, 'Could\'t logout of Facebook: ' . + $e->getMessage()); } - - } catch (Exception $e) { - common_log(LOG_WARNING, 'Could\'t logout of Facebook: ' . - $e->getMessage()); } + + return true; } } diff --git a/plugins/FBConnect/FBConnectSettings.php b/plugins/FBConnect/FBConnectSettings.php index 7e255f43a6..034ecebae2 100644 --- a/plugins/FBConnect/FBConnectSettings.php +++ b/plugins/FBConnect/FBConnectSettings.php @@ -78,63 +78,73 @@ class FBConnectSettingsAction extends ConnectSettingsAction function showContent() { $user = common_current_user(); - $flink = Foreign_link::getByUserID($user->id, FACEBOOK_CONNECT_SERVICE); - if (!$flink) { - - $this->element('p', 'form_note', - _('There is no Facebook user connected to this account.')); - - $this->element('fb:login-button', array('onlogin' => 'goto_login()', - 'length' => 'long')); - - return; - } - - $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' => 'square', - 'linked' => 'true', - 'facebook-logo' => 'true')); - $this->elementEnd('fb:profile-pic'); - - $this->elementStart('fb:name', array('uid' => $flink->foreign_id)); - $this->elementEnd('fb:name'); - $this->elementEnd('p'); - $this->elementStart('form', array('method' => 'post', 'id' => 'form_settings_facebook', 'class' => 'form_settings', 'action' => common_local_url('FBConnectSettings'))); - $this->hidden('token', common_session_token()); + if (!$flink) { - $this->elementStart('fieldset'); + $this->element('p', 'instructions', + _('There is no Facebook user connected to this account.')); - $this->element('legend', null, _('Disconnect my account from Facebook')); + $this->element('fb:login-button', array('onlogin' => 'goto_login()', + 'length' => 'long')); - if (!$user->password) { - - $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->text(_(' first.')); - $this->elementEnd('p'); } else { - $this->submit('disconnect', _('Disconnect')); - } - $this->elementEnd('fieldset'); + $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->hidden('token', common_session_token()); + + $this->elementStart('fieldset'); + + $this->element('legend', null, _('Disconnect my account from Facebook')); + + if (!$user->password) { + + $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->text(_(' first.')); + $this->elementEnd('p'); + } else { + + $note = 'Keep your %s account but disconnect from Facebook. ' . + 'You\'ll use your %s password to log in.'; + + $site = common_config('site', 'name'); + + $this->element('p', 'instructions', + sprintf($note, $site, $site)); + + $this->submit('disconnect', _('Disconnect')); + } + + $this->elementEnd('fieldset'); + } + $this->elementEnd('form'); } @@ -171,8 +181,7 @@ class FBConnectSettingsAction extends ConnectSettingsAction try { - // XXX: not sure what exactly to do here - + // Clear FB Connect cookies out $facebook = getFacebook(); $facebook->clear_cookie_state(); @@ -182,7 +191,7 @@ class FBConnectSettingsAction extends ConnectSettingsAction $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.'));