Difference between revisions of "Extension:IsAllowedHook.php"

From Organic Design wiki
m
(that works - eeezeee!)
Line 3: Line 3:
 
# - Started: 2007-10-09
 
# - Started: 2007-10-09
 
# - 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');
  
Line 16: Line 16:
 
);
 
);
  
# Create a new User class ($User2) by extending the existing one with an overridden isAllowed method
+
$wgHooks['userCan'][] = 'wfSetupIsAllowed';
$User  = get_class($wgUser);
+
function wfSetupIsAllowed() {
$User2 = $User.'2';
+
global $wgUser;
eval("class $User2 extends $User".' {
+
static $first = 0; 
function isAllowed($action = "") {
+
if ($first++) return true;
$result = NULL;
+
 
wfRunHooks("IsAllowed",array(&$this,$action,&$result));
+
# Create a new User class ($User2) by extending the existing one with an overridden isAllowed method
return $result === NULL ? $result = parent::isAllowed($action) : $result;
+
$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;
  
# Replace the $wgUser object with an identical $User2 instance
+
return true;
$oldUser = $wgUser;
+
}
$wgUser  = new $User2();
 
foreach(array_keys(get_class_vars($User)) as $k) $wgUser->$k = $oldUser->$k;
 
  
 
?>
 
?>

Revision as of 02:19, 9 October 2007

<?php

  1. Extension:IsAllowedHookTemplate:Php
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.
  1. - Started: 2007-10-09
  2. - 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 );

$wgHooks['userCan'][] = 'wfSetupIsAllowed'; function wfSetupIsAllowed() { global $wgUser; static $first = 0; if ($first++) return true;

# 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; }

?>