1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
<?php
/**
* API userrights module
*
* Copyright © 2009 Roan Kattouw <roan.kattouw@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Api;
use ChangeTags;
use MediaWiki\MainConfigNames;
use MediaWiki\ParamValidator\TypeDef\UserDef;
use MediaWiki\Specials\SpecialUserRights;
use MediaWiki\Title\Title;
use MediaWiki\User\Options\UserOptionsLookup;
use MediaWiki\User\UserGroupManager;
use MediaWiki\User\UserIdentity;
use MediaWiki\Watchlist\WatchedItemStoreInterface;
use MediaWiki\Watchlist\WatchlistManager;
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef\ExpiryDef;
use Wikimedia\Rdbms\IDBAccessObject;
/**
* @ingroup API
*/
class ApiUserrights extends ApiBase {
use ApiWatchlistTrait;
/** @var UserIdentity|null */
private $mUser = null;
private UserGroupManager $userGroupManager;
private WatchedItemStoreInterface $watchedItemStore;
public function __construct(
ApiMain $mainModule,
string $moduleName,
UserGroupManager $userGroupManager,
WatchedItemStoreInterface $watchedItemStore,
WatchlistManager $watchlistManager,
UserOptionsLookup $userOptionsLookup
) {
parent::__construct( $mainModule, $moduleName );
$this->userGroupManager = $userGroupManager;
$this->watchedItemStore = $watchedItemStore;
// Variables needed in ApiWatchlistTrait trait
$this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
$this->watchlistMaxDuration =
$this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
$this->watchlistManager = $watchlistManager;
$this->userOptionsLookup = $userOptionsLookup;
}
public function execute() {
$pUser = $this->getUser();
// Deny if the user is blocked and doesn't have the full 'userrights' permission.
// This matches what Special:UserRights does for the web UI.
if ( !$this->getAuthority()->isAllowed( 'userrights' ) ) {
$block = $pUser->getBlock( IDBAccessObject::READ_LATEST );
if ( $block && $block->isSitewide() ) {
$this->dieBlocked( $block );
}
}
$params = $this->extractRequestParams();
// Figure out expiry times from the input
$expiry = (array)$params['expiry'];
$add = (array)$params['add'];
if ( !$add ) {
$expiry = [];
} elseif ( count( $expiry ) !== count( $add ) ) {
if ( count( $expiry ) === 1 ) {
$expiry = array_fill( 0, count( $add ), $expiry[0] );
} else {
$this->dieWithError( [
'apierror-toofewexpiries',
count( $expiry ),
count( $add )
] );
}
}
// Validate the expiries
$groupExpiries = [];
foreach ( $expiry as $index => $expiryValue ) {
$group = $add[$index];
$groupExpiries[$group] = SpecialUserRights::expiryToTimestamp( $expiryValue );
if ( $groupExpiries[$group] === false ) {
$this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiryValue ) ] );
}
// not allowed to have things expiring in the past
if ( $groupExpiries[$group] && $groupExpiries[$group] < wfTimestampNow() ) {
$this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiryValue ) ] );
}
}
$user = $this->getUrUser( $params );
$tags = $params['tags'];
// Check if user can add tags
if ( $tags !== null ) {
$ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->getAuthority() );
if ( !$ableToTag->isOK() ) {
$this->dieStatus( $ableToTag );
}
}
$form = new SpecialUserRights();
$form->setContext( $this->getContext() );
$r = [];
$r['user'] = $user->getName();
$r['userid'] = $user->getId( $user->getWikiId() );
[ $r['added'], $r['removed'] ] = $form->doSaveUserGroups(
$user,
$add,
// Don't pass null to doSaveUserGroups() for array params, cast to empty array
(array)$params['remove'],
$params['reason'],
(array)$tags,
$groupExpiries
);
$watchlistExpiry = $this->getExpiryFromParams( $params );
$watchuser = $params['watchuser'];
$userPage = Title::makeTitle( NS_USER, $user->getName() );
if ( $watchuser && $user->getWikiId() === UserIdentity::LOCAL ) {
$this->setWatch( 'watch', $userPage, $this->getUser(), null, $watchlistExpiry );
} else {
$watchuser = false;
$watchlistExpiry = null;
}
$r['watchuser'] = $watchuser;
if ( $watchlistExpiry !== null ) {
$r['watchlistexpiry'] = $this->getWatchlistExpiry(
$this->watchedItemStore,
$userPage,
$this->getUser()
);
}
$result = $this->getResult();
ApiResult::setIndexedTagName( $r['added'], 'group' );
ApiResult::setIndexedTagName( $r['removed'], 'group' );
$result->addValue( null, $this->getModuleName(), $r );
}
/**
* @param array $params
* @return UserIdentity
*/
private function getUrUser( array $params ) {
if ( $this->mUser !== null ) {
return $this->mUser;
}
$this->requireOnlyOneParameter( $params, 'user', 'userid' );
$user = $params['user'] ?? '#' . $params['userid'];
$form = new SpecialUserRights();
$form->setContext( $this->getContext() );
$status = $form->fetchUser( $user );
if ( !$status->isOK() ) {
$this->dieStatus( $status );
}
$this->mUser = $status->value;
return $status->value;
}
public function mustBePosted() {
return true;
}
public function isWriteMode() {
return true;
}
public function getAllowedParams( $flags = 0 ) {
$allGroups = $this->userGroupManager->listAllGroups();
if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) {
sort( $allGroups );
}
$params = [
'user' => [
ParamValidator::PARAM_TYPE => 'user',
UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ],
],
'userid' => [
ParamValidator::PARAM_TYPE => 'integer',
ParamValidator::PARAM_DEPRECATED => true,
],
'add' => [
ParamValidator::PARAM_TYPE => $allGroups,
ParamValidator::PARAM_ISMULTI => true
],
'expiry' => [
ParamValidator::PARAM_ISMULTI => true,
ParamValidator::PARAM_ALLOW_DUPLICATES => true,
ParamValidator::PARAM_DEFAULT => 'infinite',
],
'remove' => [
ParamValidator::PARAM_TYPE => $allGroups,
ParamValidator::PARAM_ISMULTI => true
],
'reason' => [
ParamValidator::PARAM_DEFAULT => ''
],
'token' => [
// Standard definition automatically inserted
ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
],
'tags' => [
ParamValidator::PARAM_TYPE => 'tags',
ParamValidator::PARAM_ISMULTI => true
],
'watchuser' => false,
];
// Params appear in the docs in the order they are defined,
// which is why this is here and not at the bottom.
// @todo Find better way to support insertion at arbitrary position
if ( $this->watchlistExpiryEnabled ) {
$params += [
'watchlistexpiry' => [
ParamValidator::PARAM_TYPE => 'expiry',
ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
ExpiryDef::PARAM_USE_MAX => true,
]
];
}
return $params;
}
public function needsToken() {
return 'userrights';
}
protected function getWebUITokenSalt( array $params ) {
return $this->getUrUser( $params )->getName();
}
protected function getExamplesMessages() {
return [
'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
=> 'apihelp-userrights-example-user',
'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
=> 'apihelp-userrights-example-userid',
'action=userrights&user=SometimeSysop&add=sysop&expiry=1%20month&token=123ABC'
=> 'apihelp-userrights-example-expiry',
];
}
public function getHelpUrls() {
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:User_group_membership';
}
}
/** @deprecated class alias since 1.43 */
class_alias( ApiUserrights::class, 'ApiUserrights' );
|