Difference between revisions of "Extension:IsAllowedHook.php"

From Organic Design wiki
({{voodoo}})
m (formatting)
Line 6: Line 6:
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  
if (!defined('MEDIAWIKI')) die('Not an entry point.');
+
if ( !defined( 'MEDIAWIKI' ) ) die('Not an entry point.' );
  
define('ISALLOWED_VERSION','0.0.0, 2007-10-09');
+
define( 'ISALLOWED_VERSION', '0.0.0, 2007-10-09' );
  
 
$wgExtensionCredits['parserhook'][] = array(
 
$wgExtensionCredits['parserhook'][] = array(
Line 16: Line 16:
 
'url'        => 'http://www.organicdesign.co.nz/Extension:IsAllowedHook.php',
 
'url'        => 'http://www.organicdesign.co.nz/Extension:IsAllowedHook.php',
 
'version'    => ISALLOWED_VERSION
 
'version'    => ISALLOWED_VERSION
);
+
);
  
 
# This is the earliest hook I can find after which $wgUser is an instance of the User class
 
# This is the earliest hook I can find after which $wgUser is an instance of the User class
Line 24: Line 24:
 
# Return unless first call
 
# Return unless first call
 
static $first = 0;   
 
static $first = 0;   
if ($first++) return true;
+
if ( $first++ ) return true;
 
global $wgUser;
 
global $wgUser;
  
 
# Create a new User class ($User2) by extending the existing one with an overridden isAllowed method
 
# Create a new User class ($User2) by extending the existing one with an overridden isAllowed method
$User  = get_class($wgUser);
+
$User  = get_class( $wgUser );
 
$User2 = $User.'2';
 
$User2 = $User.'2';
eval("class $User2 extends $User".' {
+
eval( "class $User2 extends $User".' {
function isAllowed($action = "") {
+
function isAllowed( $action = "" ) {
 
$result = NULL;
 
$result = NULL;
wfRunHooks("IsAllowed",array(&$this,$action,&$result));
+
wfRunHooks("IsAllowed",array( &$this, $action, &$result ) );
return $result === NULL ? $result = parent::isAllowed($action) : $result;
+
return $result === NULL ? $result = parent::isAllowed( $action ) : $result;
}
+
}
}');
+
}' );
  
 
# Replace the $wgUser object with an identical $User2 instance
 
# Replace the $wgUser object with an identical $User2 instance
 
$oldUser = $wgUser;
 
$oldUser = $wgUser;
 
$wgUser  = new $User2();
 
$wgUser  = new $User2();
foreach(array_keys(get_class_vars($User)) as $k) $wgUser->$k = $oldUser->$k;
+
foreach ( array_keys( get_class_vars( $User ) ) as $k ) $wgUser->$k = $oldUser->$k;
  
 
return true;
 
return true;
}
+
}
 
 
?>
 
 
</php>
 
</php>

Revision as of 07:51, 2 July 2009

Voodoo.svg This code exhibits voodoo programming techniques. The most common of these is extending an instance's class at runtime after it has been instantiated, a technique that can be used to provide additional hooks into existing code without requiring modification of code-base files. For a list of all our scripts which exhibit voodoo, see Category:Code that uses voodoo.

<php> <?php

  1. Extension:IsAllowedHook
  2. - Started: 2007-10-09
  3. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)

if ( !defined( 'MEDIAWIKI' ) ) die('Not an entry point.' );

define( 'ISALLOWED_VERSION', '0.0.0, 2007-10-09' );

$wgExtensionCredits['parserhook'][] = array( 'name' => "IsAllowedHook", 'author' => 'User:Nad', 'description' => 'Adds a new hook called "IsAllowed" which is called from the User::isAllowed method.', 'url' => 'http://www.organicdesign.co.nz/Extension:IsAllowedHook.php', 'version' => ISALLOWED_VERSION );

  1. This is the earliest hook I can find after which $wgUser is an instance of the User class

$wgHooks['userCan'][] = 'wfSetupIsAllowed'; function wfSetupIsAllowed() {

# Return unless first call static $first = 0; if ( $first++ ) return true; global $wgUser;

# Create a new User class ($User2) by extending the existing one with an overridden isAllowed method $User = get_class( $wgUser ); $User2 = $User.'2'; eval( "class $User2 extends $User".' { function isAllowed( $action = "" ) { $result = NULL; wfRunHooks("IsAllowed",array( &$this, $action, &$result ) ); return $result === NULL ? $result = parent::isAllowed( $action ) : $result; } }' );

# Replace the $wgUser object with an identical $User2 instance $oldUser = $wgUser; $wgUser = new $User2(); foreach ( array_keys( get_class_vars( $User ) ) as $k ) $wgUser->$k = $oldUser->$k;

return true; } </php>