[OpenID] Add sync confirmation in both OpenID settings and login connection

This commit is contained in:
brunoccast 2019-06-04 16:55:49 +01:00
parent 54d7a7cccd
commit 69a1d77480
3 changed files with 182 additions and 157 deletions

View File

@ -46,7 +46,7 @@ require_once INSTALLDIR.'/plugins/OpenID/openid.php';
*/
class FinishaddopenidAction extends Action
{
var $msg = null;
public $msg = null;
/**
* Handle the redirect back from OpenID confirmation
@ -58,7 +58,7 @@ class FinishaddopenidAction extends Action
*
* @return void
*/
function handle()
public function handle()
{
parent::handle();
if (!common_logged_in()) {
@ -76,7 +76,7 @@ class FinishaddopenidAction extends Action
*
* @return void
*/
function tryLogin()
public function tryLogin()
{
$consumer = oid_consumer();
@ -89,10 +89,11 @@ class FinishaddopenidAction extends Action
} elseif ($response->status == Auth_OpenID_FAILURE) {
// TRANS: OpenID authentication failed; display the error message.
// TRANS: %s is the error message.
$this->message(sprintf(_m('OpenID authentication failed: %s.'),
$response->message));
$this->message(sprintf(
_m('OpenID authentication failed: %s.'),
$response->message
));
} elseif ($response->status == Auth_OpenID_SUCCESS) {
$display = $response->getDisplayIdentifier();
$canonical = ($response->endpoint && $response->endpoint->canonicalID) ?
$response->endpoint->canonicalID : $display;
@ -136,16 +137,19 @@ class FinishaddopenidAction extends Action
$this->message(_m('Error connecting user.'));
return;
}
if (Event::handle('StartOpenIDUpdateUser', array($cur, $canonical, &$sreg))) {
if ($sreg) {
if (isset($_SESSION['openid_sync']) && $_SESSION['openid_sync']) {
if (Event::handle('StartOpenIDUpdateUser', [$cur, $canonical, &$sreg])) {
if (!oid_update_user($cur, $sreg)) {
// TRANS: Message in case the user or the user profile cannot be saved in StatusNet.
$this->message(_m('Error updating profile.'));
return;
}
}
Event::handle('EndOpenIDUpdateUser', [$cur, $canonical, $sreg]);
}
Event::handle('EndOpenIDUpdateUser', array($cur, $canonical, $sreg));
unset($_SESSION['openid_sync']);
// success!
@ -166,7 +170,7 @@ class FinishaddopenidAction extends Action
*
* @return void
*/
function message($msg)
public function message($msg)
{
$this->message = $msg;
$this->showPage();
@ -177,7 +181,7 @@ class FinishaddopenidAction extends Action
*
* @return string title
*/
function title()
public function title()
{
// TRANS: Title after getting the status of the OpenID authorisation request.
return _m('OpenID Login');
@ -188,7 +192,7 @@ class FinishaddopenidAction extends Action
*
* @return void
*/
function showPageNotice()
public function showPageNotice()
{
if ($this->message) {
$this->element('p', 'error', $this->message);

View File

@ -25,11 +25,11 @@ require_once INSTALLDIR.'/plugins/OpenID/openid.php';
class FinishopenidloginAction extends Action
{
var $error = null;
var $username = null;
var $message = null;
public $error = null;
public $username = null;
public $message = null;
function handle()
public function handle()
{
parent::handle();
if (common_is_real_login()) {
@ -45,8 +45,10 @@ class FinishopenidloginAction extends Action
if ($this->arg('create')) {
if (!$this->boolean('license')) {
// TRANS: Message given if user does not agree with the site's license.
$this->showForm(_m('You cannot register if you do not agree to the license.'),
$this->trimmed('newname'));
$this->showForm(
_m('You cannot register if you do not agree to the license.'),
$this->trimmed('newname')
);
return;
}
$this->createNewUser();
@ -54,18 +56,20 @@ class FinishopenidloginAction extends Action
$this->connectUser();
} else {
// TRANS: Messag given on an unknown error.
$this->showForm(_m('An unknown error has occured.'),
$this->trimmed('newname'));
$this->showForm(
_m('An unknown error has occured.'),
$this->trimmed('newname')
);
}
} else {
$this->tryLogin();
}
}
function showPageNotice()
public function showPageNotice()
{
if ($this->error) {
$this->element('div', array('class' => 'error'), $this->error);
$this->element('div', ['class' => 'error'], $this->error);
} else {
$this->element('div', 'instructions',
// TRANS: Instructions given after a first successful logon using OpenID.
@ -74,13 +78,13 @@ class FinishopenidloginAction extends Action
}
}
function title()
public function title()
{
// TRANS: Title
return _m('TITLE', 'OpenID Account Setup');
}
function showForm($error=null, $username=null)
public function showForm($error=null, $username=null)
{
$this->error = $error;
$this->username = $username;
@ -93,10 +97,10 @@ class FinishopenidloginAction extends Action
* Should probably be replaced with an extensible mini version of
* the core registration form.
*/
function showContent()
public function showContent()
{
if (!empty($this->message_text)) {
$this->element('div', array('class' => 'error'), $this->message_text);
$this->element('div', ['class' => 'error'], $this->message_text);
return;
}
@ -107,12 +111,12 @@ class FinishopenidloginAction extends Action
// info. The profile will be pre-populated with whatever name,
// email, and location we can get from the OpenID provider, so
// all we ask for is the license confirmation.
$this->elementStart('form', array('method' => 'post',
$this->elementStart('form', ['method' => 'post',
'id' => 'account_create',
'class' => 'form_settings',
'action' => common_local_url('finishopenidlogin')));
'action' => common_local_url('finishopenidlogin')]);
$this->hidden('token', common_session_token());
$this->elementStart('fieldset', array('id' => 'form_openid_createaccount'));
$this->elementStart('fieldset', ['id' => 'form_openid_createaccount']);
$this->element('legend', null,
// TRANS: Fieldset legend.
_m('Create new account'));
@ -122,34 +126,36 @@ class FinishopenidloginAction extends Action
$this->elementStart('ul', 'form_data');
// Hook point for captcha etc
Event::handle('StartRegistrationFormData', array($this));
Event::handle('StartRegistrationFormData', [$this]);
$this->elementStart('li');
// TRANS: Field label.
$this->input('newname', _m('New nickname'),
$this->input('newname',
_m('New nickname'),
($this->username) ? $this->username : '',
// TRANS: Field title.
_m('1-64 lowercase letters or numbers, no punctuation or spaces.'));
$this->elementEnd('li');
$this->elementStart('li');
// TRANS: Field label.
$this->input('email', _m('Email'), $this->getEmail(),
$this->input('email', _m('Email'),
$this->getEmail(),
// TRANS: Field title.
_m('Used only for updates, announcements, '.
'and password recovery.'));
$this->elementEnd('li');
// Hook point for captcha etc
Event::handle('EndRegistrationFormData', array($this));
Event::handle('EndRegistrationFormData', [$this]);
$this->elementStart('li');
$this->element('input', array('type' => 'checkbox',
$this->element('input', ['type' => 'checkbox',
'id' => 'license',
'class' => 'checkbox',
'name' => 'license',
'value' => 'true'));
$this->elementStart('label', array('for' => 'license',
'class' => 'checkbox'));
'value' => 'true']);
$this->elementStart('label', ['for' => 'license',
'class' => 'checkbox']);
// TRANS: OpenID plugin link text.
// TRANS: %s is a link to a license with the license name as link text.
$message = _m('My text and files are available under %s ' .
@ -171,12 +177,12 @@ class FinishopenidloginAction extends Action
// The second option is to attach this OpenID to an existing account
// on the local system, which they need to provide a password for.
$this->elementStart('form', array('method' => 'post',
$this->elementStart('form', ['method' => 'post',
'id' => 'account_connect',
'class' => 'form_settings',
'action' => common_local_url('finishopenidlogin')));
'action' => common_local_url('finishopenidlogin')]);
$this->hidden('token', common_session_token());
$this->elementStart('fieldset', array('id' => 'form_openid_createaccount'));
$this->elementStart('fieldset', ['id' => 'form_openid_createaccount']);
$this->element('legend', null,
// TRANS: Used as form legend for form in which to connect an OpenID to an existing user on the site.
_m('Connect existing account'));
@ -192,6 +198,11 @@ class FinishopenidloginAction extends Action
// TRANS: Field label in form in which to connect an OpenID to an existing user on the site.
$this->password('password', _m('Password'));
$this->elementEnd('li');
$this->elementStart('li');
// TRANS: Field label in form in which to connect an OpenID to an existing user on the site.
$this->checkbox('openid-sync', _m('Sync Account'), false,
_m('Syncronize GNU social profile with this OpenID identity.'));
$this->elementEnd('li');
$this->elementEnd('ul');
// TRANS: Button text in form in which to connect an OpenID to an existing user on the site.
$this->submit('connect', _m('BUTTON', 'Connect'));
@ -205,7 +216,7 @@ class FinishopenidloginAction extends Action
*
* @return string
*/
function getEmail()
public function getEmail()
{
$email = $this->trimmed('email');
if (!empty($email)) {
@ -232,7 +243,7 @@ class FinishopenidloginAction extends Action
return '';
}
function tryLogin()
public function tryLogin()
{
$consumer = oid_consumer();
@ -290,13 +301,13 @@ class FinishopenidloginAction extends Action
}
}
function message($msg)
public function message($msg)
{
$this->message_text = $msg;
$this->showPage();
}
function saveValues($display, $canonical, $sreg)
public function saveValues($display, $canonical, $sreg)
{
common_ensure_session();
$_SESSION['openid_display'] = $display;
@ -304,18 +315,18 @@ class FinishopenidloginAction extends Action
$_SESSION['openid_sreg'] = $sreg;
}
function getSavedValues()
public function getSavedValues()
{
return array($_SESSION['openid_display'],
return [$_SESSION['openid_display'],
$_SESSION['openid_canonical'],
$_SESSION['openid_sreg']);
$_SESSION['openid_sreg']];
}
function createNewUser()
public function createNewUser()
{
// FIXME: save invite code before redirect, and check here
if (!Event::handle('StartRegistrationTry', array($this))) {
if (!Event::handle('StartRegistrationTry', [$this])) {
return;
}
@ -364,7 +375,7 @@ class FinishopenidloginAction extends Action
$this->serverError(_m('Creating new account for OpenID that already has a user.'));
}
Event::handle('StartOpenIDCreateNewUser', array($canonical, &$sreg));
Event::handle('StartOpenIDCreateNewUser', [$canonical, &$sreg]);
$location = '';
if (!empty($sreg['country'])) {
@ -388,10 +399,10 @@ class FinishopenidloginAction extends Action
// XXX: add language
// XXX: add timezone
$args = array('nickname' => $nickname,
$args = ['nickname' => $nickname,
'email' => $email,
'fullname' => $fullname,
'location' => $location);
'location' => $location];
if (!empty($invite)) {
$args['code'] = $invite->code;
@ -401,7 +412,7 @@ class FinishopenidloginAction extends Action
$result = oid_link_user($user->id, $canonical, $display);
Event::handle('EndOpenIDCreateNewUser', array($user, $canonical, $sreg));
Event::handle('EndOpenIDCreateNewUser', [$user, $canonical, $sreg]);
oid_set_last($display);
common_set_user($user);
@ -411,15 +422,16 @@ class FinishopenidloginAction extends Action
}
unset($_SESSION['openid_rememberme']);
Event::handle('EndRegistrationTry', array($this));
Event::handle('EndRegistrationTry', [$this]);
common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)), 303);
common_redirect(common_local_url('showstream', ['nickname' => $user->nickname]), 303);
}
function connectUser()
public function connectUser()
{
$nickname = $this->trimmed('nickname');
$password = $this->trimmed('password');
$sync = $this->boolean('openid-sync');
if (!common_check_user($nickname, $password)) {
// TRANS: OpenID plugin message.
@ -445,10 +457,12 @@ class FinishopenidloginAction extends Action
$this->serverError(_m('Error connecting user to OpenID.'));
}
if (Event::handle('StartOpenIDUpdateUser', array($user, $canonical, &$sreg))) {
if ($sync) {
if (Event::handle('StartOpenIDUpdateUser', [$user, $canonical, &$sreg])) {
oid_update_user($user, $sreg);
}
Event::handle('EndOpenIDUpdateUser', array($user, $canonical, $sreg));
Event::handle('EndOpenIDUpdateUser', [$user, $canonical, $sreg]);
}
oid_set_last($display);
common_set_user($user);
@ -460,7 +474,7 @@ class FinishopenidloginAction extends Action
$this->goHome($user->nickname);
}
function goHome($nickname)
public function goHome($nickname)
{
$url = common_get_returnto();
if ($url) {
@ -468,14 +482,12 @@ class FinishopenidloginAction extends Action
common_set_returnto(null);
$url = common_inject_session($url);
} else {
$url = common_local_url('all',
array('nickname' =>
$nickname));
$url = common_local_url('all', ['nickname' => $nickname]);
}
common_redirect($url, 303);
}
function bestNewNickname($display, $sreg)
public function bestNewNickname($display, $sreg)
{
// Try the passed-in nickname
@ -508,7 +520,7 @@ class FinishopenidloginAction extends Action
return null;
}
function openidToNickname($openid)
public function openidToNickname($openid)
{
if (Auth_Yadis_identifierScheme($openid) == 'XRI') {
return $this->xriToNickname($openid);
@ -521,12 +533,12 @@ class FinishopenidloginAction extends Action
// 1. Plain hostname, like http://evanp.myopenid.com/
// 2. One element in path, like http://profile.typekey.com/EvanProdromou/
// or http://getopenid.com/evanprodromou
function urlToNickname($openid)
public function urlToNickname($openid)
{
return common_url_to_nickname($openid);
}
function xriToNickname($xri)
public function xriToNickname($xri)
{
$base = $this->xriBase($xri);
@ -540,7 +552,7 @@ class FinishopenidloginAction extends Action
}
}
function xriBase($xri)
public function xriBase($xri)
{
if (substr($xri, 0, 6) == 'xri://') {
return substr($xri, 6);

View File

@ -27,7 +27,9 @@
* @link http://status.net/
*/
if (!defined('GNUSOCIAL')) { exit(1); }
if (!defined('GNUSOCIAL')) {
exit(1);
}
require_once INSTALLDIR.'/plugins/OpenID/openid.php';
@ -49,7 +51,7 @@ class OpenidsettingsAction extends SettingsAction
*
* @return string Page title
*/
function title()
public function title()
{
// TRANS: Title of OpenID settings page for a user.
return _m('TITLE', 'OpenID settings');
@ -60,7 +62,7 @@ class OpenidsettingsAction extends SettingsAction
*
* @return string Instructions for use
*/
function getInstructions()
public function getInstructions()
{
// TRANS: Form instructions for OpenID settings.
// TRANS: This message contains Markdown links in the form [description](link).
@ -69,7 +71,7 @@ class OpenidsettingsAction extends SettingsAction
'Manage your associated OpenIDs from here.');
}
function showScripts()
public function showScripts()
{
parent::showScripts();
$this->autofocus('openid_url');
@ -82,15 +84,15 @@ class OpenidsettingsAction extends SettingsAction
*
* @return void
*/
function showContent()
public function showContent()
{
if (!common_config('openid', 'trusted_provider')) {
$this->elementStart('form', array('method' => 'post',
$this->elementStart('form', ['method' => 'post',
'id' => 'form_settings_openid_add',
'class' => 'form_settings',
'action' =>
common_local_url('openidsettings')));
$this->elementStart('fieldset', array('id' => 'settings_openid_add'));
common_local_url('openidsettings')]);
$this->elementStart('fieldset', ['id' => 'settings_openid_add']);
// TRANS: Fieldset legend.
$this->element('legend', null, _m('LEGEND', 'Add OpenID'));
@ -100,8 +102,14 @@ class OpenidsettingsAction extends SettingsAction
// TRANS: Field label.
$this->input('openid_url', _m('OpenID URL'), null,
// TRANS: Form guide.
_m('An OpenID URL which identifies you.'), null, true,
array('placeholder'=>'https://example.com/you'));
_m('An OpenID URL which identifies you.'),
null, true,
['placeholder'=>'https://example.com/you']);
$this->elementEnd('li');
$this->elementStart('li');
// TRANS: Field label.
$this->checkbox('openid-sync', _m('Sync Account'), false,
_m('Syncronize GNU social profile with this OpenID identity.'));
$this->elementEnd('li');
$this->elementEnd('ul');
// TRANS: Button text for adding an OpenID URL.
@ -120,7 +128,6 @@ class OpenidsettingsAction extends SettingsAction
$this->element('h2', null, _m('HEADER', 'Remove OpenID'));
if ($cnt == 1 && !$this->scoped->hasPassword()) {
$this->element('p', 'form_guide',
// TRANS: Form guide.
_m('Removing your only OpenID '.
@ -130,13 +137,10 @@ class OpenidsettingsAction extends SettingsAction
if ($oid->fetch()) {
$this->elementStart('p');
$this->element('a', array('href' => $oid->canonical),
$oid->display);
$this->element('a', ['href' => $oid->canonical], $oid->display);
$this->elementEnd('p');
}
} else {
$this->element('p', 'form_guide',
// TRANS: Form guide.
_m('You can remove an OpenID from your account '.
@ -144,16 +148,14 @@ class OpenidsettingsAction extends SettingsAction
$idx = 0;
while ($oid->fetch()) {
$this->elementStart('form',
array('method' => 'POST',
$this->elementStart('form', ['method' => 'POST',
'id' => 'form_settings_openid_delete' . $idx,
'class' => 'form_settings',
'action' =>
common_local_url('openidsettings')));
common_local_url('openidsettings')]);
$this->elementStart('fieldset');
$this->hidden('token', common_session_token());
$this->element('a', array('href' => $oid->canonical),
$oid->display);
$this->element('a', ['href' => $oid->canonical], $oid->display);
$this->hidden("openid_url{$idx}", $oid->canonical, 'openid_url');
// TRANS: Button text to remove an OpenID.
$this->submit("remove{$idx}", _m('BUTTON', 'Remove'), 'submit remove', 'remove');
@ -164,12 +166,12 @@ class OpenidsettingsAction extends SettingsAction
}
}
$this->elementStart('form', array('method' => 'post',
$this->elementStart('form', ['method' => 'post',
'id' => 'form_settings_openid_trustroots',
'class' => 'form_settings',
'action' =>
common_local_url('openidsettings')));
$this->elementStart('fieldset', array('id' => 'settings_openid_trustroots'));
common_local_url('openidsettings')]);
$this->elementStart('fieldset', ['id' => 'settings_openid_trustroots']);
// TRANS: Fieldset legend.
$this->element('legend', null, _m('OpenID Trusted Sites'));
$this->hidden('token', common_session_token());
@ -184,12 +186,14 @@ class OpenidsettingsAction extends SettingsAction
if ($user_openid_trustroot->find()) {
while ($user_openid_trustroot->fetch()) {
$this->elementStart('li');
$this->element('input', array('name' => 'openid_trustroot[]',
$this->element('input', ['name' => 'openid_trustroot[]',
'type' => 'checkbox',
'class' => 'checkbox',
'value' => $user_openid_trustroot->trustroot,
'id' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)));
$this->element('label', array('class'=>'checkbox', 'for' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)),
'id' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)]);
$this->element('label',
['class'=>'checkbox',
'for' => 'openid_trustroot_' . crc32($user_openid_trustroot->trustroot)],
$user_openid_trustroot->trustroot);
$this->elementEnd('li');
}
@ -227,8 +231,12 @@ class OpenidsettingsAction extends SettingsAction
// TRANS: Form validation error if no OpenID providers can be added.
throw new ServerException(_m('Cannot add new providers.'));
} else {
common_ensure_session();
$_SESSION['openid_sync'] = $this->boolean('openid-sync');
$result = oid_authenticate($this->trimmed('openid_url'), 'finishaddopenid');
if (is_string($result)) { // error message
unset($_SESSION['openid-sync']);
throw new ServerException($result);
}
return _('Added new provider.');
@ -253,12 +261,13 @@ class OpenidsettingsAction extends SettingsAction
*
* @return void
*/
function removeTrustroots()
public function removeTrustroots()
{
$trustroots = $this->arg('openid_trustroot', array());
$trustroots = $this->arg('openid_trustroot', []);
foreach ($trustroots as $trustroot) {
$user_openid_trustroot = User_openid_trustroot::pkeyGet(
array('user_id'=>$this->scoped->getID(), 'trustroot'=>$trustroot));
['user_id'=>$this->scoped->getID(), 'trustroot'=>$trustroot]
);
if ($user_openid_trustroot) {
$user_openid_trustroot->delete();
} else {
@ -279,7 +288,7 @@ class OpenidsettingsAction extends SettingsAction
*
* @return void
*/
function removeOpenid()
public function removeOpenid()
{
$oid = User_openid::getKV('canonical', $this->trimmed('openid_url'));
@ -304,7 +313,7 @@ class OpenidsettingsAction extends SettingsAction
*
* @return void
*/
function savePrefs()
public function savePrefs()
{
$orig = null;
$prefs = User_openid_prefs::getKV('user_id', $this->scoped->getID());