Mediawiki用户权限

本文介绍了如何在Mediawiki中添加用户权限控制,通过自定义名字空间实现。内容包括禁用匿名用户权限,添加自定义名字空间,并详细阐述了创建NamespacePermissions.php扩展文件的过程,以及设置用户权限的步骤。适用于mediawiki-1.16.1版本,其他版本可能需要进一步测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、目        的:为Mediawiki添加用户权限控制

二、实现途径:通过自定义名字空间(custom namespaces)实现

三、说        明:1. 本文仅对mediawiki-1.16.1进行测试过,其他版本是否适合,有待进一步测试

                             2. 如果要实现该权限控制,必须要求所有链接均要加上其对应所在的名字空间名称

                                 eg. [[Yfzx:研发中心|研发中心首页]]

 

----------------------------------------------------------------------------

1. 首先禁用匿名用户权限:

    修改文件includes/DefaultSettings.php

   $wgGroupPermissions['*']['createaccount']    = false;
   $wgGroupPermissions['*']['read']                     = false;
   $wgGroupPermissions['*']['edit']                       = false;
   $wgGroupPermissions['*']['createpage']         = false;
   $wgGroupPermissions['*']['createtalk']            = false;
   $wgGroupPermissions['*']['writeapi']               = false;

2. 添加自定义名字空间:

    修改文件LocalSettings.php,在其最后添加如下代码:

    $wgExtraNamespaces = array(700 => "Yfzx", 701 => "Yfzx_Talk", 702 => "Gkzx", 703 => "Gkzx_Talk");
    require_once('extensions/NamespacePermissions.php');

    说明:

    700 => "Yfzx", 701 => "Yfzx_Talk"

              700,701表示名字空间ID,mediawiki要求自定义名字空间ID>=100,其中偶数表示名字空间,紧随其后的奇数表示该名字空间的讨论页

              Yfzx Yfzx_talk 表示名字空间名称和其对应讨论页

 3. 创建步骤2中引用的PHP文件NamespacePermissions.php

     在extensions目录下创建文件NamespacePermissions.php,内容如下:

    

 <?php
 /* NamespacePermissions - MediaWiki extension
  *
  * provides separate permissions for each action (read,edit,create,move)
  * on articles in custom namespaces for fine access management
  *
  * Author: Petr Andreev
  *
  * Sample usage:3
  *
  * $wgExtraNamespaces = array(100 => "Foo", 101 => "Foo_Talk");
  * // optional (example): allow registered users to view and edit articles in Foo
  * $wgGroupPermissions[ 'user' ][ 'ns100_read' ] = true;
  * $wgGroupPermissions[ 'user' ][ 'ns100_edit' ] = true;
  * // end of optional
  * require('extensions/NamespacePermissions.php');
  *
  * Permissions provided:
  *   # ns{$num}_read
  *   # ns{$num}_edit
  *   # ns{$num}_create
  *   # ns{$num}_move
  * where {$num} - namespace number (e.g. ns100_read, ns101_create)
  *
  * Groups provided:
  *   # ns{$title}RW - full access to the namespace {$title}
  *   # ns{$title}RO - read-only access to the namespace {$title}
  *   e.g. nsFoo_talkRW, nsFooRO
  */
 
 // permissions for autocreated groups should be set now,
 // before the User object for current user is instantiated
 namespacePermissionsCreateGroups();
 // other stuff should better be done via standard mechanism of running extensions
 $wgExtensionFunctions[] = "wfNamespacePermissions";
 
 // create groups for each custom namespace
 function namespacePermissionsCreateGroups() {
     global $wgExtraNamespaces, $wgGroupPermissions;
     foreach ( $wgExtraNamespaces as $num => $title ) {
         $wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_edit" ] = true;
         $wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_read" ] = true;
         $wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_create" ] = true;
         $wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_move" ] = true;
         $wgGroupPermissions[ "ns{$title}RO" ][ "ns{$num}_read" ] = true;
     }
 }
 
 function wfNamespacePermissions() {
     global $wgHooks;
 
     // use the userCan hook to check permissions
     $wgHooks[ 'userCan' ][] = 'namespacePermissionsCheckNamespace';
 }
 
 function namespacePermissionsCheckNamespace( $title, $user, $action, $result ) {
     if ( ( $ns = $title->getNamespace() ) >= 100 ) {
         if ( ! $user->isAllowed("ns{$ns}_{$action}") ) {
             $result = false;
             return false;
         }
     }
     return true;
 }
 
 /**
 * Add extension information to Special:Version
 */
 $wgExtensionCredits['other'][] = array(
        'name' => 'NamespacePermissions',
        'version' => '',
        'author' => 'Petr Andreev',
        'description' => 'flexible access management for custom namespaces',
        'url' => 'http://www.mediawiki.org/wiki/Extension:NamespacePermissions'
        );
?>

4. 如何设置权限:

    1) 首先以管理员身份登录wiki

    2) 首页中点击‘特殊页面’或‘所有特殊页面’

    3) 找到并点击链接‘用户权限管理

    4) 输入用户名并点击按钮

    5) 根据实际情况,为用户选择合适的名字空间权限,每个名字空间和其讨论页均对应权限RW(可读写)和RO(只读)

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值