Extension:CategoryPermissions
From MediaWiki.org
If you need per-page or partial page access restrictions, you are advised to install an appropriate content management package. MediaWiki was not written to provide per-page access restrictions, and almost all hacks or patches promising to add them will likely have flaws somewhere, which could lead to exposure of confidential data. We are not responsible for anything being leaked, leading to loss of funds or one's job.
For further details, see Security issues with authorization extensions
For further details, see Security issues with authorization extensions
|
Release status: beta |
|||
|---|---|---|---|
| Implementation | User rights | ||
| Description | User permissions restrictions based on page categories | ||
| Author(s) | Matthew Vernon | ||
| Last Version | 0.4 (2008-07-20) | ||
| MediaWiki | 1.7.1 through 1.11 | ||
| License | GPL v2 | ||
| Download | no link | ||
|
|||
|
check usage (experimental) |
|||
Contents |
[edit] What can this extension do?
Extends permissions checking by allowing group access to pages that have specific categories. This extension utilizes the usercan hook, however standard permissions checks are still performed. Use this extension at your own risk. Any security scheme based on userCan will not prevent page content from being displayed in search results for versions before MW 1.10![1]
[edit] How secure is this extension?
| Security issues with authorization extensions | |||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Transclusion | unsafe (can be fixed with $wgNonincludableNamespaces) | ||||||||||||||||||
| Preloading | safe (with MediaWiki 1.12+) | ||||||||||||||||||
| Diff/oldid | safe | ||||||||||||||||||
| Raw/render | safe | ||||||||||||||||||
| Edit section | safe | ||||||||||||||||||
| Search | safe (with MediaWiki 1.10+) | ||||||||||||||||||
| Export | safe (with MediaWiki 1.10+) | ||||||||||||||||||
| RSS/atom | safe (with MediaWiki 1.12+) | ||||||||||||||||||
| Recentchanges | ? | ||||||||||||||||||
| Redirects | safe | ||||||||||||||||||
| Known bugs |
|
||||||||||||||||||
[edit] Changes
- 0.4 - Integrated fix for return null bug in version 1.10 and up. Modified debug lines per Richard Hartmann.
- 0.31 - Changed line 49 per anonymous note
- 0.3 - No longer bypasses standard permissions except when denying access. Previous version was working around a problem that was outside the extension in my server configuration.
[edit] Usage
See Installation
[edit] Installation
- Copy the code shown in the Code section below to extensions/CategoryPermissions.php
- Make changes to LocalSettings.php as shown below
[edit] Changes to LocalSettings.php
require_once("$IP/extensions/CategoryPermissions.php");
$wgGroupDefaultAllow=true; //set to true to allow everyone access to pages without a category
$wgCategoryExclusive=array("Category:cat_name","Category:cat2_name");//deny access to these categories for anyone not in the group
$wgGroupPermissions['group_name']['Category:categoryname_read']=true;
$wgGroupPermissions['group_name']['Category:categoryname_edit']=true;
$wgGroupPermissions['group_name']['Category:categoryname_move']=true;
$wgGroupPermissions['group_name']['Category:categoryname_create']=true;
$wgGroupPermissions['group_name']['*_read']=true; //allow access to all categories
If you are using a language other than English, replace Category in each of the text strings (NOT the variable names) with the language specific word for Category.
The permissions are checked as follows:
- Each category in the list is checked for permissions
- If a category is in the array $wgCategoryExclusive and the user does not have permissions, access is immediately rejected. Users must have permissions for all categories on the page specified in $wgCategoryExclusive in order to have access granted.
- If the user has permissions for any category and has not been rejected by an exclusive category, they are granted access
- If global access (*_read, etc) is set for this user, then access is granted
- If the page has no categories, $wgGroupDefaultAllow is is used to grant or deny access
- Access is denied ... but we should never get to this step
[edit] Code
<?php /* * Custom Permissions Scheme using Categories * based on Extension:NamespacePermissions by Petr Andreev * * Provides separate permissions for each action (read,edit,create,move) based * on category tags on pages. * * Author: Matthew Vernon * Additional Contributions by Carsten Zimmermann and Richard Hartmann * * Licensed under GPL v2 - use,change,enjoy * * Usage: * * require_once('extensions/CategoryPermissions.php'); * $wgCategoryExclusive=array("Category:cat_name","Category:cat2_name");//deny acces to these categories for anyone not in the group * $wgGroupAlwaysAllow=''; //set a group name to ALWAYS allow access to this group * $wgGroupDefaultAllow=true; //set to true to allow everyone access to pages without a category * * * //add groups to category permissions by: * $wgGroupPermissions['group_name']['Category:categoryname_read']=true; * $wgGroupPermissions['group_name']['Category:categoryname_edit']=true; * $wgGroupPermissions['group_name']['Category:categoryname_move']=true; * $wgGroupPermissions['group_name']['Category:categoryname_create']=true; * * //allow access for a group to all categories like this * $wgGroupPermissions['group_name']['*_read']=true; */ //set up hook $wgExtensionFunctions[] = "wfCategoryPermissions"; function wfCategoryPermissions() { global $wgHooks; // use the userCan hook to check permissions $wgHooks[ 'userCan' ][] = 'checkCategoryPermissions'; } //register extension $wgExtensionCredits['parserhook'][] = array( 'name'=>'CategoryPermissions', 'author'=>'Matthew Vernon', 'url'=>'http://www.mediawiki.org/wiki/Extension:CategoryPermissions'); //turn on debug messages by changing to 1 or true // Remember to set $wgDebugLogFile in LocalSettings.php as well. define("__debug_permissions", 0); function checkCategoryPermissions( $title, $user, $action, $result ) { global $wgGroupDefaultAllow, $wgGroupPermissions, $wgCategoryExclusive; $user_allowed=false; //get categories for this page $parentCategories=$title->getParentCategories(); //scan list of categories, if any if(is_array($parentCategories)) { foreach( $parentCategories as $category=>$dd) { if(__debug_permissions)wfDebug("checkCategoryPermissions on line ".__LINE__.": Trying action {$action} on category {$category} for user {$user->mName}\n"); if( $user->isAllowed("{$category}_{$action}") ) { if(__debug_permissions)wfDebug("checkCategoryPermissions on line ".__LINE__.": Category-action {$category}_{$action} ALLOWED on {$title->mPrefixedText} to user {$user->mName}\n"); $user_allowed=true; } else { if(in_array($category,$wgCategoryExclusive)) { //if category is in $wgCategoryExclusive then deny anyone who doesn't have explicit access if(__debug_permissions)wfDebug("checkCategoryPermissions on line ".__LINE__.": Category-action {$category}_{$action} DENIED on {$title->mPrefixedText} to user {$user->mName}: Exclusive Category\n"); $result=false; return false; } } }//foreach( $parentCategories as $category=>$dd) }//if($parentCategories) //if we are here, then no exclusive categories have rejected us if($user_allowed==true) { $result=null; return true; } //always allow groups with *_read etc. regardless of categories if(__debug_permissions)wfDebug("checkCategoryPermissions on line ".__LINE__.": Trying action {$action} on * (all categories for user {$user->mName}\n"); if( $user->isAllowed("*_{$action}") ) { if(__debug_permissions)wfDebug("checkCategoryPermissions on line ".__LINE__.": Action {$action} ALLOWED on {$title->mPrefixedText} to user {$user->mName}: AlwaysAllow\n"); $result=null; return true; } if(__debug_permissions)wfDebug("checkCategoryPermissions on line ".__LINE__.": Trying action {$action} on {$title->mPrefixedText} for user {$user->mName}: No categories\n"); if(!($parentCategories)) { //handle special case of no categories //default action is based on wgGroupDefaultAllow if($wgGroupDefaultAllow) { if(__debug_permissions)wfDebug("checkCategoryPermissions on line ".__LINE__.": Action {$action} ALLOWED on {$title->mPrefixedText} to user {$user->mName}: No categories\n"); $result=null; return true; } else { if(__debug_permissions)wfDebug("checkCategoryPermissions on line ".__LINE__.": Action {$action} DENIED on {$title->mPrefixedText} to user {$user->mName}: No categories\n"); $result=false; return false; } } //default action=deny if(__debug_permissions)wfDebug("checkCategoryPermissions on line ".__LINE__.": Action {$action} DENIED on {$title->mPrefixedText} to user {$user->mName}: Default action\n"); $result=false; return false; } ?>
[edit] Notes
- ↑ According to Security issues with authorization extensions this has been addressed in MW 1.10, rev:21821: The search page no longer shows excerpts from pages that are not readable (but it still lists the titles)