User:Barrylb/Usercan Hook

MediaWiki extensions manual
Barrylb/Usercan Hook
Release status: unknown
Implementation User rights
Description modifies user rights
Author(s) Barrylb
License No license specified
Download No link
Translate the Barrylb/Usercan Hook extension if it is available at translatewiki.net


You can restrict access to certain MediaWiki features without modifying the core files using the userCan hook available since version 1.6.0. Tested and working on version 1.6.7 and 1.7.1.

Note the 'userCan' hook for 'edit' actions acts in addition to any other security checks so if you have this $wgGroupPermissions['*']['edit'] = false; in your LocalSettings.php then it will act in addition, and disable anonymous editing entirely.

Put one the following in your LocalSettings.php file depending on your needs:

Limit non-logged-in users to editing talk pages only

edit
function fnMyUserCan( $title, $user, $action, $result ) {
	if( $action == 'edit' ) {
		if( !$title->isTalkPage() && !$user->isLoggedIn() )
			$result = false;
	}
}

$wgHooks['userCan'][] = 'fnMyUserCan';

Later versions require that you return true or false. Refer to userCan hook for more details. So, I had to modify the above code to return a value. In addition, you need to make sure that the other users (loggedIn) are able to edit any page and the notLoggedIn not able to edit pages just talks. The updated code is as follows:

function fnMyUserCan( $title, $user, $action, $result ) {
	if( $action == 'edit' ) {
		# If it is a talk page, we enable editing
		return $user->isLoggedIn() || $title->isTalkPage();
	}
	return true;
}

$wgHooks['userCan'][] = 'fnMyUserCan';

Any user can edit talk pages. Only Sysop can edit other pages

edit
function fnMyUserCan( $title, $user, $action, $result ) {
	if( $action == 'edit' ) {
		if( !$title->isTalkPage() && !$user->isSysop() )
			$result = false;
	}
}

$wgHooks['userCan'][] = 'fnMyUserCan';

Non-logged-in users can view source but not edit

edit
function fnMyUserCan( $title, $user, $action, $result ) {
	if( ( $action == 'edit' ) && !$user->isLoggedIn() )
		$result = false;
}
$wgHooks['userCan'][] = 'fnMyUserCan';

Later versions require that you return true or false. Refer to userCan hook for more details. So, I had to modify the above code to return a value.

function fnMyUserCan( $title, $user, $action, $result ) {
        return $action != 'edit' || $user->isLoggedIn();
}
$wgHooks['userCan'][] = 'fnMyUserCan';

Last change 21:54, 16 September 2008 (UTC)