MediaWiki
REL1_21
|
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 $r['user'] = $user->getName(); 00042 $r['userid'] = $user->getId(); 00043 list( $r['added'], $r['removed'] ) = 00044 $form->doSaveUserGroups( 00045 $user, (array)$params['add'], 00046 (array)$params['remove'], $params['reason'] ); 00047 00048 $result = $this->getResult(); 00049 $result->setIndexedTagName( $r['added'], 'group' ); 00050 $result->setIndexedTagName( $r['removed'], 'group' ); 00051 $result->addValue( null, $this->getModuleName(), $r ); 00052 } 00053 00057 private function getUrUser() { 00058 if ( $this->mUser !== null ) { 00059 return $this->mUser; 00060 } 00061 00062 $params = $this->extractRequestParams(); 00063 00064 $form = new UserrightsPage; 00065 $status = $form->fetchUser( $params['user'] ); 00066 if ( !$status->isOK() ) { 00067 $errors = $status->getErrorsArray(); 00068 $this->dieUsageMsg( $errors[0] ); 00069 } else { 00070 $user = $status->value; 00071 } 00072 00073 $this->mUser = $user; 00074 return $user; 00075 } 00076 00077 public function mustBePosted() { 00078 return true; 00079 } 00080 00081 public function isWriteMode() { 00082 return true; 00083 } 00084 00085 public function getAllowedParams() { 00086 return array ( 00087 'user' => array( 00088 ApiBase::PARAM_TYPE => 'string', 00089 ApiBase::PARAM_REQUIRED => true 00090 ), 00091 'add' => array( 00092 ApiBase::PARAM_TYPE => User::getAllGroups(), 00093 ApiBase::PARAM_ISMULTI => true 00094 ), 00095 'remove' => array( 00096 ApiBase::PARAM_TYPE => User::getAllGroups(), 00097 ApiBase::PARAM_ISMULTI => true 00098 ), 00099 'token' => array( 00100 ApiBase::PARAM_TYPE => 'string', 00101 ApiBase::PARAM_REQUIRED => true 00102 ), 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 }