Jump to content
MediaWiki

API:限制API使用

本頁使用了標題或全文手工轉換
From mediawiki.org
This page is a translated version of the page API:Restricting API usage and the translation is 78% complete.
本页是MediaWiki Action API帮助文档的一部分。
MediaWiki Action API
基础
认证
账号和用户
页面操作
搜索
开发员工具
教程
· ·

有幾種方法可以限制某些特定的使用者群組使用(某些部分的)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=edit來禁止任何一個用户:

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

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

static function onApiCheckCanExecute( $module, $user, &$message ) {
 $moduleName = $module->getModuleName();
 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
 if (
 $moduleName == 'action' &&
 !$permissionManager->userHasRight( $user, 'right' )
 ) {
 $message = 'apierror-action-notallowed';
 return false;
 }
 return true;
}

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

AltStyle によって変換されたページ (->オリジナル) /