TwitterBridge working again (for signin and posting to Twitter at least)

Now we just need to make it include the newname suggestion if the form fails
(for example forgetting to check the license checkbox or so).
This commit is contained in:
Mikael Nordfeldth 2015-07-18 01:18:08 +02:00
parent beba2a25d0
commit 9fdf6474f8
3 changed files with 63 additions and 64 deletions

View File

@ -54,32 +54,28 @@ class TwitterauthorizationAction extends FormAction
var $twuid = null; var $twuid = null;
var $tw_fields = null; var $tw_fields = null;
var $access_token = null; var $access_token = null;
var $signin = null;
var $verifier = null; var $verifier = null;
protected $needLogin = false; // authorization page can also be used to create a new user protected $needLogin = false; // authorization page can also be used to create a new user
protected function doPreparation() protected function doPreparation()
{ {
$this->signin = $this->boolean('signin');
$this->oauth_token = $this->arg('oauth_token'); $this->oauth_token = $this->arg('oauth_token');
$this->verifier = $this->arg('oauth_verifier'); $this->verifier = $this->arg('oauth_verifier');
if ($this->scoped instanceof Profile) { if ($this->scoped instanceof Profile) {
$flink = Foreign_link::getByUserID($this->scoped->getID(), TWITTER_SERVICE); try {
$flink = Foreign_link::getByUserID($this->scoped->getID(), TWITTER_SERVICE);
$fuser = $flink->getForeignUser();
// If there's already a foreign link record and a foreign user // If there's already a foreign link record and a foreign user
// it means the accounts are already linked, and this is unecessary. // (no exceptions were thrown when fetching either of them...)
// So go back. // it means the accounts are already linked, and this is unecessary.
// So go back.
if ($flink instanceof Foreign_link) { common_redirect(common_local_url('twittersettings'));
try { } catch (NoResultException $e) {
$fuser = $flink->getForeignUser(); // but if we don't have a foreign user linked, let's continue authorization procedure.
common_redirect(common_local_url('twittersettings'));
} catch (NoResultException $e) {
// ok, so no foreign user but there's a foreign link??
// this logic is left since the StatusNet days
}
} }
} }
} }
@ -96,16 +92,18 @@ class TwitterauthorizationAction extends FormAction
$this->access_token = new OAuthToken($this->trimmed('access_token_key'), $this->trimmed('access_token_secret')); $this->access_token = new OAuthToken($this->trimmed('access_token_key'), $this->trimmed('access_token_secret'));
if ($this->arg('create')) { if ($this->arg('create')) {
common_debug('TwitterBridgeDebug - POST with create');
if (!$this->boolean('license')) { if (!$this->boolean('license')) {
// TRANS: Form validation error displayed when the checkbox to agree to the license has not been checked. // TRANS: Form validation error displayed when the checkbox to agree to the license has not been checked.
throw new ClientException(_m('You cannot register if you do not agree to the license.')); throw new ClientException(_m('You cannot register if you do not agree to the license.'));
} }
return $this->createNewUser(); return $this->createNewUser();
} else if ($this->arg('connect')) { } elseif ($this->arg('connect')) {
common_debug('TwitterBridgeDebug - POST with connect');
return $this->connectNewUser(); return $this->connectNewUser();
} }
common_debug('Twitter bridge - ' . print_r($this->args, true)); common_debug('TwitterBridgeDebug - ' . print_r($this->args, true));
// TRANS: Form validation error displayed when an unhandled error occurs. // TRANS: Form validation error displayed when an unhandled error occurs.
throw new ClientException(_m('No known action for POST.')); throw new ClientException(_m('No known action for POST.'));
} }
@ -125,7 +123,7 @@ class TwitterauthorizationAction extends FormAction
$_SESSION['twitter_request_token'] = $req_tok->key; $_SESSION['twitter_request_token'] = $req_tok->key;
$_SESSION['twitter_request_token_secret'] = $req_tok->secret; $_SESSION['twitter_request_token_secret'] = $req_tok->secret;
$auth_link = $client->getAuthorizeLink($req_tok, $this->signin); $auth_link = $client->getTwitterAuthorizeLink($req_tok, $this->boolean('signin'));
} catch (OAuthClientException $e) { } catch (OAuthClientException $e) {
$msg = sprintf( $msg = sprintf(
'OAuth client error - code: %1s, msg: %2s', 'OAuth client error - code: %1s, msg: %2s',
@ -270,11 +268,13 @@ class TwitterauthorizationAction extends FormAction
// $this->oauth_token is only populated once Twitter authorizes our // $this->oauth_token is only populated once Twitter authorizes our
// request token. If it's empty we're at the beginning of the auth // request token. If it's empty we're at the beginning of the auth
// process // process
if (empty($this->oauth_token)) { if (empty($this->error)) {
// authorizeRequestToken either throws an exception or redirects if (empty($this->oauth_token)) {
$this->authorizeRequestToken(); // authorizeRequestToken either throws an exception or redirects
} else { $this->authorizeRequestToken();
$this->saveAccessToken(); } else {
$this->saveAccessToken();
}
} }
parent::showPage(); parent::showPage();
@ -302,8 +302,8 @@ class TwitterauthorizationAction extends FormAction
$this->hidden('tw_fields_name', $this->tw_fields['fullname']); $this->hidden('tw_fields_name', $this->tw_fields['fullname']);
$this->hidden('token', common_session_token()); $this->hidden('token', common_session_token());
// Don't allow new account creation if site is flagged as invite only // Only allow new account creation if site is not flagged invite-only
if (common_config('site', 'inviteonly') == false) { if (!common_config('site', 'inviteonly')) {
$this->elementStart('fieldset'); $this->elementStart('fieldset');
$this->element('legend', null, $this->element('legend', null,
// TRANS: Fieldset legend. // TRANS: Fieldset legend.
@ -418,14 +418,17 @@ class TwitterauthorizationAction extends FormAction
return ''; return '';
} }
function createNewUser() protected function createNewUser()
{ {
common_debug('TwitterBridgeDebug - createNewUser');
if (!Event::handle('StartRegistrationTry', array($this))) { if (!Event::handle('StartRegistrationTry', array($this))) {
common_debug('TwitterBridgeDebug - StartRegistrationTry failed');
// TRANS: Client error displayed when trying to create a new user but a plugin aborted the process. // TRANS: Client error displayed when trying to create a new user but a plugin aborted the process.
throw new ClientException(_m('Registration of new user was aborted, maybe you failed a captcha?')); throw new ClientException(_m('Registration of new user was aborted, maybe you failed a captcha?'));
} }
if (common_config('site', 'closed')) { if (common_config('site', 'closed')) {
common_debug('TwitterBridgeDebug - site is closed for registrations');
// TRANS: Client error displayed when trying to create a new user while creating new users is not allowed. // TRANS: Client error displayed when trying to create a new user while creating new users is not allowed.
throw new ClientException(_m('Registration not allowed.')); throw new ClientException(_m('Registration not allowed.'));
} }
@ -433,6 +436,7 @@ class TwitterauthorizationAction extends FormAction
$invite = null; $invite = null;
if (common_config('site', 'inviteonly')) { if (common_config('site', 'inviteonly')) {
common_debug('TwitterBridgeDebug - site is inviteonly');
$code = $_SESSION['invitecode']; $code = $_SESSION['invitecode'];
if (empty($code)) { if (empty($code)) {
// TRANS: Client error displayed when trying to create a new user while creating new users is not allowed. // TRANS: Client error displayed when trying to create a new user while creating new users is not allowed.
@ -442,11 +446,13 @@ class TwitterauthorizationAction extends FormAction
$invite = Invitation::getKV('code', $code); $invite = Invitation::getKV('code', $code);
if (!$invite instanceof Invite) { if (!$invite instanceof Invite) {
common_debug('TwitterBridgeDebug - and we failed the invite code test');
// TRANS: Client error displayed when trying to create a new user with an invalid invitation code. // TRANS: Client error displayed when trying to create a new user with an invalid invitation code.
throw new ClientException(_m('Not a valid invitation code.')); throw new ClientException(_m('Not a valid invitation code.'));
} }
} }
common_debug('TwitterBridgeDebug - trying our nickname: '.$this->trimmed('newname'));
// Nickname::normalize throws exception if the nickname is taken // Nickname::normalize throws exception if the nickname is taken
$nickname = Nickname::normalize($this->trimmed('newname'), true); $nickname = Nickname::normalize($this->trimmed('newname'), true);
@ -463,12 +469,16 @@ class TwitterauthorizationAction extends FormAction
$args['email'] = $email; $args['email'] = $email;
} }
common_debug('TwitterBridgeDebug - registering user with args:'.var_export($args,true));
$user = User::register($args); $user = User::register($args);
common_debug('TwitterBridgeDebug - registered the user and saving foreign link for '.$user->id);
$this->saveForeignLink($user->id, $this->saveForeignLink($user->id,
$this->twuid, $this->twuid,
$this->access_token); $this->access_token);
common_debug('TwitterBridgeDebug - saving twitter user after creating new local user '.$user->id);
save_twitter_user($this->twuid, $this->tw_fields['screen_name']); save_twitter_user($this->twuid, $this->tw_fields['screen_name']);
common_set_user($user); common_set_user($user);
@ -534,19 +544,16 @@ class TwitterauthorizationAction extends FormAction
common_redirect(common_local_url('twittersettings'), 303); common_redirect(common_local_url('twittersettings'), 303);
} }
function tryLogin() protected function tryLogin()
{ {
common_debug('TwitterBridge Plugin - ' . common_debug('TwitterBridge Plugin - ' .
"Trying login for Twitter user $this->twuid."); "Trying login for Twitter user $this->twuid.");
$flink = Foreign_link::getByForeignID($this->twuid, try {
TWITTER_SERVICE); $flink = Foreign_link::getByForeignID($this->twuid, TWITTER_SERVICE);
if (!empty($flink)) {
$user = $flink->getUser(); $user = $flink->getUser();
if (!empty($user)) { if ($user instanceof User) {
common_debug('TwitterBridge Plugin - ' . common_debug('TwitterBridge Plugin - ' .
"Logged in Twitter user $flink->foreign_id as user $user->id ($user->nickname)"); "Logged in Twitter user $flink->foreign_id as user $user->id ($user->nickname)");
@ -554,9 +561,11 @@ class TwitterauthorizationAction extends FormAction
common_real_login(true); common_real_login(true);
$this->goHome($user->nickname); $this->goHome($user->nickname);
} }
} catch (NoResultException $e) {
// Either no Foreign_link was found or not the user connected to it.
// Let's just continue to allow creating or logging in as a new user.
} }
common_debug('TwitterBridge Plugin - ' . common_debug("TwitterBridge Plugin - No flink found for twuid: {$this->twuid} - new user");
"No flink found for twuid: $this->twuid - new user");
return; return;
throw new ServerException(_m('No foreign link found for Twitter user')); throw new ServerException(_m('No foreign link found for Twitter user'));

View File

@ -216,25 +216,15 @@ class TwittersettingsAction extends ProfileSettingsAction
* *
* @return void * @return void
*/ */
function handlePost() protected function doPost()
{ {
// CSRF protection
$token = $this->trimmed('token');
if (!$token || $token != common_session_token()) {
// TRANS: Client error displayed when the session token does not match or is not given.
$this->showForm(_m('There was a problem with your session token. '.
'Try again, please.'));
return;
}
if ($this->arg('save')) { if ($this->arg('save')) {
$this->savePreferences(); return $this->savePreferences();
} else if ($this->arg('disconnect')) { } else if ($this->arg('disconnect')) {
$this->removeTwitterAccount(); return $this->removeTwitterAccount();
} else {
// TRANS: Client error displayed when the submitted form contains unexpected data.
$this->showForm(_m('Unexpected form submission.'));
} }
// TRANS: Client error displayed when the submitted form contains unexpected data.
throw new ClientException(_m('Unexpected form submission.'));
} }
/** /**
@ -242,26 +232,26 @@ class TwittersettingsAction extends ProfileSettingsAction
* *
* @return void * @return void
*/ */
function removeTwitterAccount() protected function removeTwitterAccount()
{ {
$user = common_current_user(); if (!$this->flink instanceof Foreign_link) {
$flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE); // TRANS: Error message possibly displayed when trying to remove a connected Twitter account when there isn't one connected.
throw new AlreadyFulfilledException(_m('No Twitter connection to remove.'));
if (empty($flink)) {
// TRANS: Client error displayed when trying to remove a connected Twitter account when there isn't one connected.
$this->clientError(_m('No Twitter connection to remove.'));
} }
$result = $flink->safeDelete(); $result = $this->flink->safeDelete();
if (empty($result)) { if ($result === false) {
common_log_db_error($flink, 'DELETE', __FILE__); common_log_db_error($this->flink, 'DELETE', __FILE__);
// TRANS: Server error displayed when trying to remove a connected Twitter account fails. // TRANS: Server error displayed when trying to remove a connected Twitter account fails.
$this->serverError(_m('Could not remove Twitter user.')); throw new ServerException(_m('Could not remove Twitter user.'));
} }
$this->flink = null;
$this->fuser = null;
// TRANS: Success message displayed after disconnecting a Twitter account. // TRANS: Success message displayed after disconnecting a Twitter account.
$this->showForm(_m('Twitter account disconnected.'), true); return _m('Twitter account disconnected.');
} }
/** /**
@ -269,7 +259,7 @@ class TwittersettingsAction extends ProfileSettingsAction
* *
* @return void * @return void
*/ */
function savePreferences() protected function savePreferences()
{ {
$noticesend = $this->boolean('noticesend'); $noticesend = $this->boolean('noticesend');
$noticerecv = $this->boolean('noticerecv'); $noticerecv = $this->boolean('noticerecv');
@ -298,7 +288,7 @@ class TwittersettingsAction extends ProfileSettingsAction
} }
// TRANS: Success message after saving Twitter integration preferences. // TRANS: Success message after saving Twitter integration preferences.
$this->showForm(_m('Twitter preferences saved.'), true); return _m('Twitter preferences saved.');
} }
/** /**

View File

@ -126,7 +126,7 @@ class TwitterOAuthClient extends OAuthClient
* *
* @return the link * @return the link
*/ */
function getAuthorizeLink($request_token, $signin = false) function getTwitterAuthorizeLink($request_token, $signin = false)
{ {
$url = ($signin) ? self::$signinUrl : self::$authorizeURL; $url = ($signin) ? self::$signinUrl : self::$authorizeURL;