From 53959450522f019b7c4e874a4bb15fd387703806 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Fri, 4 Jan 2008 11:50:22 +0000 Subject: [PATCH] * Unbroke Special:Userrights for wikis without pretty URLs ** This probably shouldn't be hard-coded the way I did it * UserrightsPage::saveUserGroups() now takes $removegroup and $addgroup parameters by reference. Parameters are changed to reflect what was actually added and removed. * Re-adding ApiChangeRights module, which now handles permission denied errors more gracefully --- includes/AutoLoader.php | 1 + includes/SpecialUserrights.php | 10 ++- includes/api/ApiChangeRights.php | 145 +++++++++++++++++++++++++++++++ includes/api/ApiMain.php | 3 +- 4 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 includes/api/ApiChangeRights.php diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 0d1c751429..1b328d5692 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -349,6 +349,7 @@ function __autoload($className) { # apiedit branch 'ApiBlock' => 'includes/api/ApiBlock.php', + 'ApiChangeRights' => 'includes/api/ApiChangeRights.php', 'ApiDelete' => 'includes/api/ApiDelete.php', 'ApiMove' => 'includes/api/ApiMove.php', 'ApiProtect' => 'includes/api/ApiProtect.php', diff --git a/includes/SpecialUserrights.php b/includes/SpecialUserrights.php index 639ed3abc7..fb4af200b0 100644 --- a/includes/SpecialUserrights.php +++ b/includes/SpecialUserrights.php @@ -94,7 +94,7 @@ class UserrightsPage extends SpecialPage { * @param string $reason Reason for group change * @return null */ - function saveUserGroups( $username, $removegroup, $addgroup, $reason = '') { + function saveUserGroups( $username, &$removegroup, &$addgroup, $reason = '') { $user = $this->fetchUser( $username ); if( !$user ) { return; @@ -106,7 +106,11 @@ class UserrightsPage extends SpecialPage { array_intersect( (array)$removegroup, $changeable['remove'] ) ); $addgroup = array_unique( array_intersect( (array)$addgroup, $changeable['add'] ) ); - + + // If nothing is changed, no log entry should be created + if($removegroup == $addgroup) + return; + $oldGroups = $user->getGroups(); $newGroups = $oldGroups; // remove then add groups @@ -241,6 +245,7 @@ class UserrightsPage extends SpecialPage { function switchForm() { global $wgOut; $form = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $this->getTitle()->escapeLocalURL(), 'name' => 'uluser' ) ); + $form .= Xml::hidden( 'title', 'Special:Userrights' ); $form .= '
' . wfMsgHtml( 'userrights-lookup-user' ) . ''; $form .= '

' . Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget ) . '

'; $form .= '

' . Xml::submitButton( wfMsg( 'editusergroup' ) ) . '

'; @@ -288,6 +293,7 @@ class UserrightsPage extends SpecialPage { $wgOut->addHTML( Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->escapeLocalURL(), 'name' => 'editGroup' ) ) . Xml::hidden( 'user', $user->getName() ) . + Xml::hidden( 'title', 'Special:Userrights' ) . Xml::hidden( 'wpEditToken', $wgUser->editToken( $user->getName() ) ) . Xml::openElement( 'fieldset' ) . Xml::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) . diff --git a/includes/api/ApiChangeRights.php b/includes/api/ApiChangeRights.php new file mode 100644 index 0000000000..6d349f8f5b --- /dev/null +++ b/includes/api/ApiChangeRights.php @@ -0,0 +1,145 @@ +.@home.nl + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * http://www.gnu.org/copyleft/gpl.html + */ + +if (!defined('MEDIAWIKI')) { + // Eclipse helper - will be ignored in production + require_once ("ApiBase.php"); +} + +/** + * @addtogroup API + */ +class ApiChangeRights extends ApiBase { + + public function __construct($main, $action) { + parent :: __construct($main, $action); + } + + public function execute() { + global $wgUser, $wgRequest; + $this->getMain()->requestWriteMode(); + + if(wfReadOnly()) + $this->dieUsage('The wiki is in read-only mode', 'readonly'); + $params = $this->extractRequestParams(); + + $ur = new UserrightsPage($wgRequest); + $allowed = $ur->changeableGroups(); + $res = array(); + + if(is_null($params['user'])) + $this->dieUsage('The user parameter must be set', 'nouser'); + + $uName = User::getCanonicalName($params['user']); + $u = User::newFromName($uName); + if(!$u) + $this->dieUsage("Invalid username ``{$params['user']}''", 'invaliduser'); + if($u->getId() == 0) // Anon or non-existent + $this->dieUsage("User ``{$params['user']}'' doesn't exist", 'nosuchuser'); + + $curgroups = $u->getGroups(); + + if($params['listgroups']) + { + $res['user'] = $uName; + $res['allowedgroups'] = $allowed; + $res['ingroups'] = $curgroups; + $this->getResult()->setIndexedTagName($res['ingroups'], 'group'); + $this->getResult()->setIndexedTagName($res['allowedgroups']['add'], 'group'); + $this->getResult()->setIndexedTagName($res['allowedgroups']['remove'], 'group'); + } +; + if($params['gettoken']) + { + $res['changerightstoken'] = $wgUser->editToken($uName); + $this->getResult()->addValue(null, $this->getModuleName(), $res); + return; + } + + if(empty($params['addto']) && empty($params['rmfrom'])) + $this->dieUsage('At least one of the addto and rmfrom parameters must be set', 'noaddrm'); + if(is_null($params['token'])) + $this->dieUsage('The token parameter must be set', 'notoken'); + if(!$wgUser->matchEditToken($params['token'], $uName)) + $this->dieUsage('Invalid token', 'badtoken'); + + $dbw = wfGetDb(DB_MASTER); + $dbw->begin(); + $ur->saveUserGroups($uName, $params['rmfrom'], $params['addto'], $params['reason']); + $dbw->commit(); + $res['user'] = $uName; + $res['addedto'] = $params['addto']; + $res['removedfrom'] = $params['rmfrom']; + $res['reason'] = $params['reason']; + + $this->getResult()->setIndexedTagName($res['addedto'], 'group'); + $this->getResult()->setIndexedTagName($res['removedfrom'], 'group'); + $this->getResult()->addValue(null, $this->getModuleName(), $res); + } + + protected function getAllowedParams() { + return array ( + 'user' => null, + 'token' => null, + 'gettoken' => false, + 'listgroups' => false, + 'addto' => array( + ApiBase :: PARAM_ISMULTI => true, + ), + 'rmfrom' => array( + ApiBase :: PARAM_ISMULTI => true, + ), + 'reason' => '' + ); + } + + protected function getParamDescription() { + return array ( + 'user' => 'The user you want to add to or remove from groups.', + 'token' => 'A changerights token previously obtained through the gettoken parameter.', + 'gettoken' => 'Output a token. Note that the user parameter still has to be set.', + 'listgroups' => 'List the groups the user is in, and the ones you can add them to and remove them from.', + 'addto' => 'Pipe-separated list of groups to add this user to', + 'rmfrom' => 'Pipe-separated list of groups to remove this user from', + 'reason' => 'Reason for change (optional)' + ); + } + + protected function getDescription() { + return array( + 'Add or remove a user from certain groups.' + ); + } + + protected function getExamples() { + return array ( + 'api.php?action=changerights&user=Bob&gettoken&listgroups', + 'api.php?action=changerights&user=Bob&token=123ABC&addto=sysop&reason=Promoting%20per%20RFA' + ); + } + + public function getVersion() { + return __CLASS__ . ': $Id: ApiChangeRights.php 28216 2007-12-06 18:33:18Z vasilievvv $'; + } +} diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 5509c31e6d..9c9ce5c68d 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -69,7 +69,8 @@ class ApiMain extends ApiBase { 'protect' => 'ApiProtect', 'block' => 'ApiBlock', 'unblock' => 'ApiUnblock', - 'move' => 'ApiMove' + 'move' => 'ApiMove', + 'changerights' => 'ApiChangeRights' ); /** -- 2.20.1