]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
OAuth stuff adapted for FormAction
authorMikael Nordfeldth <mmn@hethane.se>
Thu, 16 Jul 2015 22:20:46 +0000 (00:20 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Thu, 16 Jul 2015 23:07:19 +0000 (01:07 +0200)
TODO: Break OAuth out into a plugin.

actions/newapplication.php
actions/oauthappssettings.php
actions/oauthconnectionssettings.php
classes/Profile.php
lib/applicationlist.php
lib/connectedappslist.php

index 37bede0d72f3c547a3a4e3eb15bde6253c831d00..5032bb74951a8f05a8096ec9115999a33b2f8b33 100644 (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
  * @link     http://status.net/
  */
-class NewApplicationAction extends FormAction
+class NewApplicationAction extends SettingsAction
 {
     function title()
     {
@@ -54,6 +54,7 @@ class NewApplicationAction extends FormAction
         if ($this->arg('cancel')) {
             common_redirect(common_local_url('oauthappssettings'), 303);
         } elseif ($this->arg('save')) {
+            //trySave will never return, just throw exception or redirect
             $this->trySave();
         }
 
@@ -72,7 +73,7 @@ class NewApplicationAction extends FormAction
         return _('Use this form to register a new application.');
     }
 
-    private function trySave()
+    protected function trySave()
     {
         $name         = $this->trimmed('name');
         $description  = $this->trimmed('description');
@@ -137,7 +138,7 @@ class NewApplicationAction extends FormAction
         $app->query('BEGIN');
 
         $app->name         = $name;
-        $app->owner        = $this->scoped->id;
+        $app->owner        = $this->scoped->getID();
         $app->description  = $description;
         $app->source_url   = $source_url;
         $app->organization = $organization;
index e9b6280feb81ad65e1f0c1374b1d22de2c641b37..43e9b33663ac62c6da1ee94e5694fb71d19221af 100644 (file)
@@ -43,19 +43,11 @@ if (!defined('GNUSOCIAL')) { exit(1); }
 
 class OauthappssettingsAction extends SettingsAction
 {
-    var $page = 0;
+    protected $page = null;
 
-    function prepare($args)
+    protected function doPreparation()
     {
-        parent::prepare($args);
-        $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;
+        $this->page = $this->int('page') ?: 1;
     }
 
     /**
@@ -82,21 +74,13 @@ class OauthappssettingsAction extends SettingsAction
         return _('Applications you have registered');
     }
 
-    /**
-     * Content area of the page
-     *
-     * @return void
-     */
-
     function showContent()
     {
-        $user = common_current_user();
-
         $offset = ($this->page - 1) * APPS_PER_PAGE;
         $limit  =  APPS_PER_PAGE + 1;
 
         $application = new Oauth_application();
-        $application->owner = $user->id;
+        $application->owner = $this->scoped->getID();
         $application->whereAdd("name != 'anonymous'");
         $application->limit($offset, $limit);
         $application->orderBy('created DESC');
@@ -105,7 +89,7 @@ class OauthappssettingsAction extends SettingsAction
         $cnt = 0;
 
         if ($application) {
-            $al = new ApplicationList($application, $user, $this);
+            $al = new ApplicationList($application, $this->scoped, $this);
             $cnt = $al->show();
             if (0 == $cnt) {
                 $this->showEmptyListMessage();
@@ -131,34 +115,11 @@ class OauthappssettingsAction extends SettingsAction
 
     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.'));
 
         $this->elementStart('div', 'guide');
         $this->raw(common_markup_to_html($message));
         $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;
-        }
-    }
 }
index a3ba7eda3930a05510d262c35c86fb456b68dc26..0c5a14344370e609df043fb67262683df6501a96 100644 (file)
@@ -42,15 +42,14 @@ if (!defined('GNUSOCIAL')) { exit(1); }
  */
 class OauthconnectionssettingsAction extends SettingsAction
 {
-    var $page        = null;
-    var $oauth_token = null;
+    var $page = null;
 
-    function prepare($args)
+    protected $oauth_token = null;
+
+    protected function doPreparation()
     {
-        parent::prepare($args);
         $this->oauth_token = $this->arg('oauth_token');
-        $this->page = ($this->arg('page')) ? ($this->arg('page') + 0) : 1;
-        return true;
+        $this->page = $this->int('page') ?: 1;
     }
 
     /**
@@ -83,18 +82,15 @@ class OauthconnectionssettingsAction extends SettingsAction
 
     function showContent()
     {
-        $user    = common_current_user();
-        $profile = $user->getProfile();
-
         $offset = ($this->page - 1) * APPS_PER_PAGE;
         $limit  =  APPS_PER_PAGE + 1;
 
-        $connection = $user->getConnectedApps($offset, $limit);
+        $connection = $this->scoped->getConnectedApps($offset, $limit);
 
         $cnt = 0;
 
         if (!empty($connection)) {
-            $cal = new ConnectedAppsList($connection, $user, $this);
+            $cal = new ConnectedAppsList($connection, $this->scoped, $this);
             $cnt = $cal->show();
         }
 
@@ -107,7 +103,7 @@ class OauthconnectionssettingsAction extends SettingsAction
             $cnt > APPS_PER_PAGE,
             $this->page,
             'connectionssettings',
-            array('nickname' => $user->nickname)
+            array('nickname' => $this->scoped->getNickname())
         );
     }
 
@@ -121,24 +117,14 @@ class OauthconnectionssettingsAction extends SettingsAction
      *
      * @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')) {
-            $this->revokeAccess($this->oauth_token);
-        } else {
-            // TRANS: Client error when submitting a form with unexpected information.
-            $this->clientError(_('Unexpected form submission.'), 401);
+            return $this->revokeAccess($this->oauth_token);
         }
+
+        // TRANS: Client error when submitting a form with unexpected information.
+        throw new ClientException(_('Unexpected form submission.'), 401);
     }
 
     /**
index f628965a7482a1be41b054e76fdf3def1e16651a..384eaa07263e7414056ee7c8445ecaff5bdf0964 100644 (file)
@@ -1623,4 +1623,9 @@ class Profile extends Managed_DataObject
     public function setPref($namespace, $topic, $data) {
         return Profile_prefs::setData($this, $namespace, $topic, $data);
     }
+
+    public function getConnectedApps($offset=0, $limit=null)
+    {
+        return $this->getUser()->getConnectedApps($offset, $limit);
+    }
 }
index b2cc572e3ee145f31f1db3356216b1e8ebf07464..ab51a73096c50c637dd6cc078f1c5fddde2a5daa 100644 (file)
@@ -46,16 +46,12 @@ class ApplicationList extends Widget
     /** Owner of this list */
     var $owner = null;
 
-    /** Action object using us. */
-    var $action = null;
-
-    function __construct($application, $owner=null, $action=null)
+    function __construct($application, Profile $owner, Action $out=null)
     {
-        parent::__construct($action);
+        parent::__construct($out);
 
         $this->application = $application;
         $this->owner       = $owner;
-        $this->action      = $action;
     }
 
     function show()
@@ -69,7 +65,7 @@ class ApplicationList extends Widget
             if($cnt > APPS_PER_PAGE) {
                 break;
             }
-            $this->showapplication();
+            $this->showApplication();
         }
 
         $this->out->elementEnd('ul');
@@ -79,8 +75,6 @@ class ApplicationList extends Widget
 
     function showApplication()
     {
-        $user = common_current_user();
-
         $this->out->elementStart('li', array('class' => 'application h-entry',
                                              'id'    => 'oauthclient-' . $this->application->id));
 
index 7e5eb7482cd91edf1c9d8188c5533bb9fe50c952..c2a27e75a83c72692068bf2e37ae02269be2dee6 100644 (file)
@@ -46,18 +46,14 @@ class ConnectedAppsList extends Widget
     /** Owner of this list */
     var $owner = null;
 
-    /** Action object using us. */
-    var $action = null;
-
-    function __construct($connection, $owner=null, $action=null)
+    function __construct($connection, Profile $owner, Action $out=null)
     {
-        parent::__construct($action);
+        parent::__construct($out);
 
         common_debug("ConnectedAppsList constructor");
 
         $this->connection = $connection;
-        $this->owner       = $owner;
-        $this->action      = $action;
+        $this->owner      = $owner;
     }
 
     /* Override this in subclasses. */