[TwitterBridge] Create Foreign_user before Foreign_link
To keep foreign key constraints intact.
This commit is contained in:
parent
c11629035b
commit
598e8e4d56
|
@ -1,34 +1,31 @@
|
||||||
<?php
|
<?php
|
||||||
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||||
|
//
|
||||||
|
// GNU social is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// GNU social is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* StatusNet, the distributed open-source microblogging tool
|
|
||||||
*
|
|
||||||
* Class for doing OAuth authentication against Twitter
|
* Class for doing OAuth authentication against Twitter
|
||||||
*
|
*
|
||||||
* PHP version 5
|
|
||||||
*
|
|
||||||
* LICENCE: This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
* @category Plugin
|
* @category Plugin
|
||||||
* @package StatusNet
|
* @package GNUsocial
|
||||||
* @author Zach Copley <zach@status.net>
|
* @author Zach Copley <zach@status.net>
|
||||||
* @author Julien C <chaumond@gmail.com>
|
* @author Julien C <chaumond@gmail.com>
|
||||||
* @copyright 2009-2010 StatusNet, Inc.
|
* @copyright 2009-2010 StatusNet, Inc.
|
||||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
* @link http://status.net/
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (!defined('GNUSOCIAL')) { exit(1); }
|
defined('GNUSOCIAL') || die();
|
||||||
|
|
||||||
require_once dirname(__DIR__) . '/twitter.php';
|
require_once dirname(__DIR__) . '/twitter.php';
|
||||||
require_once INSTALLDIR . '/lib/util/oauthclient.php';
|
require_once INSTALLDIR . '/lib/util/oauthclient.php';
|
||||||
|
@ -41,20 +38,18 @@ require_once INSTALLDIR . '/lib/util/oauthclient.php';
|
||||||
* (Foreign_link) between the StatusNet user and Twitter user and stores the
|
* (Foreign_link) between the StatusNet user and Twitter user and stores the
|
||||||
* access token and secret in the link.
|
* access token and secret in the link.
|
||||||
*
|
*
|
||||||
* @category Plugin
|
* @category Plugin
|
||||||
* @package StatusNet
|
* @package GNUsocial
|
||||||
* @author Zach Copley <zach@status.net>
|
* @author Zach Copley <zach@status.net>
|
||||||
* @author Julien C <chaumond@gmail.com>
|
* @author Julien C <chaumond@gmail.com>
|
||||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
* @link http://status.net/
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
class TwitterauthorizationAction extends FormAction
|
class TwitterauthorizationAction extends FormAction
|
||||||
{
|
{
|
||||||
var $twuid = null;
|
public $twuid = null;
|
||||||
var $tw_fields = null;
|
public $tw_fields = null;
|
||||||
var $access_token = null;
|
public $access_token = null;
|
||||||
var $verifier = null;
|
public $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
|
||||||
|
|
||||||
|
@ -75,7 +70,7 @@ class TwitterauthorizationAction extends FormAction
|
||||||
|
|
||||||
common_redirect(common_local_url('twittersettings'));
|
common_redirect(common_local_url('twittersettings'));
|
||||||
} catch (NoResultException $e) {
|
} catch (NoResultException $e) {
|
||||||
// but if we don't have a foreign user linked, let's continue authorization procedure.
|
// but if we don't have a foreign user linked, let's continue authorization procedure.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,15 +92,15 @@ class TwitterauthorizationAction extends FormAction
|
||||||
// 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();
|
$this->createNewUser();
|
||||||
} elseif ($this->arg('connect')) {
|
} elseif ($this->arg('connect')) {
|
||||||
common_debug('TwitterBridgeDebug - POST with connect');
|
common_debug('TwitterBridgeDebug - POST with connect');
|
||||||
return $this->connectNewUser();
|
$this->connectNewUser();
|
||||||
|
} else {
|
||||||
|
common_debug('TwitterBridgeDebug - ' . print_r($this->args, true));
|
||||||
|
// TRANS: Form validation error displayed when an unhandled error occurs.
|
||||||
|
throw new ClientException(_m('No known action for POST.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
common_debug('TwitterBridgeDebug - ' . print_r($this->args, true));
|
|
||||||
// TRANS: Form validation error displayed when an unhandled error occurs.
|
|
||||||
throw new ClientException(_m('No known action for POST.'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -142,9 +137,9 @@ class TwitterauthorizationAction extends FormAction
|
||||||
* Called when Twitter returns an authorized request token. Exchanges
|
* Called when Twitter returns an authorized request token. Exchanges
|
||||||
* it for an access token and stores it.
|
* it for an access token and stores it.
|
||||||
*
|
*
|
||||||
* @return nothing
|
* @return void
|
||||||
*/
|
*/
|
||||||
function saveAccessToken()
|
private function saveAccessToken(): void
|
||||||
{
|
{
|
||||||
// Check to make sure Twitter returned the same request
|
// Check to make sure Twitter returned the same request
|
||||||
// token we sent them
|
// token we sent them
|
||||||
|
@ -165,7 +160,6 @@ class TwitterauthorizationAction extends FormAction
|
||||||
// Test the access token and get the user's Twitter info
|
// Test the access token and get the user's Twitter info
|
||||||
$client = new TwitterOAuthClient($atok->key, $atok->secret);
|
$client = new TwitterOAuthClient($atok->key, $atok->secret);
|
||||||
$twitter_user = $client->verifyCredentials();
|
$twitter_user = $client->verifyCredentials();
|
||||||
|
|
||||||
} catch (OAuthClientException $e) {
|
} catch (OAuthClientException $e) {
|
||||||
$msg = sprintf(
|
$msg = sprintf(
|
||||||
'OAuth client error - code: %1$s, msg: %2$s',
|
'OAuth client error - code: %1$s, msg: %2$s',
|
||||||
|
@ -182,14 +176,13 @@ class TwitterauthorizationAction extends FormAction
|
||||||
|
|
||||||
$this->saveForeignLink($this->scoped->getID(), $twitter_user->id, $atok);
|
$this->saveForeignLink($this->scoped->getID(), $twitter_user->id, $atok);
|
||||||
save_twitter_user($twitter_user->id, $twitter_user->screen_name);
|
save_twitter_user($twitter_user->id, $twitter_user->screen_name);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$this->twuid = $twitter_user->id;
|
$this->twuid = $twitter_user->id;
|
||||||
$this->tw_fields = array("screen_name" => $twitter_user->screen_name,
|
$this->tw_fields = array("screen_name" => $twitter_user->screen_name,
|
||||||
"fullname" => $twitter_user->name);
|
"fullname" => $twitter_user->name);
|
||||||
$this->access_token = $atok;
|
$this->access_token = $atok;
|
||||||
return $this->tryLogin();
|
$this->tryLogin();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up the the mess we made in the session
|
// Clean up the the mess we made in the session
|
||||||
|
@ -210,10 +203,13 @@ class TwitterauthorizationAction extends FormAction
|
||||||
* @param int $twuid Twitter user ID
|
* @param int $twuid Twitter user ID
|
||||||
* @param OAuthToken $token the access token to save
|
* @param OAuthToken $token the access token to save
|
||||||
*
|
*
|
||||||
* @return nothing
|
* @return void
|
||||||
*/
|
*/
|
||||||
function saveForeignLink($user_id, $twuid, $access_token)
|
private function saveForeignLink(
|
||||||
{
|
int $user_id,
|
||||||
|
int $twuid,
|
||||||
|
OAuthToken $access_token
|
||||||
|
): void {
|
||||||
$flink = new Foreign_link();
|
$flink = new Foreign_link();
|
||||||
|
|
||||||
$flink->user_id = $user_id;
|
$flink->user_id = $user_id;
|
||||||
|
@ -247,17 +243,15 @@ class TwitterauthorizationAction extends FormAction
|
||||||
// TRANS: Server error displayed when linking to a Twitter account fails.
|
// TRANS: Server error displayed when linking to a Twitter account fails.
|
||||||
throw new ServerException(_m('Could not link your Twitter account.'));
|
throw new ServerException(_m('Could not link your Twitter account.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $flink_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getInstructions()
|
public function getInstructions()
|
||||||
{
|
{
|
||||||
// TRANS: Page instruction. %s is the StatusNet sitename.
|
// TRANS: Page instruction. %s is the StatusNet sitename.
|
||||||
return sprintf(_m('This is the first time you have logged into %s so we must connect your Twitter account to a local account. You can either create a new account, or connect with your existing account, if you have one.'), common_config('site', 'name'));
|
return sprintf(_m('This is the first time you have logged into %s so we must connect your Twitter account to a local account. You can either create a new account, or connect with your existing account, if you have one.'), common_config('site', 'name'));
|
||||||
}
|
}
|
||||||
|
|
||||||
function title()
|
public function title()
|
||||||
{
|
{
|
||||||
// TRANS: Page title.
|
// TRANS: Page title.
|
||||||
return _m('Twitter Account Setup');
|
return _m('Twitter Account Setup');
|
||||||
|
@ -285,7 +279,7 @@ class TwitterauthorizationAction extends FormAction
|
||||||
* Should probably be replaced with an extensible mini version of
|
* Should probably be replaced with an extensible mini version of
|
||||||
* the core registration form.
|
* the core registration form.
|
||||||
*/
|
*/
|
||||||
function showContent()
|
public function showContent()
|
||||||
{
|
{
|
||||||
$this->elementStart('form', array('method' => 'post',
|
$this->elementStart('form', array('method' => 'post',
|
||||||
'id' => 'form_settings_twitter_connect',
|
'id' => 'form_settings_twitter_connect',
|
||||||
|
@ -305,12 +299,18 @@ class TwitterauthorizationAction extends FormAction
|
||||||
// Only allow new account creation if site is not flagged invite-only
|
// Only allow new account creation if site is not flagged invite-only
|
||||||
if (!common_config('site', 'inviteonly')) {
|
if (!common_config('site', 'inviteonly')) {
|
||||||
$this->elementStart('fieldset');
|
$this->elementStart('fieldset');
|
||||||
$this->element('legend', null,
|
$this->element(
|
||||||
// TRANS: Fieldset legend.
|
'legend',
|
||||||
_m('Create new account'));
|
null,
|
||||||
$this->element('p', null,
|
// TRANS: Fieldset legend.
|
||||||
// TRANS: Sub form introduction text.
|
_m('Create new account')
|
||||||
_m('Create a new user with this nickname.'));
|
);
|
||||||
|
$this->element(
|
||||||
|
'p',
|
||||||
|
null,
|
||||||
|
// TRANS: Sub form introduction text.
|
||||||
|
_m('Create a new user with this nickname.')
|
||||||
|
);
|
||||||
$this->elementStart('ul', 'form_data');
|
$this->elementStart('ul', 'form_data');
|
||||||
|
|
||||||
// Hook point for captcha etc
|
// Hook point for captcha etc
|
||||||
|
@ -318,17 +318,23 @@ class TwitterauthorizationAction extends FormAction
|
||||||
|
|
||||||
$this->elementStart('li');
|
$this->elementStart('li');
|
||||||
// TRANS: Field label.
|
// TRANS: Field label.
|
||||||
$this->input('newname', _m('New nickname'),
|
$this->input(
|
||||||
$this->username ?: '',
|
'newname',
|
||||||
// TRANS: Field title for nickname field.
|
_m('New nickname'),
|
||||||
_m('1-64 lowercase letters or numbers, no punctuation or spaces.'));
|
$this->username ?: '',
|
||||||
|
// TRANS: Field title for nickname field.
|
||||||
|
_m('1-64 lowercase letters or numbers, no punctuation or spaces.')
|
||||||
|
);
|
||||||
$this->elementEnd('li');
|
$this->elementEnd('li');
|
||||||
$this->elementStart('li');
|
$this->elementStart('li');
|
||||||
// TRANS: Field label.
|
// TRANS: Field label.
|
||||||
$this->input('email', _m('LABEL','Email'), $this->getEmail(),
|
$this->input(
|
||||||
// TRANS: Field title for e-mail address field.
|
'email',
|
||||||
_m('Used only for updates, announcements, '.
|
_m('LABEL', 'Email'),
|
||||||
'and password recovery'));
|
$this->getEmail(),
|
||||||
|
// TRANS: Field title for e-mail address field.
|
||||||
|
_m('Used only for updates, announcements, and password recovery')
|
||||||
|
);
|
||||||
$this->elementEnd('li');
|
$this->elementEnd('li');
|
||||||
|
|
||||||
// Hook point for captcha etc
|
// Hook point for captcha etc
|
||||||
|
@ -336,17 +342,23 @@ class TwitterauthorizationAction extends FormAction
|
||||||
|
|
||||||
$this->elementEnd('ul');
|
$this->elementEnd('ul');
|
||||||
// TRANS: Button text for creating a new StatusNet account in the Twitter connect page.
|
// TRANS: Button text for creating a new StatusNet account in the Twitter connect page.
|
||||||
$this->submit('create', _m('BUTTON','Create'));
|
$this->submit('create', _m('BUTTON', 'Create'));
|
||||||
$this->elementEnd('fieldset');
|
$this->elementEnd('fieldset');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->elementStart('fieldset');
|
$this->elementStart('fieldset');
|
||||||
$this->element('legend', null,
|
$this->element(
|
||||||
// TRANS: Fieldset legend.
|
'legend',
|
||||||
_m('Connect existing account'));
|
null,
|
||||||
$this->element('p', null,
|
// TRANS: Fieldset legend.
|
||||||
// TRANS: Sub form introduction text.
|
_m('Connect existing account')
|
||||||
_m('If you already have an account, login with your username and password to connect it to your Twitter account.'));
|
);
|
||||||
|
$this->element(
|
||||||
|
'p',
|
||||||
|
null,
|
||||||
|
// TRANS: Sub form introduction text.
|
||||||
|
_m('If you already have an account, login with your username and password to connect it to your Twitter account.')
|
||||||
|
);
|
||||||
$this->elementStart('ul', 'form_data');
|
$this->elementStart('ul', 'form_data');
|
||||||
$this->elementStart('li');
|
$this->elementStart('li');
|
||||||
// TRANS: Field label.
|
// TRANS: Field label.
|
||||||
|
@ -360,9 +372,12 @@ class TwitterauthorizationAction extends FormAction
|
||||||
$this->elementEnd('fieldset');
|
$this->elementEnd('fieldset');
|
||||||
|
|
||||||
$this->elementStart('fieldset');
|
$this->elementStart('fieldset');
|
||||||
$this->element('legend', null,
|
$this->element(
|
||||||
// TRANS: Fieldset legend.
|
'legend',
|
||||||
_m('License'));
|
null,
|
||||||
|
// TRANS: Fieldset legend.
|
||||||
|
_m('License')
|
||||||
|
);
|
||||||
$this->elementStart('ul', 'form_data');
|
$this->elementStart('ul', 'form_data');
|
||||||
$this->elementStart('li');
|
$this->elementStart('li');
|
||||||
$this->element('input', array('type' => 'checkbox',
|
$this->element('input', array('type' => 'checkbox',
|
||||||
|
@ -387,7 +402,7 @@ class TwitterauthorizationAction extends FormAction
|
||||||
$this->elementEnd('ul');
|
$this->elementEnd('ul');
|
||||||
$this->elementEnd('fieldset');
|
$this->elementEnd('fieldset');
|
||||||
// TRANS: Button text for connecting an existing StatusNet account in the Twitter connect page..
|
// TRANS: Button text for connecting an existing StatusNet account in the Twitter connect page..
|
||||||
$this->submit('connect', _m('BUTTON','Connect'));
|
$this->submit('connect', _m('BUTTON', 'Connect'));
|
||||||
$this->elementEnd('fieldset');
|
$this->elementEnd('fieldset');
|
||||||
$this->elementEnd('form');
|
$this->elementEnd('form');
|
||||||
}
|
}
|
||||||
|
@ -397,7 +412,7 @@ class TwitterauthorizationAction extends FormAction
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function getEmail()
|
private function getEmail(): string
|
||||||
{
|
{
|
||||||
$email = $this->trimmed('email');
|
$email = $this->trimmed('email');
|
||||||
if (!empty($email)) {
|
if (!empty($email)) {
|
||||||
|
@ -469,18 +484,28 @@ class TwitterauthorizationAction extends FormAction
|
||||||
$args['email'] = $email;
|
$args['email'] = $email;
|
||||||
}
|
}
|
||||||
|
|
||||||
common_debug('TwitterBridgeDebug - registering user with args:'.var_export($args,true));
|
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);
|
common_debug(
|
||||||
|
'TwitterBridgeDebug - registered the user and saving twitter user'
|
||||||
$this->saveForeignLink($user->id,
|
);
|
||||||
$this->twuid,
|
|
||||||
$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_debug(
|
||||||
|
'TwitterBridgeDebug - saving foreign link after creating new '
|
||||||
|
. 'local user ' . $user->id
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->saveForeignLink(
|
||||||
|
$user->id,
|
||||||
|
$this->twuid,
|
||||||
|
$this->access_token
|
||||||
|
);
|
||||||
|
|
||||||
common_set_user($user);
|
common_set_user($user);
|
||||||
common_real_login(true);
|
common_real_login(true);
|
||||||
|
|
||||||
|
@ -492,7 +517,7 @@ class TwitterauthorizationAction extends FormAction
|
||||||
common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)), 303);
|
common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)), 303);
|
||||||
}
|
}
|
||||||
|
|
||||||
function connectNewUser()
|
private function connectNewUser(): void
|
||||||
{
|
{
|
||||||
$nickname = $this->trimmed('nickname');
|
$nickname = $this->trimmed('nickname');
|
||||||
$password = $this->trimmed('password');
|
$password = $this->trimmed('password');
|
||||||
|
@ -511,9 +536,11 @@ class TwitterauthorizationAction extends FormAction
|
||||||
}
|
}
|
||||||
|
|
||||||
// throws exception on failure
|
// throws exception on failure
|
||||||
$this->saveForeignLink($user->id,
|
$this->saveForeignLink(
|
||||||
$this->twuid,
|
$user->id,
|
||||||
$this->access_token);
|
$this->twuid,
|
||||||
|
$this->access_token
|
||||||
|
);
|
||||||
|
|
||||||
save_twitter_user($this->twuid, $this->tw_fields['screen_name']);
|
save_twitter_user($this->twuid, $this->tw_fields['screen_name']);
|
||||||
|
|
||||||
|
@ -526,7 +553,7 @@ class TwitterauthorizationAction extends FormAction
|
||||||
$this->goHome($user->nickname);
|
$this->goHome($user->nickname);
|
||||||
}
|
}
|
||||||
|
|
||||||
function connectUser()
|
private function connectUser(): void
|
||||||
{
|
{
|
||||||
$user = common_current_user();
|
$user = common_current_user();
|
||||||
|
|
||||||
|
@ -570,22 +597,23 @@ class TwitterauthorizationAction extends FormAction
|
||||||
throw new ServerException(_m('No foreign link found for Twitter user'));
|
throw new ServerException(_m('No foreign link found for Twitter user'));
|
||||||
}
|
}
|
||||||
|
|
||||||
function goHome($nickname)
|
private function goHome(string $nickname): void
|
||||||
{
|
{
|
||||||
$url = common_get_returnto();
|
$url = common_get_returnto();
|
||||||
if ($url) {
|
if ($url) {
|
||||||
// We don't have to return to it again
|
// We don't have to return to it again
|
||||||
common_set_returnto(null);
|
common_set_returnto(null);
|
||||||
} else {
|
} else {
|
||||||
$url = common_local_url('all',
|
$url = common_local_url(
|
||||||
array('nickname' =>
|
'all',
|
||||||
$nickname));
|
['nickname' => $nickname]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
common_redirect($url, 303);
|
common_redirect($url, 303);
|
||||||
}
|
}
|
||||||
|
|
||||||
function bestNewNickname()
|
private function bestNewNickname(): ?string
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return Nickname::normalize($this->tw_fields['fullname'], true);
|
return Nickname::normalize($this->tw_fields['fullname'], true);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user