allow methods to generate a "user does not have required permissions" error by throwi...
authorHappy-melon <happy-melon@users.mediawiki.org>
Tue, 12 Apr 2011 22:59:17 +0000 (22:59 +0000)
committerHappy-melon <happy-melon@users.mediawiki.org>
Tue, 12 Apr 2011 22:59:17 +0000 (22:59 +0000)
includes/Exception.php
includes/OutputPage.php

index 3151cd8..ea8986d 100644 (file)
@@ -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.
  */
index ad1a26d..1d1df0e 100644 (file)
@@ -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' ) );