MediaWiki  REL1_23
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'] ) = $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 
00058     private function getUrUser() {
00059         if ( $this->mUser !== null ) {
00060             return $this->mUser;
00061         }
00062 
00063         $params = $this->extractRequestParams();
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             'token' => array(
00105                 ApiBase::PARAM_TYPE => 'string',
00106                 ApiBase::PARAM_REQUIRED => true
00107             ),
00108             'reason' => array(
00109                 ApiBase::PARAM_DFLT => ''
00110             )
00111         );
00112     }
00113 
00114     public function getParamDescription() {
00115         return array(
00116             'user' => 'User name',
00117             'userid' => 'User id',
00118             'add' => 'Add the user to these groups',
00119             'remove' => 'Remove the user from these groups',
00120             'token' => 'A userrights token previously retrieved through list=users',
00121             'reason' => 'Reason for the change',
00122         );
00123     }
00124 
00125     public function getDescription() {
00126         return 'Add/remove a user to/from groups.';
00127     }
00128 
00129     public function needsToken() {
00130         return true;
00131     }
00132 
00133     public function getTokenSalt() {
00134         return $this->getUrUser()->getName();
00135     }
00136 
00137     public function getExamples() {
00138         return array(
00139             'api.php?action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC',
00140             'api.php?action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
00141         );
00142     }
00143 
00144     public function getHelpUrls() {
00145         return 'https://www.mediawiki.org/wiki/API:User_group_membership';
00146     }
00147 }