From: Happy-melon Date: Tue, 12 Apr 2011 22:59:17 +0000 (+0000) Subject: allow methods to generate a "user does not have required permissions" error by throwi... X-Git-Tag: 1.31.0-rc.0~30884 X-Git-Url: http://git.cyclocoop.org/%24action?a=commitdiff_plain;h=2e1e9bf0e685cb7b432d2e916e0e640ee167b18f;p=lhc%2Fweb%2Fwiklou.git allow methods to generate a "user does not have required permissions" error by throwing an exception rather than calling $wgOut->permissionRequired(). Currently somewhat circular as the exception goes back to OutputPage::showErrorPage(), but hopefully that global dependency can be reduced in future. --- diff --git a/includes/Exception.php b/includes/Exception.php index 3151cd8bdd..ea8986d883 100644 --- a/includes/Exception.php +++ b/includes/Exception.php @@ -311,28 +311,65 @@ class FatalError extends MWException { } /** + * An error page which can definitely be safely rendered using the OutputPage * @ingroup Exception */ class ErrorPageError extends MWException { - public $title, $msg; + public $title, $msg, $params; /** * Note: these arguments are keys into wfMsg(), not text! */ - function __construct( $title, $msg ) { + function __construct( $title, $msg, $params = null ) { $this->title = $title; $this->msg = $msg; + $this->params = $params; parent::__construct( wfMsg( $msg ) ); } function report() { global $wgOut; - $wgOut->showErrorPage( $this->title, $this->msg ); + $wgOut->showErrorPage( $this->title, $this->msg, $this->params ); $wgOut->output(); } } +/** + * Show an error when a user tries to do something they do not have the necessary + * permissions for. + */ +class PermissionsError extends ErrorPageError { + public $permission; + + function __construct( $permission ) { + global $wgLang; + + $this->permission = $permission; + + $groups = array_map( + array( 'User', 'makeGroupLinkWiki' ), + User::getGroupsWithPermission( $this->permission ) + ); + + if( $groups ) { + parent::__construct( + 'badaccess', + 'badaccess-groups', + array( + $wgLang->commaList( $groups ), + count( $groups ) + ) + ); + } else { + parent::__construct( + 'badaccess', + 'badaccess-group0' + ); + } + } +} + /** * Install an exception handler for MediaWiki exception types. */ diff --git a/includes/OutputPage.php b/includes/OutputPage.php index ad1a26d97b..1d1df0efcb 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2044,28 +2044,11 @@ class OutputPage { /** * Display an error page noting that a given permission bit is required. - * + * @deprecated since 1.18, just throw the exception directly * @param $permission String: key required */ public function permissionRequired( $permission ) { - $this->setPageTitle( wfMsg( 'badaccess' ) ); - $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) ); - $this->setRobotPolicy( 'noindex,nofollow' ); - $this->setArticleRelated( false ); - $this->mBodytext = ''; - - $groups = array_map( array( 'User', 'makeGroupLinkWiki' ), - User::getGroupsWithPermission( $permission ) ); - if( $groups ) { - $this->addWikiMsg( - 'badaccess-groups', - $this->getContext()->getLang()->commaList( $groups ), - count( $groups ) - ); - } else { - $this->addWikiMsg( 'badaccess-group0' ); - } - $this->returnToMain(); + throw new PermissionsError( $permission ); } /** @@ -2073,8 +2056,7 @@ class OutputPage { */ public function loginToUse() { if( $this->getUser()->isLoggedIn() ) { - $this->permissionRequired( 'read' ); - return; + throw new PermissionsError( 'read' ); } $this->setPageTitle( wfMsg( 'loginreqtitle' ) );