MediaWiki  REL1_24
ApiUserrights.php
Go to the documentation of this file.
00001 <?php
00002 
00031 class ApiUserrights extends ApiBase {
00032 
00033     private $mUser = null;
00034 
00035     public function execute() {
00036         $params = $this->extractRequestParams();
00037 
00038         $user = $this->getUrUser( $params );
00039 
00040         $form = new UserrightsPage;
00041         $form->setContext( $this->getContext() );
00042         $r['user'] = $user->getName();
00043         $r['userid'] = $user->getId();
00044         list( $r['added'], $r['removed'] ) = $form->doSaveUserGroups(
00045             $user, (array)$params['add'],
00046             (array)$params['remove'], $params['reason']
00047         );
00048 
00049         $result = $this->getResult();
00050         $result->setIndexedTagName( $r['added'], 'group' );
00051         $result->setIndexedTagName( $r['removed'], 'group' );
00052         $result->addValue( null, $this->getModuleName(), $r );
00053     }
00054 
00059     private function getUrUser( array $params ) {
00060         if ( $this->mUser !== null ) {
00061             return $this->mUser;
00062         }
00063 
00064         $this->requireOnlyOneParameter( $params, 'user', 'userid' );
00065 
00066         $user = isset( $params['user'] ) ? $params['user'] : '#' . $params['userid'];
00067 
00068         $form = new UserrightsPage;
00069         $form->setContext( $this->getContext() );
00070         $status = $form->fetchUser( $user );
00071         if ( !$status->isOK() ) {
00072             $this->dieStatus( $status );
00073         }
00074 
00075         $this->mUser = $status->value;
00076 
00077         return $status->value;
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             ),
00093             'userid' => array(
00094                 ApiBase::PARAM_TYPE => 'integer',
00095             ),
00096             'add' => array(
00097                 ApiBase::PARAM_TYPE => User::getAllGroups(),
00098                 ApiBase::PARAM_ISMULTI => true
00099             ),
00100             'remove' => array(
00101                 ApiBase::PARAM_TYPE => User::getAllGroups(),
00102                 ApiBase::PARAM_ISMULTI => true
00103             ),
00104             'reason' => array(
00105                 ApiBase::PARAM_DFLT => ''
00106             )
00107         );
00108     }
00109 
00110     public function getParamDescription() {
00111         return array(
00112             'user' => 'User name',
00113             'userid' => 'User id',
00114             'add' => 'Add the user to these groups',
00115             'remove' => 'Remove the user from these groups',
00116             'token' => array(
00117                 /* Standard description automatically prepended */
00118                 'For compatibility, the token used in the web UI is also accepted.'
00119             ),
00120             'reason' => 'Reason for the change',
00121         );
00122     }
00123 
00124     public function getDescription() {
00125         return 'Add/remove a user to/from groups.';
00126     }
00127 
00128     public function needsToken() {
00129         return 'userrights';
00130     }
00131 
00132     protected function getWebUITokenSalt( array $params ) {
00133         return $this->getUrUser( $params )->getName();
00134     }
00135 
00136     public function getExamples() {
00137         return array(
00138             'api.php?action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC',
00139             'api.php?action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
00140         );
00141     }
00142 
00143     public function getHelpUrls() {
00144         return 'https://www.mediawiki.org/wiki/API:User_group_membership';
00145     }
00146 }