Difference between revisions of "Extension:IsAllowedHook.php"
From Organic Design wiki
(experimental IsAllowed hook) |
m (formatting) |
||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | {{voodoo}} | ||
+ | <source lang="php"> | ||
<?php | <?php | ||
− | # Extension:IsAllowedHook | + | # Extension:IsAllowedHook |
# - 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(' | + | define( 'ISALLOWED_VERSION', '0.0.0, 2007-10-09' ); |
$wgExtensionCredits['parserhook'][] = array( | $wgExtensionCredits['parserhook'][] = array( | ||
Line 13: | Line 15: | ||
'description' => 'Adds a new hook called "IsAllowed" which is called from the User::isAllowed method.', | 'description' => 'Adds a new hook called "IsAllowed" which is called from the User::isAllowed method.', | ||
'url' => 'http://www.organicdesign.co.nz/Extension:IsAllowedHook.php', | 'url' => 'http://www.organicdesign.co.nz/Extension:IsAllowedHook.php', | ||
− | 'version' => | + | 'version' => ISALLOWED_VERSION |
− | + | ); | |
+ | |||
+ | # 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 |
− | function isAllowed($action = "") { | + | $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; | |
− | # Replace the $wgUser object with an identical User2 instance | + | $wgUser = new $User2(); |
− | $oldUser = $wgUser; | + | foreach ( array_keys( get_class_vars( $User ) ) as $k ) $wgUser->$k = $oldUser->$k; |
− | $wgUser = new $User2(); | ||
− | foreach(array_keys(get_class_vars($User)) as $k) $wgUser->$k = $oldUser->$k; | ||
− | + | return true; | |
+ | } | ||
+ | </source> | ||
+ | [[Category:Extensions|IsAllowedHook]] |
Latest revision as of 14:17, 31 October 2016
<?php
# Extension:IsAllowedHook
# - Started: 2007-10-09
# - 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' => '[http://www.organicdesign.co.nz/nad 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
);
# 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;
}