37 private $mUser =
null;
54 parent::__construct( $mainModule, $moduleName );
55 $this->userGroupManager = $userGroupManager;
56 $this->watchedItemStore = $watchedItemStore;
60 $this->watchlistMaxDuration =
62 $this->watchlistManager = $watchlistManager;
63 $this->userOptionsLookup = $userOptionsLookup;
64 $this->userGroupAssignmentService = $userGroupAssignmentService;
65 $this->multiFormatUserIdentityLookup = $multiFormatUserIdentityLookup;
73 if ( !$this->
getAuthority()->isAllowed(
'userrights' ) ) {
74 $block = $pUser->getBlock( IDBAccessObject::READ_LATEST );
75 if ( $block && $block->isSitewide() ) {
83 $expiry = (array)$params[
'expiry'];
84 $add = (array)$params[
'add'];
87 } elseif ( count( $expiry ) !== count( $add ) ) {
88 if ( count( $expiry ) === 1 ) {
89 $expiry = array_fill( 0, count( $add ), $expiry[0] );
92 'apierror-toofewexpiries',
101 foreach ( $expiry as $index => $expiryValue ) {
102 $group = $add[$index];
103 $groupExpiries[$group] = UserGroupAssignmentService::expiryToTimestamp( $expiryValue );
105 if ( $groupExpiries[$group] ===
false ) {
110 if ( $groupExpiries[$group] && $groupExpiries[$group] <
wfTimestampNow() ) {
115 $user = $this->getUrUser( $params );
117 $tags = $params[
'tags'];
120 if ( $tags !==
null ) {
121 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->
getAuthority() );
122 if ( !$ableToTag->isOK() ) {
128 $r[
'user'] = $user->getName();
129 $r[
'userid'] = $user->getId( $user->getWikiId() );
130 [ $r[
'added'], $r[
'removed'] ] = $this->userGroupAssignmentService->saveChangesToUserGroups(
135 (array)$params[
'remove'],
143 $watchuser = $params[
'watchuser'];
144 if ( $watchuser && $user->getWikiId() === UserIdentity::LOCAL ) {
145 $this->
setWatch(
'watch', $userPage, $this->
getUser(),
null, $watchlistExpiry );
148 $watchlistExpiry =
null;
150 $r[
'watchuser'] = $watchuser;
151 if ( $watchlistExpiry !==
null ) {
153 $this->watchedItemStore,
169 private function getUrUser( array $params ) {
170 if ( $this->mUser !==
null ) {
176 $userDesignator = $params[
'user'] ??
'#' . $params[
'userid'];
177 $status = $this->multiFormatUserIdentityLookup->getUserIdentity( $userDesignator, $this->
getAuthority() );
178 if ( !$status->isOK() ) {
182 $user = $status->value;
183 $canHaveRights = $this->userGroupAssignmentService->targetCanHaveUserGroups( $user );
184 if ( !$canHaveRights ) {
187 $user->isRegistered() ? [
'userrights-no-group', $user->getName() ] :
'nosuchusershort'
191 $this->mUser = $user;
208 $allGroups = $this->userGroupManager->listAllGroups();
216 ParamValidator::PARAM_TYPE =>
'user',
217 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'id' ],
220 ParamValidator::PARAM_TYPE =>
'integer',
221 ParamValidator::PARAM_DEPRECATED =>
true,
224 ParamValidator::PARAM_TYPE => $allGroups,
225 ParamValidator::PARAM_ISMULTI => true
228 ParamValidator::PARAM_ISMULTI =>
true,
229 ParamValidator::PARAM_ALLOW_DUPLICATES =>
true,
230 ParamValidator::PARAM_DEFAULT =>
'infinite',
233 ParamValidator::PARAM_TYPE => $allGroups,
234 ParamValidator::PARAM_ISMULTI => true
237 ParamValidator::PARAM_DEFAULT =>
''
244 ParamValidator::PARAM_TYPE =>
'tags',
245 ParamValidator::PARAM_ISMULTI => true
247 'watchuser' =>
false,
253 if ( $this->watchlistExpiryEnabled ) {
255 'watchlistexpiry' => [
256 ParamValidator::PARAM_TYPE =>
'expiry',
257 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
258 ExpiryDef::PARAM_USE_MAX =>
true,
273 return $this->getUrUser( $params )->getName();
279 'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
280 =>
'apihelp-userrights-example-user',
281 'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
282 =>
'apihelp-userrights-example-userid',
283 'action=userrights&user=SometimeSysop&add=sysop&expiry=1%20month&token=123ABC'
284 =>
'apihelp-userrights-example-expiry',
290 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:User_group_membership';
295class_alias( ApiUserrights::class,
'ApiUserrights' );
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This is the main API class, used for both external and internal processing.
A class containing constants representing the names of configuration variables.
const WatchlistExpiry
Name constant for the WatchlistExpiry setting, for use with Config::get()
const WatchlistExpiryMaxDuration
Name constant for the WatchlistExpiryMaxDuration setting, for use with Config::get()
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...