Extension:AdminWhiteList/version 1.1

From MediaWiki.org

Jump to: navigation, search
<?php 
 
if ( ! defined( 'MEDIAWIKI' ) ) die();
 
$wgExtensionCredits['other'][] = array(
    'name'=>'AdminWhiteList',
    'author'=>'Nino Dafonte <nino.dafonte@gmail.com>. Modifiée par José M. Ciges <zaipower@ciges.net>',
    'description'=>'Create/Remove admin users from array in LocalSettings. Its useful to add sysop and bureaucrat '.
                   'groups to users automatically when they log in',
    'version'=>'1.1'
);
 
/*
 * Changelog 1.0 -> 1.1
 * - The hook is 'UserLoadAfterLoadFromSession' (now this extension works also for users autoauthentified)
 * - If the user is not more in $wgAdminWhiteList array he/she is removed from sysop and bureaucrat MediaWiki groups
 * - The error reporting uses $wgAdminWhiteListDebug and $wgDebugLogGroups['adminwhitelist'] configuration settings
 */
 
//constants for error reporting
define("NONSENSITIVE", 1);
define("SENSITIVE", 2);
define("HIGHLYSENSITIVE", 3);
 
# Register hooks 
$wgHooks['UserLoadAfterLoadFromSession'][] = 'wfAdminWhiteList';
 
class AdminWhiteList {
 
	private $white_list = array();
	private $logged_user;
 
	public function __construct($user) {
		global $wgAdminWhiteList;	
 
		// 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->printDebug('AdminWhiteList Extension is loaded but not configured. Please add an '.
			                   'array info with your uids autorized as sysop. Variable: $wgAdminWhiteList', NONSENSITIVE);
		}
	}
 
	public function manageUserRights() {
		// test if the user logged is on our white list
		if (in_array($this->logged_user->mName, $this->white_list)) {
			$this->printDebug("We must include this user into sysop: {$this->logged_user->mName}", NONSENSITIVE);
 
			$aGroups = $this->logged_user->getEffectiveGroups(false);
			$this->printDebug("User groups:", SENSITIVE);
			$this->printDebug(implode(" :: ", $aGroups), SENSITIVE);
			// 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->printDebug("User added into sysop", NONSENSITIVE);
				} 
			else {
				$this->printDebug("User is already into sysop group. No action done", NONSENSITIVE);	
				}
 
			// 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->printDebug("User added into bureaucrat", NONSENSITIVE);
				} 
			else {
				$this->printDebug("User is already into bureaucrat group. No action done", NONSENSITIVE);	
				}
 
			}
		else {
			// test if the user should be removed from groups sysop and bureaucrat
			$aGroups = $this->logged_user->getEffectiveGroups(false);
			$this->printDebug("User groups:", SENSITIVE);
			$this->printDebug(implode(" :: ", $aGroups), SENSITIVE);
			// if the user in the sysop group we should remove the user from it
			if (in_array('sysop', $aGroups)) {
				// remove the logged user from the sysop group
				$this->printDebug("We must remove this user from sysop: {$this->logged_user->mName}", NONSENSITIVE);
				$this->logged_user->removeGroup('sysop');
				$this->printDebug("User removed from sysop", NONSENSITIVE);
				} 
			else {
				$this->printDebug("User not in sysop group. No action done", NONSENSITIVE);	
				}
 
			// if the user in the bureaucrat group we should remove the user from it
			if (in_array('bureaucrat', $aGroups)) {
				// remove the logged user from the sysop group
				$this->printDebug("We must remove this user from bureaucrat: {$this->logged_user->mName}", NONSENSITIVE);
				$this->logged_user->removeGroup('bureaucrat');
				$this->printDebug("User removed from bureaucrat", NONSENSITIVE);
				} 
			else {
				$this->printDebug("User not in bureaucrat group. No action done", NONSENSITIVE);	
				}
 
			}
 
		$this->printDebug("Returning to mediawiki loaders.", NONSENSITIVE);
	}
 
	/**
	 * Prints debugging information. $debugText is what you want to print, $debugVal
	 * is the level at which you want to print the information.
	 *
	 * @param string $debugText
	 * @param string $debugVal
	 * @access private
	 */
	private function printDebug( $debugText, $debugVal, $debugArr = null ) {
		global $wgAdminWhiteListDebug;
 
		if ( isset( $debugArr ) ) {
			if ( $wgAdminWhiteListDebug > $debugVal ) {
				$text = $debugText . " " . implode( "::", $debugArr );
				wfDebugLog( 'adminwhitelist', $text, false );
			}
		} else {
			if ( $wgAdminWhiteListDebug > $debugVal ) {
				wfDebugLog( 'adminwhitelist', $debugText, false );
			}
		}
	}
 
}
 
function wfAdminWhiteList(&$user) {	
 
	$objWhiteList = new AdminWhiteList($user);
	$objWhiteList->manageUserRights();
 
	// To continue the hook loaders
	return true;
 
}
 
?>