r41527 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r41526‎ | r41527 | r41528 >
Date:01:12, 2 October 2008
Author:aaron
Status:old (Comments)
Tags:
Comment:
* Pull isLocked() and isHidden() up to User using callbacks
* Add quick globalblock check function to User
* Add 'locked'/'globally blocked' notices to checkuser (bug 15272) (bug 15792)
Modified paths:
  • /trunk/extensions/CentralAuth/CentralAuthPlugin.php (modified) (history)
  • /trunk/extensions/CheckUser/CheckUser.i18n.php (modified) (history)
  • /trunk/extensions/CheckUser/CheckUser_body.php (modified) (history)
  • /trunk/extensions/GlobalBlocking/GlobalBlocking.php (modified) (history)
  • /trunk/phase3/includes/AuthPlugin.php (modified) (history)
  • /trunk/phase3/includes/User.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/User.php
@@ -183,7 +183,7 @@
184184 /**
185185 * \type{\bool} Whether the cache variables have been loaded.
186186 */
187 - var $mDataLoaded;
 187+ var $mDataLoaded, $mAuthLoaded;
188188
189189 /**
190190 * \type{\string} Initialization data source if mDataLoaded==false. May be one of:
@@ -199,7 +199,8 @@
200200 /** @name Lazy-initialized variables, invalidated with clearInstanceCache */
201201 //@{
202202 var $mNewtalk, $mDatePreference, $mBlockedby, $mHash, $mSkin, $mRights,
203 - $mBlockreason, $mBlock, $mEffectiveGroups;
 203+ $mBlockreason, $mBlock, $mEffectiveGroups, $mBlockedGlobally,
 204+ $mLocked, $mHideName;
204205 //@}
205206
206207 /**
@@ -252,6 +253,23 @@
253254 }
254255 wfProfileOut( __METHOD__ );
255256 }
 257+
 258+ protected function callAuthPlugin( $fname /* $args */ ) {
 259+ $args = func_get_args();
 260+ array_shift( $args );
 261+ // Load auth plugin conterpart functions for User functions
 262+ if( !$this->mAuthLoaded ) {
 263+ global $wgAuth;
 264+ $this->mAuthCallbacks = array();
 265+ $wgAuth->setUserCallbacks( $this, $this->mAuthCallbacks );
 266+ $this->mAuthLoaded = true;
 267+ }
 268+ // Try to call the auth plugin version of this function
 269+ if( isset($this->mAuthCallbacks[$fname]) && is_callable($this->mAuthCallbacks[$fname]) ) {
 270+ return call_user_func_array( $this->mAuthCallbacks[$fname], $args );
 271+ }
 272+ return NULL;
 273+ }
256274
257275 /**
258276 * Load user table data, given mId has already been set.
@@ -1295,6 +1313,59 @@
12961314 $this->getBlockedStatus();
12971315 return $this->mBlockreason;
12981316 }
 1317+
 1318+ /**
 1319+ * Check if user is blocked on all wikis.
 1320+ * Do not use for actual edit permission checks!
 1321+ * This is intented for quick UI checks.
 1322+ *
 1323+ * @param $ip \type{\string} IP address, uses current client if none given
 1324+ * @return \type{\bool} True if blocked, false otherwise
 1325+ */
 1326+ function isBlockedGlobally( $ip = '' ) {
 1327+ if( $this->mBlockedGlobally !== null ) {
 1328+ return $this->mBlockedGlobally;
 1329+ }
 1330+ // User is already an IP?
 1331+ if( IP::isIPAddress( $this->getName() ) ) {
 1332+ $ip = $this->getName();
 1333+ } else if( !$ip ) {
 1334+ $ip = wfGetIP();
 1335+ }
 1336+ $blocked = false;
 1337+ wfRunHooks( 'UserIsBlockedGlobally', array( &$this, $ip, &$blocked ) );
 1338+ $this->mBlockedGlobally = (bool)$blocked;
 1339+ return $this->mBlockedGlobally;
 1340+ }
 1341+
 1342+ /**
 1343+ * Check if user account is locked
 1344+ *
 1345+ * @return \type{\bool} True if locked, false otherwise
 1346+ */
 1347+ function isLocked() {
 1348+ if( $this->mLocked !== null ) {
 1349+ return $this->mLocked;
 1350+ }
 1351+ $this->mLocked = (bool)$this->callAuthPlugin( __FUNCTION__ );
 1352+ return $this->mLocked;
 1353+ }
 1354+
 1355+ /**
 1356+ * Check if user account is hidden
 1357+ *
 1358+ * @return \type{\bool} True if hidden, false otherwise
 1359+ */
 1360+ function isHidden() {
 1361+ if( $this->mHideName !== null ) {
 1362+ return $this->mHideName;
 1363+ }
 1364+ $this->getBlockedStatus();
 1365+ if( !$this->mHideName ) {
 1366+ $this->mHideName = (bool)$this->callAuthPlugin( __FUNCTION__ );
 1367+ }
 1368+ return $this->mHideName;
 1369+ }
12991370
13001371 /**
13011372 * Get the user's ID.
Index: trunk/phase3/includes/AuthPlugin.php
@@ -241,4 +241,11 @@
242242 function getCanonicalName( $username ) {
243243 return $username;
244244 }
 245+
 246+ /**
 247+ * Adds any functionality to a User object in context of the auth system
 248+ */
 249+ function setUserCallbacks( $user, &$callbacks ) {
 250+ return true;
 251+ }
245252 }
Index: trunk/extensions/GlobalBlocking/GlobalBlocking.php
@@ -27,6 +27,7 @@
2828 $wgExtensionMessagesFiles['GlobalBlocking'] = "$dir/GlobalBlocking.i18n.php";
2929 $wgExtensionAliasesFiles['GlobalBlocking'] = "$dir/GlobalBlocking.alias.php";
3030 $wgHooks['getUserPermissionsErrorsExpensive'][] = 'GlobalBlocking::getUserPermissionsErrors';
 31+$wgHooks['UserIsBlockedGlobally'][] = 'GlobalBlocking::isBlockedGlobally';
3132
3233 $wgAutoloadClasses['SpecialGlobalBlock'] = "$dir/SpecialGlobalBlock.php";
3334 $wgSpecialPages['GlobalBlock'] = 'SpecialGlobalBlock';
@@ -77,20 +78,38 @@
7879 if ($action == 'read' || !$wgApplyGlobalBlocks) {
7980 return true;
8081 }
 82+ $ip = wfGetIp();
 83+ $blockError = self::getUserBlockErrors( $user, $ip );
 84+ if( !empty($blockError) ) {
 85+ $result[] = $blockError;
 86+ return false;
 87+ }
 88+ return true;
 89+ }
 90+
 91+ static function isBlockedGlobally( &$user, $ip, &$blocked ) {
 92+ $blockError = self::getUserBlockErrors( $user, $ip );
 93+ if( $blockError ) {
 94+ $blocked = true;
 95+ return false;
 96+ }
 97+ return true;
 98+ }
8199
 100+ static function getUserBlockErrors( $user, $ip ) {
82101 $dbr = GlobalBlocking::getGlobalBlockingSlave();
83 - $ip = wfGetIp();
84102
85103 $hex_ip = IP::toHex( $ip );
86 -
87104 $ip_pattern = substr( $hex_ip, 0, 4 ) . '%'; // Don't bother checking blocks out of this /16.
88105
89 - $conds = array( 'gb_range_end>='.$dbr->addQuotes($hex_ip), // This block in the given range.
90 - 'gb_range_start<='.$dbr->addQuotes($hex_ip),
91 - 'gb_range_start like ' . $dbr->addQuotes( $ip_pattern ),
92 - 'gb_expiry>'.$dbr->addQuotes($dbr->timestamp(wfTimestampNow())) );
 106+ $conds = array(
 107+ 'gb_range_end>='.$dbr->addQuotes($hex_ip), // This block in the given range.
 108+ 'gb_range_start<='.$dbr->addQuotes($hex_ip),
 109+ 'gb_range_start like ' . $dbr->addQuotes( $ip_pattern ),
 110+ 'gb_expiry>'.$dbr->addQuotes($dbr->timestamp(wfTimestampNow()))
 111+ );
93112
94 - if (!$user->isAnon())
 113+ if ( !$user->isAnon() )
95114 $conds['gb_anon_only'] = 0;
96115
97116 // Get the block
@@ -99,12 +118,12 @@
100119 // Check for local whitelisting
101120 if (GlobalBlocking::getWhitelistInfo( $block->gb_id ) ) {
102121 // Block has been whitelisted.
103 - return true;
 122+ return array();
104123 }
105124
106125 if ( $user->isAllowed( 'ipblock-exempt' ) ) {
107126 // User is exempt from IP blocks.
108 - return true;
 127+ return array();
109128 }
110129
111130 $expiry = Block::formatExpiry( $block->gb_expiry );
@@ -114,11 +133,9 @@
115134 $display_wiki = self::getWikiName( $block->gb_by_wiki );
116135 $user_display = self::maybeLinkUserpage( $block->gb_by_wiki, $block->gb_by );
117136
118 - $result[] = array('globalblocking-blocked', $user_display, $display_wiki, $block->gb_reason, $expiry);
119 - return false;
 137+ return array('globalblocking-blocked', $user_display, $display_wiki, $block->gb_reason, $expiry);
120138 }
121 -
122 - return true;
 139+ return array();
123140 }
124141
125142 static function getGlobalBlockingMaster() {
Index: trunk/extensions/CentralAuth/CentralAuthPlugin.php
@@ -244,4 +244,10 @@
245245 }
246246 }
247247 }
 248+
 249+ function setUserCallbacks( $user, &$callbacks ) {
 250+ $central = CentralAuthUser::getInstance( $user );
 251+ $callbacks['isLocked'] = array( $central, 'isLocked' );
 252+ $callbacks['isHidden'] = array( $central, 'isHidden' );
 253+ }
248254 }
Index: trunk/extensions/CheckUser/CheckUser_body.php
@@ -787,12 +787,16 @@
788788 $block->fromMaster( false ); // use slaves
789789 $ip = IP::isIPAddress( $name ) ? // If an account, get last IP
790790 $name : $users_infosets[$name][count($users_infosets[$name])-1][0];
 791+ # Load user object
 792+ $user = User::newFromName( $name );
791793 if( $block->load( $ip, $users_ids[$name] ) ) {
 794+ // Range blocked?
792795 if( IP::isIPAddress($block->mAddress) && strpos($block->mAddress,'/') ) {
793796 $userpage = Title::makeTitle( NS_USER, $block->mAddress );
794797 $blocklog = $this->sk->makeKnownLinkObj( $logs, wfMsgHtml('checkuser-blocked'),
795798 'type=block&page=' . urlencode( $userpage->getPrefixedText() ) );
796799 $flags[] = '<strong>(' . $blocklog . ' - ' . $block->mAddress . ')</strong>';
 800+ // Auto blocked?
797801 } else if( $block->mAuto ) {
798802 $blocklog = $this->sk->makeKnownLinkObj( $blocklist,
799803 wfMsgHtml('checkuser-blocked'), 'ip=' . urlencode( "#$block->mId" ) );
@@ -803,23 +807,21 @@
804808 'type=block&page=' . urlencode( $userpage->getPrefixedText() ) );
805809 $flags[] = '<strong>(' . $blocklog . ')</strong>';
806810 }
 811+ // Blocked on all wikis?
 812+ } else if( $user->isBlockedGlobally( $ip ) ) {
 813+ $flags[] = '<strong>(' . wfMsgHtml('checkuser-gblocked') . ')</strong>';
807814 } else if( self::userWasBlocked( $name ) ) {
808815 $userpage = Title::makeTitle( NS_USER, $name );
809816 $blocklog = $this->sk->makeKnownLinkObj( $logs, wfMsgHtml('checkuser-wasblocked'),
810817 'type=block&page=' . urlencode( $userpage->getPrefixedText() ) );
811818 $flags[] = '<strong>(' . $blocklog . ')</strong>';
812819 }
813 - # Check how many accounts the user made recently?
814 - if( $ip ) {
815 - $key = wfMemcKey( 'acctcreate', 'ip', $ip );
816 - $count = intval( $wgMemc->get( $key ) );
817 - if( $count ) {
818 - $flags[] = '<strong>[' . wfMsgExt('checkuser-accounts',array('parsemag'),$count) . ']</strong>';
819 - }
820 - }
821820 # Check for extra user rights...
822821 if( $users_ids[$name] ) {
823822 $user = User::newFromId( $users_ids[$name] );
 823+ if( $user->isLocked() ) {
 824+ $flags[] = '<b>(' . wfMsgHtml('checkuser-locked') . ')</b>';
 825+ }
824826 $list = array();
825827 foreach( $user->getGroups() as $group ) {
826828 $list[] = self::buildGroupLink( $group );
@@ -829,6 +831,14 @@
830832 $flags[] = '<i>(' . $groups . ')</i>';
831833 }
832834 }
 835+ # Check how many accounts the user made recently?
 836+ if( $ip ) {
 837+ $key = wfMemcKey( 'acctcreate', 'ip', $ip );
 838+ $count = intval( $wgMemc->get( $key ) );
 839+ if( $count ) {
 840+ $flags[] = '<strong>[' . wfMsgExt('checkuser-accounts',array('parsemag'),$count) . ']</strong>';
 841+ }
 842+ }
833843 $s .= implode(' ',$flags);
834844 $s .= '<ol>';
835845 # List out each IP/XFF combo for this username
Index: trunk/extensions/CheckUser/CheckUser.i18n.php
@@ -41,6 +41,8 @@
4242 'checkuser-log-fail' => 'Unable to add log entry',
4343 'checkuser-nolog' => 'No log file found.',
4444 'checkuser-blocked' => 'Blocked',
 45+ 'checkuser-gblocked' => 'Blocked globally',
 46+ 'checkuser-locked' => 'Locked',
4547 'checkuser-wasblocked' => 'Previously blocked',
4648 'checkuser-massblock' => 'Block selected users',
4749 'checkuser-massblock-text' => 'Selected accounts will be blocked indefinitely, with autoblocking enabled and account creation disabled.

Follow-up revisions

RevisionCommit summaryAuthorDate
r41684Partial revert of r41527. Do this in a cleaner way.aaron02:45, 5 October 2008

Comments

#Comment by Brion VIBBER (talk | contribs)   23:49, 2 October 2008

Whoa, this is some big scary stuff.

#Comment by Voice of All (talk | contribs)   02:45, 5 October 2008

Scar stuff removed in r41684

#Comment by Voice of All (talk | contribs)   02:46, 5 October 2008
  • Scary, heh

Status & tagging log