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