<?php
if ( ! defined( 'MEDIAWIKI' ) ) die();
$wgExtensionCredits['other'][] = array(
'name'=>'AdminWhiteList',
'author'=>'Nino Dafonte <nino.dafonte@gmail.com>',
'description'=>'Create admin users from array in LocalSettings. Its useful to add sysop and bureaucrat '.
'groups to users automatically when they log in',
'version'=>'1.0'
);
$wgHooks['UserLoginComplete'][] = 'wfAdminWhiteList';
class AdminWhiteList {
private $debug_info = false;
private $white_list = array();
private $logged_user;
public function __construct($user) {
global $wgAdminWhiteList, $wgAdminWhiteListDebug;
// test if we must show debug info or not
if (isset($wgAdminWhiteListDebug) && $wgAdminWhiteListDebug) {
$this->debug_info = true;
} else {
$this->debug_info = false;
}
// test if the extension is configured properly
if (isset($wgAdminWhiteList) && is_array($wgAdminWhiteList)) {
$this->white_list = $wgAdminWhiteList;
// get logged user to add it to the user_groups table
$this->logged_user = $user;
} else {
$this->print_debug('AdminWhiteList Extension is loaded but not configured. Please add an '.
'array info with your uids autorized as sysop. Variable: $wgAdminWhiteList');
}
}
public function manageUserRights() {
// test if the user logged is on our white list
if (in_array($this->logged_user->mName, $this->white_list)) {
$this->print_debug("We must include this user into sysop: {$this->logged_user->mName}");
// test if the user is sysop right now
$aGroups = $this->logged_user->getEffectiveGroups(false);
$this->print_debug("User groups:");
$this->print_debug(implode(" :: ", $aGroups));
// if the user is already in the sysop group we have nothing to do
if (!in_array('sysop', $aGroups)) {
// add the logged user to the sysop group
$this->logged_user->addGroup('sysop');
$this->print_debug("User added into sysop");
} else {
$this->print_debug("User is already into sysop group. No action done");
}
// if the user is already in the bureaucrat group we have nothing to do
if (!in_array('bureaucrat', $aGroups)) {
// add the logged user to the sysop group
$this->logged_user->addGroup('bureaucrat');
$this->print_debug("User added into bureaucrat");
} else {
$this->print_debug("User is already into bureaucrat group. No action done");
}
}
$this->print_debug("Returning to mediawiki loaders.");
}
private function print_debug($str) {
if ($this->debug_info) {
echo $str . "<br />";
}
}
}
function wfAdminWhiteList(&$user) {
$objWhiteList = new AdminWhiteList($user);
$objWhiteList->manageUserRights();
// To continue the hook loaders
return true;
}
?>