Restrict Access by Category and Group

From Meta, a Wikimedia project coordination wiki
This is an archived version of this page, as edited by 194.224.226.6 (talk) at 12:33, 1 July 2008 (New page: = Restrict Access by Category and Group = This pretends to add Restricted view of the Wiki content. You have some Categories in your wiki and you want to make these visible only for some u...). It may differ significantly from the current version.
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Restrict Access by Category and Group

This pretends to add Restricted view of the Wiki content. You have some Categories in your wiki and you want to make these visible only for some users.

Basically you can add a group with the same name of the category and then everybody in that group can access to that category.

Installing

This extension only make a modification on the LocalSettings.php, and uses the Hooks/userCan function. Then you only must edit your LocalSettings.php and add at the end:

# This is to add a Group/Category Restricted access (by lodopidolo).
function userCanGrupoCategoria($title, $user, $action, $result) 
{
	global $wgGroupPermissions;
	global $wgWhitelistRead;

	$categoriaValida = false;
	if (count($title->getParentCategories()) == 0) {	
		$categoriaValida = true;
	} else {
		foreach( $wgGroupPermissions as $key => $value ) {
			if (in_array($key, $user->getGroups()) && (array_key_exists(strtolower("category:".str_replace(" ", "_", $key)), array_change_key_case($title->getParentCategories(), CASE_LOWER)))) {
				$categoriaValida = true;
			}
		}	
	}
	if (($user->isLoggedIn() && $categoriaValida) || in_array($title, $wgWhitelistRead))
	        $result = true;
	else
		$result = false;
	return $result;
}
$wgHooks['userCan'][] = 'userCanGrupoCategoria';

This code tell to mediawiki that has another userCan function, an this new function is going to read all group that user belongs and if is the same one another category then validate the access. Documents without category are validated too.

You must specify $wgWhitelistRead variable with the pages that everybody can acceess: Main_Page and UserLogin:

$wgWhitelistRead = array("Main_Page", "Especial:Userlogin");

Use

After you have added this extension, you only must create groups in your LocalSettings.php:

# We create a group named Base de Datos.
$wgGroupPermissions['Base de Datos']['read'] = true;

In other languages

If you work in Spanih then you must replace category with categoría in:

if (in_array($key, $user->getGroups()) && 
    (array_key_exists(strtolower("categoría:".str_replace(" ", "_", $key)), 
        array_change_key_case($title->getParentCategories(), CASE_LOWER)))) {

and in then $wgWhitelistRead specification, you must replace Main_page with Portada:

$wgWhitelistRead = array("Portada", "Especial:Userlogin");

Version

This is probed on mediawiki 1.12.0.

andy (dot) orencio.org