OAuth stuff adapted for FormAction

TODO: Break OAuth out into a plugin.
This commit is contained in:
Mikael Nordfeldth 2015-07-17 00:20:46 +02:00
parent ba5a43f2f9
commit a6e299a2fc
6 changed files with 34 additions and 91 deletions

View File

@ -41,7 +41,7 @@ if (!defined('GNUSOCIAL')) { exit(1); }
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class NewApplicationAction extends FormAction class NewApplicationAction extends SettingsAction
{ {
function title() function title()
{ {
@ -54,6 +54,7 @@ class NewApplicationAction extends FormAction
if ($this->arg('cancel')) { if ($this->arg('cancel')) {
common_redirect(common_local_url('oauthappssettings'), 303); common_redirect(common_local_url('oauthappssettings'), 303);
} elseif ($this->arg('save')) { } elseif ($this->arg('save')) {
//trySave will never return, just throw exception or redirect
$this->trySave(); $this->trySave();
} }
@ -72,7 +73,7 @@ class NewApplicationAction extends FormAction
return _('Use this form to register a new application.'); return _('Use this form to register a new application.');
} }
private function trySave() protected function trySave()
{ {
$name = $this->trimmed('name'); $name = $this->trimmed('name');
$description = $this->trimmed('description'); $description = $this->trimmed('description');
@ -137,7 +138,7 @@ class NewApplicationAction extends FormAction
$app->query('BEGIN'); $app->query('BEGIN');
$app->name = $name; $app->name = $name;
$app->owner = $this->scoped->id; $app->owner = $this->scoped->getID();
$app->description = $description; $app->description = $description;
$app->source_url = $source_url; $app->source_url = $source_url;
$app->organization = $organization; $app->organization = $organization;

View File

@ -43,19 +43,11 @@ if (!defined('GNUSOCIAL')) { exit(1); }
class OauthappssettingsAction extends SettingsAction class OauthappssettingsAction extends SettingsAction
{ {
var $page = 0; protected $page = null;
function prepare($args) protected function doPreparation()
{ {
parent::prepare($args); $this->page = $this->int('page') ?: 1;
$this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
if (!common_logged_in()) {
// TRANS: Message displayed to an anonymous user trying to view OAuth application list.
$this->clientError(_('You must be logged in to list your applications.'));
}
return true;
} }
/** /**
@ -82,21 +74,13 @@ class OauthappssettingsAction extends SettingsAction
return _('Applications you have registered'); return _('Applications you have registered');
} }
/**
* Content area of the page
*
* @return void
*/
function showContent() function showContent()
{ {
$user = common_current_user();
$offset = ($this->page - 1) * APPS_PER_PAGE; $offset = ($this->page - 1) * APPS_PER_PAGE;
$limit = APPS_PER_PAGE + 1; $limit = APPS_PER_PAGE + 1;
$application = new Oauth_application(); $application = new Oauth_application();
$application->owner = $user->id; $application->owner = $this->scoped->getID();
$application->whereAdd("name != 'anonymous'"); $application->whereAdd("name != 'anonymous'");
$application->limit($offset, $limit); $application->limit($offset, $limit);
$application->orderBy('created DESC'); $application->orderBy('created DESC');
@ -105,7 +89,7 @@ class OauthappssettingsAction extends SettingsAction
$cnt = 0; $cnt = 0;
if ($application) { if ($application) {
$al = new ApplicationList($application, $user, $this); $al = new ApplicationList($application, $this->scoped, $this);
$cnt = $al->show(); $cnt = $al->show();
if (0 == $cnt) { if (0 == $cnt) {
$this->showEmptyListMessage(); $this->showEmptyListMessage();
@ -131,34 +115,11 @@ class OauthappssettingsAction extends SettingsAction
function showEmptyListMessage() function showEmptyListMessage()
{ {
// TRANS: Empty list message on page with OAuth applications. // TRANS: Empty list message on page with OAuth applications. Markup allowed
$message = sprintf(_('You have not registered any applications yet.')); $message = sprintf(_('You have not registered any applications yet.'));
$this->elementStart('div', 'guide'); $this->elementStart('div', 'guide');
$this->raw(common_markup_to_html($message)); $this->raw(common_markup_to_html($message));
$this->elementEnd('div'); $this->elementEnd('div');
} }
/**
* Handle posts to this form
*
* Based on the button that was pressed, muxes out to other functions
* to do the actual task requested.
*
* All sub-functions reload the form with a message -- success or failure.
*
* @return void
*/
function handlePost()
{
// CSRF protection
$token = $this->trimmed('token');
if (!$token || $token != common_session_token()) {
$this->showForm(_('There was a problem with your session token. '.
'Try again, please.'));
return;
}
}
} }

View File

@ -43,14 +43,13 @@ if (!defined('GNUSOCIAL')) { exit(1); }
class OauthconnectionssettingsAction extends SettingsAction class OauthconnectionssettingsAction extends SettingsAction
{ {
var $page = null; var $page = null;
var $oauth_token = null;
function prepare($args) protected $oauth_token = null;
protected function doPreparation()
{ {
parent::prepare($args);
$this->oauth_token = $this->arg('oauth_token'); $this->oauth_token = $this->arg('oauth_token');
$this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1; $this->page = $this->int('page') ?: 1;
return true;
} }
/** /**
@ -83,18 +82,15 @@ class OauthconnectionssettingsAction extends SettingsAction
function showContent() function showContent()
{ {
$user = common_current_user();
$profile = $user->getProfile();
$offset = ($this->page - 1) * APPS_PER_PAGE; $offset = ($this->page - 1) * APPS_PER_PAGE;
$limit = APPS_PER_PAGE + 1; $limit = APPS_PER_PAGE + 1;
$connection = $user->getConnectedApps($offset, $limit); $connection = $this->scoped->getConnectedApps($offset, $limit);
$cnt = 0; $cnt = 0;
if (!empty($connection)) { if (!empty($connection)) {
$cal = new ConnectedAppsList($connection, $user, $this); $cal = new ConnectedAppsList($connection, $this->scoped, $this);
$cnt = $cal->show(); $cnt = $cal->show();
} }
@ -107,7 +103,7 @@ class OauthconnectionssettingsAction extends SettingsAction
$cnt > APPS_PER_PAGE, $cnt > APPS_PER_PAGE,
$this->page, $this->page,
'connectionssettings', 'connectionssettings',
array('nickname' => $user->nickname) array('nickname' => $this->scoped->getNickname())
); );
} }
@ -121,24 +117,14 @@ class OauthconnectionssettingsAction extends SettingsAction
* *
* @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(_('There was a problem with your session token. '.
'Try again, please.'));
return;
}
if ($this->arg('revoke')) { if ($this->arg('revoke')) {
$this->revokeAccess($this->oauth_token); return $this->revokeAccess($this->oauth_token);
} else {
// TRANS: Client error when submitting a form with unexpected information.
$this->clientError(_('Unexpected form submission.'), 401);
} }
// TRANS: Client error when submitting a form with unexpected information.
throw new ClientException(_('Unexpected form submission.'), 401);
} }
/** /**

View File

@ -1623,4 +1623,9 @@ class Profile extends Managed_DataObject
public function setPref($namespace, $topic, $data) { public function setPref($namespace, $topic, $data) {
return Profile_prefs::setData($this, $namespace, $topic, $data); return Profile_prefs::setData($this, $namespace, $topic, $data);
} }
public function getConnectedApps($offset=0, $limit=null)
{
return $this->getUser()->getConnectedApps($offset, $limit);
}
} }

View File

@ -46,16 +46,12 @@ class ApplicationList extends Widget
/** Owner of this list */ /** Owner of this list */
var $owner = null; var $owner = null;
/** Action object using us. */ function __construct($application, Profile $owner, Action $out=null)
var $action = null;
function __construct($application, $owner=null, $action=null)
{ {
parent::__construct($action); parent::__construct($out);
$this->application = $application; $this->application = $application;
$this->owner = $owner; $this->owner = $owner;
$this->action = $action;
} }
function show() function show()
@ -69,7 +65,7 @@ class ApplicationList extends Widget
if($cnt > APPS_PER_PAGE) { if($cnt > APPS_PER_PAGE) {
break; break;
} }
$this->showapplication(); $this->showApplication();
} }
$this->out->elementEnd('ul'); $this->out->elementEnd('ul');
@ -79,8 +75,6 @@ class ApplicationList extends Widget
function showApplication() function showApplication()
{ {
$user = common_current_user();
$this->out->elementStart('li', array('class' => 'application h-entry', $this->out->elementStart('li', array('class' => 'application h-entry',
'id' => 'oauthclient-' . $this->application->id)); 'id' => 'oauthclient-' . $this->application->id));

View File

@ -46,18 +46,14 @@ class ConnectedAppsList extends Widget
/** Owner of this list */ /** Owner of this list */
var $owner = null; var $owner = null;
/** Action object using us. */ function __construct($connection, Profile $owner, Action $out=null)
var $action = null;
function __construct($connection, $owner=null, $action=null)
{ {
parent::__construct($action); parent::__construct($out);
common_debug("ConnectedAppsList constructor"); common_debug("ConnectedAppsList constructor");
$this->connection = $connection; $this->connection = $connection;
$this->owner = $owner; $this->owner = $owner;
$this->action = $action;
} }
/* Override this in subclasses. */ /* Override this in subclasses. */