FB Connect plugin - better workflow for disconnecting and reconnecting Facebook accounts

This commit is contained in:
Zach Copley 2009-05-28 00:08:00 +00:00
parent 60ac9bc6fd
commit 1e9c5b52b4
3 changed files with 135 additions and 78 deletions

View File

@ -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...");

View File

@ -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,11 +257,19 @@ 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();
try {
$fbuid = $facebook->get_loggedin_user();
if ($fbuid > 0) {
@ -275,4 +282,7 @@ class FBConnectPlugin extends Plugin
}
}
return true;
}
}

View File

@ -78,41 +78,40 @@ 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')));
if (!$flink) {
$this->element('p', 'instructions',
_('There is no Facebook user connected to this account.'));
$this->element('fb:login-button', array('onlogin' => 'goto_login()',
'length' => 'long'));
} else {
$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');
@ -131,10 +130,21 @@ class FBConnectSettingsAction extends ConnectSettingsAction
$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.'));