r85928 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85927‎ | r85928 | r85929 >
Date:22:59, 12 April 2011
Author:happy-melon
Status:resolved
Tags:
Comment:
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.
Modified paths:
  • /trunk/phase3/includes/Exception.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/OutputPage.php
@@ -2044,28 +2044,11 @@
20452045
20462046 /**
20472047 * Display an error page noting that a given permission bit is required.
2048 - *
 2048+ * @deprecated since 1.18, just throw the exception directly
20492049 * @param $permission String: key required
20502050 */
20512051 public function permissionRequired( $permission ) {
2052 - $this->setPageTitle( wfMsg( 'badaccess' ) );
2053 - $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
2054 - $this->setRobotPolicy( 'noindex,nofollow' );
2055 - $this->setArticleRelated( false );
2056 - $this->mBodytext = '';
2057 -
2058 - $groups = array_map( array( 'User', 'makeGroupLinkWiki' ),
2059 - User::getGroupsWithPermission( $permission ) );
2060 - if( $groups ) {
2061 - $this->addWikiMsg(
2062 - 'badaccess-groups',
2063 - $this->getContext()->getLang()->commaList( $groups ),
2064 - count( $groups )
2065 - );
2066 - } else {
2067 - $this->addWikiMsg( 'badaccess-group0' );
2068 - }
2069 - $this->returnToMain();
 2052+ throw new PermissionsError( $permission );
20702053 }
20712054
20722055 /**
@@ -2073,8 +2056,7 @@
20742057 */
20752058 public function loginToUse() {
20762059 if( $this->getUser()->isLoggedIn() ) {
2077 - $this->permissionRequired( 'read' );
2078 - return;
 2060+ throw new PermissionsError( 'read' );
20792061 }
20802062
20812063 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
Index: trunk/phase3/includes/Exception.php
@@ -311,29 +311,66 @@
312312 }
313313
314314 /**
 315+ * An error page which can definitely be safely rendered using the OutputPage
315316 * @ingroup Exception
316317 */
317318 class ErrorPageError extends MWException {
318 - public $title, $msg;
 319+ public $title, $msg, $params;
319320
320321 /**
321322 * Note: these arguments are keys into wfMsg(), not text!
322323 */
323 - function __construct( $title, $msg ) {
 324+ function __construct( $title, $msg, $params = null ) {
324325 $this->title = $title;
325326 $this->msg = $msg;
 327+ $this->params = $params;
326328 parent::__construct( wfMsg( $msg ) );
327329 }
328330
329331 function report() {
330332 global $wgOut;
331333
332 - $wgOut->showErrorPage( $this->title, $this->msg );
 334+ $wgOut->showErrorPage( $this->title, $this->msg, $this->params );
333335 $wgOut->output();
334336 }
335337 }
336338
337339 /**
 340+ * Show an error when a user tries to do something they do not have the necessary
 341+ * permissions for.
 342+ */
 343+class PermissionsError extends ErrorPageError {
 344+ public $permission;
 345+
 346+ function __construct( $permission ) {
 347+ global $wgLang;
 348+
 349+ $this->permission = $permission;
 350+
 351+ $groups = array_map(
 352+ array( 'User', 'makeGroupLinkWiki' ),
 353+ User::getGroupsWithPermission( $this->permission )
 354+ );
 355+
 356+ if( $groups ) {
 357+ parent::__construct(
 358+ 'badaccess',
 359+ 'badaccess-groups',
 360+ array(
 361+ $wgLang->commaList( $groups ),
 362+ count( $groups )
 363+ )
 364+ );
 365+ } else {
 366+ parent::__construct(
 367+ 'badaccess',
 368+ 'badaccess-group0'
 369+ );
 370+ }
 371+ }
 372+}
 373+
 374+/**
338375 * Install an exception handler for MediaWiki exception types.
339376 */
340377 function wfInstallExceptionHandler() {

Follow-up revisions

RevisionCommit summaryAuthorDate
r85930Follow-up r85928: AutoLoader entry.happy-melon23:02, 12 April 2011

Status & tagging log