User talk:Brett and Restrict Access by Category and Group: Difference between pages

From Meta, a Wikimedia project coordination wiki
(Difference between pages)
Content deleted Content added
New page: {{subst:welcome}} --~~~~
 
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 1:
= Restrict Access by Category and Group =
<div class="borderc6 backgroundc2" style="padding:0.3em 0.5em 0.3em 0.5em; border-style:solid; border-width:medium; color: lightgrey;">
This pretends to add Restricted view of the Wiki content.
<center>{{Welcome/lang}}</center>
You have some Categories in your wiki and you want to make these visible only for some users.
==Welcome to Meta!==
 
Basically you can add a group with the same name of the category and then everybody in that group can access to that category.
<span style="color: black;">
 
<div class="lang-en" lang="en">
== Installing ==
Hello Brett, and welcome to the '''Wikimedia [[Meta:About|Meta-Wiki]]'''! This website is for coordinating and discussing all [[Complete list of Wikimedia projects|Wikimedia projects]]. You may find it useful to read our [[Meta:Policy|policy page]]. If you are interested in doing translations, visit [[Meta:Babylon]]. You can also leave a note on [[Meta:Babel]] or [[Metapub|Meta:Metapub]] (please read the instructions at the top of the page before posting there). If you would like, feel free to ask me questions on my [[Help:Talk page|talk page]]. Happy editing!
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:
</div></div> --[[User:Herbythyme|<font color="green">Herby</font>]] <b><sup><small><span style="color:#90F">[[User talk:Herbythyme|talk thyme]]</span></small></sup></b> 11:16, 1 July 2008 (UTC)
<pre>
# 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';
</pre>
 
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''':
<pre>
$wgWhitelistRead = array("Main_Page", "Especial:Userlogin");
</pre>
 
== Use ==
After you have added this extension, you only must create groups in your '''LocalSettings.php''':
<pre>
# We create a group named Base de Datos.
$wgGroupPermissions['Base de Datos']['read'] = true;
</pre>
 
== In other languages ==
If you work in Spanih then you must replace '''category''' with '''categoría''' in:
<pre>
if (in_array($key, $user->getGroups()) &&
(array_key_exists(strtolower("categoría:".str_replace(" ", "_", $key)),
array_change_key_case($title->getParentCategories(), CASE_LOWER)))) {
</pre>
and in then $wgWhitelistRead specification, you must replace '''Main_page''' with '''Portada''':
<pre>
$wgWhitelistRead = array("Portada", "Especial:Userlogin");
</pre>
== Version ==
This is probed on mediawiki '''1.12.0'''.
 
andy (dot) orencio.org
 
[[Category:MediaWiki extensions]]

Revision as of 12:33, 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 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