API:Restricting API usage/zh

Category:MediaWiki action API/zh
Category:Pages using deprecated NoteTA template#%20

有幾種方法可以限制某些特定的使用者群組使用(某些部分的)API,或完全停用它。

其中的某些需要修改用户权限

Disabling general access

There is no dedicated user permission for accessing the API. To disable API access for a specific user group, you can disable read permissions for that group. For instance, to disallow anonymous requests,

$wgGroupPermissions['*']['read'] = false;

Note that some API modules may be available regardless. If access is successfully prevented, the API output will usually show the error code 'readapidenied'.

禁用模块

您可以在LocalSettings.php中添加一行來对所有用户禁用各個別的模块。 究竟要添加什么要取决于禁用的模块类型:

  • 对于action=模块,应添加$wgAPIModules['modulename'] = 'ApiDisabled';
  • 对于prop=模块,应添加$wgAPIPropModules['modulename'] = 'ApiQueryDisabled';
  • 对于list=模块,应添加$wgAPIListModules['modulename'] = 'ApiQueryDisabled';
  • 对于meta=模块,应添加$wgAPIMetaModules['modulename'] = 'ApiQueryDisabled';

使用action=edit來禁止任何一個用户:

$wgAPIModules['edit'] = 'ApiDisabled';

要限制API操作的访问权限,为ApiCheckCanExecute添加下面的钩子:

static function onApiCheckCanExecute( $module, $user, &$message ) {
    $moduleName = $module->getModuleName();
    if (
        $moduleName == 'action' &&
        !in_array( 'right', $user->getRights() )
    ) {
        $message = 'apierror-action-notallowed';
        return false;
    }
    return true;
}

'action', 'right''apierror-action-notallowed'替换为适当的值。

Category:MediaWiki action API/zh Category:Pages using deprecated NoteTA template