needLogin renamed checkLogin and made a property

Action extended classes now can set 'needLogin' as a protected property,
which is defaulted to 'false'. However, FormAction defaults this to 'true'
because most of the form actions will require a current login to be valid.

NewgroupAction, NewmessageAction, NewnoticeAction are all affected by this
commit and in the future we will migrate each potential formaction to the
proper class parent tree. :)
This commit is contained in:
Mikael Nordfeldth 2013-09-02 11:58:47 +02:00
parent e5e3aeb4e6
commit f0e967fefd
5 changed files with 25 additions and 41 deletions

View File

@ -24,6 +24,7 @@
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net> * @author Sarven Capadisli <csarven@status.net>
* @copyright 2008-2009 StatusNet, Inc. * @copyright 2008-2009 StatusNet, Inc.
* @copyright 2013 Free Software Foundation, Inc.
* @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/
*/ */
@ -58,11 +59,6 @@ class NewgroupAction extends FormAction
{ {
parent::prepare($args); parent::prepare($args);
if (!common_logged_in()) {
// TRANS: Client error displayed trying to create a group while not logged in.
$this->clientError(_('You must be logged in to create a group.'), 403);
}
// $this->scoped is the current user profile // $this->scoped is the current user profile
if (!$this->scoped->hasRight(Right::CREATEGROUP)) { if (!$this->scoped->hasRight(Right::CREATEGROUP)) {
// TRANS: Client exception thrown when a user tries to create a group while banned. // TRANS: Client exception thrown when a user tries to create a group while banned.

View File

@ -25,11 +25,12 @@
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @author Sarven Capadisli <csarven@status.net> * @author Sarven Capadisli <csarven@status.net>
* @copyright 2008-2009 StatusNet, Inc. * @copyright 2008-2009 StatusNet, Inc.
* @copyright 2013 Free Software Foundation, Inc.
* @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/
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
@ -77,10 +78,6 @@ class NewmessageAction extends FormAction
{ {
parent::prepare($args); parent::prepare($args);
if (!common_logged_in()) {
$this->needLogin();
}
$user = $this->scoped->getUser(); $user = $this->scoped->getUser();
$this->content = $this->trimmed('content'); $this->content = $this->trimmed('content');

View File

@ -63,31 +63,6 @@ class NewnoticeAction extends FormAction
return _m('TITLE','New notice'); return _m('TITLE','New notice');
} }
/**
* Handle input, produce output
*
* Switches based on GET or POST method. On GET, shows a form
* for posting a notice. On POST, saves the results of that form.
*
* Results may be a full page, or just a single notice list item,
* depending on whether AJAX was requested.
*
* @param array $args $_REQUEST contents
*
* @return void
*/
protected function prepare(array $args=array())
{
parent::prepare($args);
if (!common_logged_in()) {
// TRANS: Error message displayed when trying to perform an action that requires a logged in user.
$this->clientError(_('Not logged in.'), 403);
}
return true;
}
/** /**
* This handlePost saves a new notice, based on arguments * This handlePost saves a new notice, based on arguments
* *

View File

@ -62,6 +62,7 @@ class Action extends HTMLOutputter // lawsuit
protected $action = false; protected $action = false;
protected $ajax = false; protected $ajax = false;
protected $menus = true; protected $menus = true;
protected $needLogin = false;
// The currently scoped profile // The currently scoped profile
protected $scoped = null; protected $scoped = null;
@ -142,6 +143,10 @@ class Action extends HTMLOutputter // lawsuit
StatusNet::setAjax(true); StatusNet::setAjax(true);
} }
if ($this->needLogin) {
$this->checkLogin(); // if not logged in, this redirs/excepts
}
$this->scoped = Profile::current(); $this->scoped = Profile::current();
return true; return true;
@ -1377,17 +1382,27 @@ class Action extends HTMLOutputter // lawsuit
} }
/** /**
* Redirect to login page (with returnto) * If not logged in, take appropriate action (redir or exception)
* *
* @return nothing * @param boolean $redir Redirect to login if not logged in
*
* @return boolean true if logged in (never returns if not)
*/ */
function needLogin() public function checkLogin($redir=true)
{ {
// this might be updated with a login check before redirecting if (common_logged_in()) {
return true;
}
if ($redir==true) {
common_set_returnto($_SERVER['REQUEST_URI']); common_set_returnto($_SERVER['REQUEST_URI']);
common_redirect(common_local_url('login')); common_redirect(common_local_url('login'));
} }
// TRANS: Error message displayed when trying to perform an action that requires a logged in user.
$this->clientError(_('Not logged in.'), 403);
}
/** /**
* Returns the current URL * Returns the current URL
* *

View File

@ -45,6 +45,7 @@ class FormAction extends Action
{ {
protected $form = null; protected $form = null;
protected $type = null; protected $type = null;
protected $needLogin = true;
protected function prepare(array $args=array()) { protected function prepare(array $args=array()) {
parent::prepare($args); parent::prepare($args);