MediaWiki  REL1_22
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();
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'] ) =
00045             $form->doSaveUserGroups(
00046                 $user, (array)$params['add'],
00047                 (array)$params['remove'], $params['reason'] );
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 
00058     private function getUrUser() {
00059         if ( $this->mUser !== null ) {
00060             return $this->mUser;
00061         }
00062 
00063         $params = $this->extractRequestParams();
00064 
00065         $form = new UserrightsPage;
00066         $form->setContext( $this->getContext() );
00067         $status = $form->fetchUser( $params['user'] );
00068         if ( !$status->isOK() ) {
00069             $this->dieStatus( $status );
00070         } else {
00071             $user = $status->value;
00072         }
00073 
00074         $this->mUser = $user;
00075         return $user;
00076     }
00077 
00078     public function mustBePosted() {
00079         return true;
00080     }
00081 
00082     public function isWriteMode() {
00083         return true;
00084     }
00085 
00086     public function getAllowedParams() {
00087         return array(
00088             'user' => array(
00089                 ApiBase::PARAM_TYPE => 'string',
00090                 ApiBase::PARAM_REQUIRED => true
00091             ),
00092             'add' => array(
00093                 ApiBase::PARAM_TYPE => User::getAllGroups(),
00094                 ApiBase::PARAM_ISMULTI => true
00095             ),
00096             'remove' => array(
00097                 ApiBase::PARAM_TYPE => User::getAllGroups(),
00098                 ApiBase::PARAM_ISMULTI => true
00099             ),
00100             'token' => array(
00101                 ApiBase::PARAM_TYPE => 'string',
00102                 ApiBase::PARAM_REQUIRED => 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             'add' => 'Add the user to these groups',
00114             'remove' => 'Remove the user from these groups',
00115             'token' => 'A userrights token previously retrieved through list=users',
00116             'reason' => 'Reason for the change',
00117         );
00118     }
00119 
00120     public function getDescription() {
00121         return 'Add/remove a user to/from groups';
00122     }
00123 
00124     public function needsToken() {
00125         return true;
00126     }
00127 
00128     public function getTokenSalt() {
00129         return $this->getUrUser()->getName();
00130     }
00131 
00132     public function getExamples() {
00133         return array(
00134             'api.php?action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
00135         );
00136     }
00137 
00138     public function getHelpUrls() {
00139         return 'https://www.mediawiki.org/wiki/API:User_group_membership';
00140     }
00141 }