execute();
}
/**
* A class to manage user levels rights.
* @addtogroup SpecialPage
*/
class UserrightsForm extends HTMLForm {
var $mPosted, $mRequest, $mSaveprefs;
/** Escaped local url name*/
var $action;
/** Constructor*/
public function __construct( &$request ) {
$this->mPosted = $request->wasPosted();
$this->mRequest =& $request;
$this->mName = 'userrights';
$titleObj = SpecialPage::getTitleFor( 'Userrights' );
$this->action = $titleObj->escapeLocalURL();
}
/**
* Manage forms to be shown according to posted data.
* Depending on the submit button used, call a form or a save function.
*/
function execute() {
// show the general form
$this->switchForm();
if( $this->mPosted ) {
// show some more forms
if( $this->mRequest->getCheck( 'ssearchuser' ) ) {
$this->editUserGroupsForm( $this->mRequest->getVal( 'user-editname' ) );
}
// save settings
if( $this->mRequest->getCheck( 'saveusergroups' ) ) {
global $wgUser;
$username = $this->mRequest->getVal( 'user-editname' );
$reason = $this->mRequest->getVal( 'user-reason' );
if( $wgUser->matchEditToken( $this->mRequest->getVal( 'wpEditToken' ), $username ) ) {
$this->saveUserGroups( $username,
$this->mRequest->getArray( 'member' ),
$this->mRequest->getArray( 'available' ),
$reason );
}
}
}
}
/**
* Save user groups changes in the database.
* Data comes from the editUserGroupsForm() form function
*
* @param string $username Username to apply changes to.
* @param array $removegroup id of groups to be removed.
* @param array $addgroup id of groups to be added.
* @param string $reason Reason for group change
*
*/
function saveUserGroups( $username, $removegroup, $addgroup, $reason = '' ) {
global $wgOut;
$u = User::newFromName($username);
if(is_null($u)) {
$wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
return;
}
if($u->getID() == 0) {
$wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
return;
}
$oldGroups = $u->getGroups();
$newGroups = $oldGroups;
// remove then add groups
if(isset($removegroup)) {
$newGroups = array_diff($newGroups, $removegroup);
foreach( $removegroup as $group ) {
if ( $this->canRemove( $group ) ) {
$u->removeGroup( $group );
}
}
}
if(isset($addgroup)) {
$newGroups = array_merge($newGroups, $addgroup);
foreach( $addgroup as $group ) {
if ( $this->canAdd( $group ) ) {
$u->addGroup( $group );
}
}
}
$newGroups = array_unique( $newGroups );
wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
wfRunHooks( 'UserRights', array( &$u, $addgroup, $removegroup ) );
$log = new LogPage( 'rights' );
$log->addEntry( 'rights', Title::makeTitle( NS_USER, $u->getName() ), $reason, array( $this->makeGroupNameList( $oldGroups ),
$this->makeGroupNameList( $newGroups ) ) );
}
function makeGroupNameList( $ids ) {
return implode( ', ', $ids );
}
/**
* Output a form to allow searching for a user
*/
function switchForm() {
global $wgOut, $wgRequest;
$username = $wgRequest->getText( 'user-editname' );
$form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->action, 'name' => 'uluser' ) );
$form .= '
';
$form .= '';
$wgOut->addHTML( $form );
}
/**
* Edit user groups membership
* @param string $username Name of the user.
*/
function editUserGroupsForm($username) {
global $wgOut;
$user = User::newFromName($username);
if( is_null( $user ) ) {
$wgOut->addWikiText( wfMsg( 'nouserspecified' ) );
return;
} elseif( $user->getID() == 0 ) {
$wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
return;
}
$this->showEditUserGroupsForm( $username, $user->getGroups() );
$this->showLogFragment( $user, $wgOut );
}
/**
* Go through used and available groups and return the ones that this
* form will be able to manipulate based on the current user's system
* permissions.
*
* @param $groups Array: list of groups the given user is in
* @return Array: Tuple of addable, then removable groups
*/
protected function splitGroups( $groups ) {
list($addable, $removable) = array_values( $this->changeableGroups() );
$removable = array_intersect($removable, $groups ); // Can't remove groups the user doesn't have
$addable = array_diff( $addable, $groups ); // Can't add groups the user does have
return array( $addable, $removable );
}
/**
* Show the form to edit group memberships.
*
* @todo make all CSS-y and semantic
* @param $username String: Name of user you're editing
* @param $groups Array: Array of groups the user is in
*/
protected function showEditUserGroupsForm( $username, $groups ) {
global $wgOut, $wgUser;
list( $addable, $removable ) = $this->splitGroups( $groups );
$wgOut->addHTML(
Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->action, 'name' => 'editGroup' ) ) .
Xml::hidden( 'user-editname', $username ) .
Xml::hidden( 'wpEditToken', $wgUser->editToken( $username ) ) .
Xml::openElement( 'fieldset' ) .
Xml::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
$wgOut->parse( wfMsg( 'editinguser', $username ) ) .
$this->explainRights() .
"