Restrict Access by Category and Group: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
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...
 
Line 48: Line 48:


== In other languages ==
== In other languages ==
If you work in Spanih then you must replace '''category''' with '''categoría''' in:
If you work in Spanish then you must replace '''category''' with '''categoría''' in:
<pre>
<pre>
if (in_array($key, $user->getGroups()) &&
if (in_array($key, $user->getGroups()) &&
Line 58: Line 58:
$wgWhitelistRead = array("Portada", "Especial:Userlogin");
$wgWhitelistRead = array("Portada", "Especial:Userlogin");
</pre>
</pre>

== Version ==
== Version ==
This is probed on mediawiki '''1.12.0'''.
This is probed on mediawiki '''1.12.0'''.

Revision as of 12:36, 1 July 2008

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 Spanish 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