r108175 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108174‎ | r108175 | r108176 >
Date:20:29, 5 January 2012
Author:ialex
Status:resolved
Tags:
Comment:
Per request of Aaron Schulz, follow-up r102187: added new 'TitleReadWhitelist' hook to allow extensions to bypass core (and other extensions) checks to allow an user to read the page
Modified paths:
  • /trunk/phase3/docs/hooks.txt (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)

Diff [purge]

Index: trunk/phase3/docs/hooks.txt
@@ -1926,6 +1926,14 @@
19271927 $pageid: database ID of the page that's been moved
19281928 $redirid: database ID of the created redirect
19291929
 1930+'TitleReadWhitelist': called at the end of read permissions checks, just before
 1931+ adding the default error message if nothing allows the user to read the page.
 1932+ Return false will prevent core from adding its error message, but you need
 1933+ to removed extensions' error messages from $errors yourself.
 1934+$title: Title object being checked against
 1935+$user: Current user object
 1936+&$errors: errors
 1937+
19301938 'UndeleteForm::showHistory': called in UndeleteForm::showHistory, after a
19311939 PageArchive object has been created but before any further processing is done.
19321940 &$archive: PageArchive object
Index: trunk/phase3/includes/Title.php
@@ -1965,6 +1965,7 @@
19661966 * @return Array list of errors
19671967 */
19681968 private function checkReadPermissions( $action, $user, $errors, $doExpensiveQueries, $short ) {
 1969+ global $wgWhitelistRead;
19691970 static $useShortcut = null;
19701971
19711972 # Initialize the $useShortcut boolean, to determine if we can skip quite a bit of code below
@@ -1991,61 +1992,57 @@
19921993 }
19931994 }
19941995
1995 - # Shortcut for public wikis, allows skipping quite a bit of code
1996 - if ( $useShortcut ) {
1997 - return $errors;
1998 - }
 1996+ $whitelisted = false;
19991997
2000 - # If the user is allowed to read pages, he is allowed to read all pages
2001 - if ( $user->isAllowed( 'read' ) ) {
2002 - return $errors;
2003 - }
2004 -
2005 - # Always grant access to the login page.
2006 - # Even anons need to be able to log in.
2007 - if ( $this->isSpecial( 'Userlogin' )
 1998+ if ( $useShortcut ) {
 1999+ # Shortcut for public wikis, allows skipping quite a bit of code
 2000+ $whitelisted = true;
 2001+ } elseif ( $user->isAllowed( 'read' ) ) {
 2002+ # If the user is allowed to read pages, he is allowed to read all pages
 2003+ $whitelisted = true;
 2004+ } elseif ( $this->isSpecial( 'Userlogin' )
20082005 || $this->isSpecial( 'ChangePassword' )
20092006 || $this->isSpecial( 'PasswordReset' )
20102007 ) {
2011 - return $errors;
2012 - }
2013 -
2014 - # Time to check the whitelist
2015 - global $wgWhitelistRead;
2016 -
2017 - # Only do these checks is there's something to check against
2018 - if ( is_array( $wgWhitelistRead ) && count( $wgWhitelistRead ) ) {
2019 - # Check for explicit whitelisting
 2008+ # Always grant access to the login page.
 2009+ # Even anons need to be able to log in.
 2010+ $whitelisted = true;
 2011+ } elseif ( is_array( $wgWhitelistRead ) && count( $wgWhitelistRead ) ) {
 2012+ # Time to check the whitelist
 2013+ # Only do these checks is there's something to check against
20202014 $name = $this->getPrefixedText();
20212015 $dbName = $this->getPrefixedDBKey();
20222016
20232017 // Check with and without underscores
20242018 if ( in_array( $name, $wgWhitelistRead, true ) || in_array( $dbName, $wgWhitelistRead, true ) ) {
2025 - return $errors;
2026 - }
2027 -
2028 - # Old settings might have the title prefixed with
2029 - # a colon for main-namespace pages
2030 - if ( $this->getNamespace() == NS_MAIN ) {
 2019+ # Check for explicit whitelisting
 2020+ $whitelisted = true;
 2021+ } elseif ( $this->getNamespace() == NS_MAIN ) {
 2022+ # Old settings might have the title prefixed with
 2023+ # a colon for main-namespace pages
20312024 if ( in_array( ':' . $name, $wgWhitelistRead ) ) {
2032 - return $errors;
 2025+ $whitelisted = true;
20332026 }
2034 - }
2035 -
2036 - # If it's a special page, ditch the subpage bit and check again
2037 - if ( $this->isSpecialPage() ) {
 2027+ } elseif ( $this->isSpecialPage() ) {
 2028+ # If it's a special page, ditch the subpage bit and check again
20382029 $name = $this->getDBkey();
20392030 list( $name, /* $subpage */ ) = SpecialPageFactory::resolveAlias( $name );
20402031 if ( $name !== false ) {
20412032 $pure = SpecialPage::getTitleFor( $name )->getPrefixedText();
20422033 if ( in_array( $pure, $wgWhitelistRead, true ) ) {
2043 - return $errors;
 2034+ $whitelisted = true;
20442035 }
20452036 }
20462037 }
20472038 }
20482039
2049 - $errors[] = $this->missingPermissionError( $action, $short );
 2040+ # If the user is allowed to read tge page; don't call the hook
 2041+ if ( $whitelisted && !count( $errors ) ) {
 2042+ return array();
 2043+ } elseif ( wfRunHooks( 'TitleReadWhitelist', array( $this, $user, &$errors ) ) && !$whitelisted ) {
 2044+ $errors[] = $this->missingPermissionError( $action, $short );
 2045+ }
 2046+
20502047 return $errors;
20512048 }
20522049

Follow-up revisions

RevisionCommit summaryAuthorDate
r108193* Cleanup for r108175: made if easier for extensions to interact with each ot...aaron06:16, 6 January 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r102187* Merged Title::userCanRead() check in Title::getUserPermissionsErrors()...ialex19:59, 6 November 2011

Status & tagging log