MediaWiki  REL1_19
ApiUserrights.php
Go to the documentation of this file.
00001 <?php
00002 
00031 class ApiUserrights extends ApiBase {
00032 
00033         public function __construct( $main, $action ) {
00034                 parent::__construct( $main, $action );
00035         }
00036 
00037         private $mUser = null;
00038 
00039         public function execute() {
00040                 $params = $this->extractRequestParams();
00041 
00042                 $user = $this->getUrUser();
00043 
00044                 $form = new UserrightsPage;
00045                 $r['user'] = $user->getName();
00046                 list( $r['added'], $r['removed'] ) =
00047                         $form->doSaveUserGroups(
00048                                 $user, (array)$params['add'],
00049                                 (array)$params['remove'], $params['reason'] );
00050 
00051                 $result = $this->getResult();
00052                 $result->setIndexedTagName( $r['added'], 'group' );
00053                 $result->setIndexedTagName( $r['removed'], 'group' );
00054                 $result->addValue( null, $this->getModuleName(), $r );
00055         }
00056 
00060         private function getUrUser() {
00061                 if ( $this->mUser !== null ) {
00062                         return $this->mUser;
00063                 }
00064 
00065                 $params = $this->extractRequestParams();
00066 
00067                 $form = new UserrightsPage;
00068                 $status = $form->fetchUser( $params['user'] );
00069                 if ( !$status->isOK() ) {
00070                         $errors = $status->getErrorsArray();
00071                         $this->dieUsageMsg( $errors[0] );
00072                 } else {
00073                         $user = $status->value;
00074                 }
00075 
00076                 $this->mUser = $user;
00077                 return $user;
00078         }
00079 
00080         public function mustBePosted() {
00081                 return true;
00082         }
00083 
00084         public function isWriteMode() {
00085                 return true;
00086         }
00087 
00088         public function getAllowedParams() {
00089                 return array (
00090                         'user' => array(
00091                                 ApiBase::PARAM_TYPE => 'string',
00092                                 ApiBase::PARAM_REQUIRED => true
00093                         ),
00094                         'add' => array(
00095                                 ApiBase::PARAM_TYPE => User::getAllGroups(),
00096                                 ApiBase::PARAM_ISMULTI => true
00097                         ),
00098                         'remove' => array(
00099                                 ApiBase::PARAM_TYPE => User::getAllGroups(),
00100                                 ApiBase::PARAM_ISMULTI => true
00101                         ),
00102                         'token' => null,
00103                         'reason' => array(
00104                                 ApiBase::PARAM_DFLT => ''
00105                         )
00106                 );
00107         }
00108 
00109         public function getParamDescription() {
00110                 return array(
00111                         'user' => 'User name',
00112                         'add' => 'Add the user to these groups',
00113                         'remove' => 'Remove the user from these groups',
00114                         'token' => 'A userrights token previously retrieved through list=users',
00115                         'reason' => 'Reason for the change',
00116                 );
00117         }
00118 
00119         public function getDescription() {
00120                 return 'Add/remove a user to/from groups';
00121         }
00122 
00123         public function needsToken() {
00124                 return true;
00125         }
00126 
00127         public function getTokenSalt() {
00128                 return $this->getUrUser()->getName();
00129         }
00130 
00131         public function getExamples() {
00132                 return array(
00133                         'api.php?action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
00134                 );
00135         }
00136 
00137         public function getHelpUrls() {
00138                 return 'https://www.mediawiki.org/wiki/API:User_group_membership';
00139         }
00140 
00141         public function getVersion() {
00142                 return __CLASS__ . ': $Id$';
00143         }
00144 }