API:更改用户组成员
本页是MediaWiki Action API帮助文档的一部份。 |
MediaWiki版本: | ≥ 1.16 |
POST請求以從用戶組增加或移除用戶,從而授予或移除某些用戶權限。
API帮助文档
示例
要使用此API,您首先需要登入以驗證您自己的用戶組成員。 只有某些用戶組能透過此API更改用戶權限。 參見API:登录 以獲得有關登入的更多細節。
Once you are logged in, make a GET request to obtain a userrights token .
The query above applies to MediaWiki v1.24+; in older versions, the userrights
token would depend on the name of the user whose rights were being changed.
查詢如下所示:
For compatibility reasons, the API will also accept the token used in the web UI.
Whichever method you choose, once you have your token, you can use it to make your userrights
request, as seen below.
POST请求
從bureaucrat組中移除Bob,並將其添加到sysop組,從而授予他們管理員權限。
api.php? action=userrights& user=Bob& add=sysop& remove=bureaucrat& reason=Oops,%20put%20Bob%20in%20the%20wrong%20group& token=sampleUserrightsToken+/ [在Api沙盒中尝试]
回应
{
"userrights": {
"user": "Bob",
"userid": 2793024,
"removed": ["bureaucrat"],
"added": ["sysop"]
}
}
示例代码
Python
#!/usr/bin/python3
"""
userrights.py
MediaWiki API Demos
Demo of `Userrights` module: Add and remove user rights by
changing the user's group membership.
MIT license
"""
import requests
S = requests.Session()
URL = "https://test.wikipedia.org/w/api.php"
# Step 1: Retrieve a login token
PARAMS_1 = {
"action": "query",
"meta": "tokens",
"type": "login",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_1)
DATA = R.json()
LOGIN_TOKEN = DATA["query"]["tokens"]["logintoken"]
# Step 2: Send a post request to log in. See
# https://www.mediawiki.org/wiki/Manual:Bot_passwords
# for a special note on logging in using a simplified
# interface when accessing wikis via an application,
# rather than the GUI
PARAMS_2 = {
"action": "login",
"lgname": "username",
"lgpassword": "password",
"lgtoken": LOGIN_TOKEN,
"format": "json"
}
R = S.post(URL, data=PARAMS_2)
# Step 3: Obtain a Userrights token
PARAMS_3 = {
"action": "query",
"format": "json",
"meta": "tokens",
"type": "userrights"
}
R = S.get(url=URL, params=PARAMS_3)
DATA = R.json()
USERRIGHTS_TOKEN = DATA["query"]["tokens"]["userrightstoken"]
# Step 4: Request to add or remove a user from a group
PARAMS_4 = {
"action": "userrights",
"format": "json",
"user": "Bob",
"add": "sysop",
"remove": "bureaucrat",
"reason": "OOPS! added Bob to the wrong group",
"token": USERRIGHTS_TOKEN
}
R = S.post(URL, data=PARAMS_4)
DATA = R.json()
print(DATA)
PHP
<?php
- /*
- userrights.js
-
- MediaWiki API Demos
- 查詢如下所示:
- changing the user's group membership.
-
- POST請求以從用戶組增加或移除用戶,從而授予或移除某些用戶權限。
- */
$endPoint = "http://dev.wiki.local.wmftest.net:8080/w/api.php";
$login_Token = getLoginToken(); // Step 1
loginRequest( $login_Token ); // Step 2
$userrights_Token = getUserRightsToken(); // Step 3
change_userrights( $userrights_Token ); // Step 4
// Step 1: GET request to fetch login token
function getLoginToken() {
global $endPoint;
$params1 = [
"action" => "query",
"meta" => "tokens",
"type" => "login",
"format" => "json"
];
$url = $endPoint . "?" . http_build_query( $params1 );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
return $result["query"]["tokens"]["logintoken"];
}
// Step 2: POST request to log in. Use of main account for login is not
// supported. Obtain credentials via Special:BotPasswords
// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
function loginRequest( $logintoken ) {
global $endPoint;
$params2 = [
"action" => "clientlogin",
"username" => "username",
"password" => "password",
'loginreturnurl' => 'http://127.0.0.1:5000/',
"logintoken" => $logintoken,
"format" => "json"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endPoint );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params2 ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
}
// Step 3: GET request to fetch userrights token
function getUserRightsToken() {
global $endPoint;
$params3 = [
"action" => "query",
"meta" => "tokens",
"type" => "userrights",
"format" => "json"
];
$url = $endPoint . "?" . http_build_query( $params3 );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
return $result["query"]["tokens"]["userrightstoken"];
}
// Step 4: POST request to add or remove a user from a group
function change_userrights( $userrightstoken ) {
global $endPoint;
$params4 = [
"action" => "userrights",
"user" => "ABCDEF",
"add" => "bot",
"expiry" => "infinite",
"reason" => "API Testing",
"token" => $userrightstoken,
"format" => "json"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endPoint );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params4 ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
echo ( $output );
}
JavaScript
- /*
- userrights.js
-
- MediaWiki API Demos
- 查詢如下所示:
- changing the user's group membership.
-
- POST請求以從用戶組增加或移除用戶,從而授予或移除某些用戶權限。
- */
var request = require('request').defaults({jar: true}),
url = "http://dev.wiki.local.wmftest.net:8080/w/api.php";
// Step 1: GET request to fetch login token
function getLoginToken() {
var params_0 = {
action: "query",
meta: "tokens",
type: "login",
format: "json"
};
request.get({ url: url, qs: params_0 }, function (error, res, body) {
if (error) {
return;
}
var data = JSON.parse(body);
loginRequest(data.query.tokens.logintoken);
});
}
// Step 2: POST request to log in.
// Use of main account for login is not
// supported. Obtain credentials via Special:BotPasswords
// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
function loginRequest(login_token) {
var params_1 = {
action: "clientlogin",
username: "username",
password: "password",
loginreturnurl: "http://127.0.0.1:5000/",
logintoken: login_token,
format: "json"
};
request.post({ url: url, form: params_1 }, function (error, res, body) {
if (error) {
return;
}
getUserRightsToken();
});
}
// Step 3: GET request to fetch UserRights token
function getUserRightsToken() {
var params_2 = {
action: "query",
meta: "tokens",
type: "userrights",
format: "json"
};
request.get({ url: url, qs: params_2 }, function(error, res, body) {
if (error) {
return;
}
var data = JSON.parse(body);
userrights(data.query.tokens.userrightstoken);
});
}
// Step 4: POST request to add or remove a user from a group
function userrights(userrights_token) {
var params_3 = {
action: "userrights",
user: "ABCDEFG",
add: "bot",
expiry: "infinite",
reason: "API Testing",
token: userrights_token,
format: "json"
};
request.post({ url: url, form: params_3 }, function (error, res, body) {
if (error) {
return;
}
console.log(body);
});
}
// Start From Step 1
getLoginToken();
MediaWiki JS
- /*
- userrights.js
-
- MediaWiki API Demos
- 查詢如下所示:
- changing the user's group membership.
-
- POST請求以從用戶組增加或移除用戶,從而授予或移除某些用戶權限。
- */
var params = {
action: 'userrights',
user: 'ABCD',
add: 'sysop',
reason: 'Added ABCD to the sysop group',
format: 'json'
},
api = new mw.Api();
api.postWithToken( 'userrights', params ).done( function ( data ) {
console.log( data );
} );
可能的错误
代码 | 信息 |
---|---|
nouser | user参数必须被设置。 |
nosuchuser | 用户"用户名"不存在 嘗試更改匿名用戶的權限時可能會發生這種情況。
|
notoken | token参数必须被设置。 |
badtoken | 无效的CSRF令牌。 |
readonly | 此wiki目前为只读模式。 |
参数历史
- v1.29: 启用
expiry
- v1.23: 啟用
userid
附加提醒
- 預設只有行政員用戶組中的用戶才能授予或移除用戶權限。
- 有些wiki允許非行政員在有限的基礎上授予或移除權限,例如將這些權限限制在用戶自己的帳戶上。
- If you do not possess the ability to grant or remove rights to the target user, the API will not throw an error; instead, the
add
andremove
fields in the response will simply contain empty arrays.
參見
- Help:用户权限和用户组 - describes how user rights and group membership work in greater detail.
- Special:ListGroupRights - lists all the rights and privileges conferred to each user group on a particular wiki.
- Special:UserRights - a GUI way to add or remove user rights, available in wikis running v1.29+.
- API:用户 - gets information about a list of users, including their groups and rights.