Jump to content

API:限制API使用

本頁使用了標題或全文手工轉換
From mediawiki.org
This page is a translated version of the page API:Restricting API usage and the translation is 70% complete.

有幾種方法可以限制某些特定的使用者群組使用(某些部分的)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();
    $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
    if (
        $moduleName == 'action' &&
        !$permissionManager->userHasRight( $user, 'right' )
    ) {
        $message = 'apierror-action-notallowed';
        return false;
    }
    return true;
}

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