PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/specials/SpecialUserrights.php

https://github.com/tav/confluence
PHP | 636 lines | 417 code | 81 blank | 138 comment | 73 complexity | 4d58a9f859a9f2a132c04b560c9f8f38 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Special page to allow managing user group membership
  4. *
  5. * @file
  6. * @ingroup SpecialPage
  7. */
  8. /**
  9. * A class to manage user levels rights.
  10. * @ingroup SpecialPage
  11. */
  12. class UserrightsPage extends SpecialPage {
  13. # The target of the local right-adjuster's interest. Can be gotten from
  14. # either a GET parameter or a subpage-style parameter, so have a member
  15. # variable for it.
  16. protected $mTarget;
  17. protected $isself = false;
  18. public function __construct() {
  19. parent::__construct( 'Userrights' );
  20. }
  21. public function isRestricted() {
  22. return true;
  23. }
  24. public function userCanExecute( $user ) {
  25. return $this->userCanChangeRights( $user, false );
  26. }
  27. public function userCanChangeRights( $user, $checkIfSelf = true ) {
  28. $available = $this->changeableGroups();
  29. return !empty( $available['add'] )
  30. or !empty( $available['remove'] )
  31. or ( ( $this->isself || !$checkIfSelf ) and
  32. (!empty( $available['add-self'] )
  33. or !empty( $available['remove-self'] )));
  34. }
  35. /**
  36. * Manage forms to be shown according to posted data.
  37. * Depending on the submit button used, call a form or a save function.
  38. *
  39. * @param $par Mixed: string if any subpage provided, else null
  40. */
  41. function execute( $par ) {
  42. // If the visitor doesn't have permissions to assign or remove
  43. // any groups, it's a bit silly to give them the user search prompt.
  44. global $wgUser, $wgRequest;
  45. if( $par ) {
  46. $this->mTarget = $par;
  47. } else {
  48. $this->mTarget = $wgRequest->getVal( 'user' );
  49. }
  50. if (!$this->mTarget) {
  51. /*
  52. * If the user specified no target, and they can only
  53. * edit their own groups, automatically set them as the
  54. * target.
  55. */
  56. $available = $this->changeableGroups();
  57. if (empty($available['add']) && empty($available['remove']))
  58. $this->mTarget = $wgUser->getName();
  59. }
  60. if ($this->mTarget == $wgUser->getName())
  61. $this->isself = true;
  62. if( !$this->userCanChangeRights( $wgUser, true ) ) {
  63. // fixme... there may be intermediate groups we can mention.
  64. global $wgOut;
  65. $wgOut->showPermissionsErrorPage( array(
  66. $wgUser->isAnon()
  67. ? 'userrights-nologin'
  68. : 'userrights-notallowed' ) );
  69. return;
  70. }
  71. if ( wfReadOnly() ) {
  72. global $wgOut;
  73. $wgOut->readOnlyPage();
  74. return;
  75. }
  76. $this->outputHeader();
  77. $this->setHeaders();
  78. // show the general form
  79. $this->switchForm();
  80. if( $wgRequest->wasPosted() ) {
  81. // save settings
  82. if( $wgRequest->getCheck( 'saveusergroups' ) ) {
  83. $reason = $wgRequest->getVal( 'user-reason' );
  84. $tok = $wgRequest->getVal( 'wpEditToken' );
  85. if( $wgUser->matchEditToken( $tok, $this->mTarget ) ) {
  86. $this->saveUserGroups(
  87. $this->mTarget,
  88. $reason
  89. );
  90. global $wgOut;
  91. $url = $this->getSuccessURL();
  92. $wgOut->redirect( $url );
  93. return;
  94. }
  95. }
  96. }
  97. // show some more forms
  98. if( $this->mTarget ) {
  99. $this->editUserGroupsForm( $this->mTarget );
  100. }
  101. }
  102. function getSuccessURL() {
  103. return $this->getTitle( $this->mTarget )->getFullURL();
  104. }
  105. /**
  106. * Save user groups changes in the database.
  107. * Data comes from the editUserGroupsForm() form function
  108. *
  109. * @param $username String: username to apply changes to.
  110. * @param $reason String: reason for group change
  111. * @return null
  112. */
  113. function saveUserGroups( $username, $reason = '') {
  114. global $wgRequest, $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
  115. $user = $this->fetchUser( $username );
  116. if( !$user ) {
  117. return;
  118. }
  119. $allgroups = $this->getAllGroups();
  120. $addgroup = array();
  121. $removegroup = array();
  122. // This could possibly create a highly unlikely race condition if permissions are changed between
  123. // when the form is loaded and when the form is saved. Ignoring it for the moment.
  124. foreach ($allgroups as $group) {
  125. // We'll tell it to remove all unchecked groups, and add all checked groups.
  126. // Later on, this gets filtered for what can actually be removed
  127. if ($wgRequest->getCheck( "wpGroup-$group" )) {
  128. $addgroup[] = $group;
  129. } else {
  130. $removegroup[] = $group;
  131. }
  132. }
  133. // Validate input set...
  134. $changeable = $this->changeableGroups();
  135. $addable = array_merge( $changeable['add'], $this->isself ? $changeable['add-self'] : array() );
  136. $removable = array_merge( $changeable['remove'], $this->isself ? $changeable['remove-self'] : array() );
  137. $removegroup = array_unique(
  138. array_intersect( (array)$removegroup, $removable ) );
  139. $addgroup = array_unique(
  140. array_intersect( (array)$addgroup, $addable ) );
  141. $oldGroups = $user->getGroups();
  142. $newGroups = $oldGroups;
  143. // remove then add groups
  144. if( $removegroup ) {
  145. $newGroups = array_diff($newGroups, $removegroup);
  146. foreach( $removegroup as $group ) {
  147. $user->removeGroup( $group );
  148. }
  149. }
  150. if( $addgroup ) {
  151. $newGroups = array_merge($newGroups, $addgroup);
  152. foreach( $addgroup as $group ) {
  153. $user->addGroup( $group );
  154. }
  155. }
  156. $newGroups = array_unique( $newGroups );
  157. // Ensure that caches are cleared
  158. $user->invalidateCache();
  159. wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
  160. wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
  161. if( $user instanceof User ) {
  162. // hmmm
  163. wfRunHooks( 'UserRights', array( &$user, $addgroup, $removegroup ) );
  164. }
  165. if( $newGroups != $oldGroups ) {
  166. $this->addLogEntry( $user, $oldGroups, $newGroups );
  167. }
  168. }
  169. /**
  170. * Add a rights log entry for an action.
  171. */
  172. function addLogEntry( $user, $oldGroups, $newGroups ) {
  173. global $wgRequest;
  174. $log = new LogPage( 'rights' );
  175. $log->addEntry( 'rights',
  176. $user->getUserPage(),
  177. $wgRequest->getText( 'user-reason' ),
  178. array(
  179. $this->makeGroupNameListForLog( $oldGroups ),
  180. $this->makeGroupNameListForLog( $newGroups )
  181. )
  182. );
  183. }
  184. /**
  185. * Edit user groups membership
  186. * @param $username String: name of the user.
  187. */
  188. function editUserGroupsForm( $username ) {
  189. global $wgOut;
  190. $user = $this->fetchUser( $username );
  191. if( !$user ) {
  192. return;
  193. }
  194. $groups = $user->getGroups();
  195. $this->showEditUserGroupsForm( $user, $groups );
  196. // This isn't really ideal logging behavior, but let's not hide the
  197. // interwiki logs if we're using them as is.
  198. $this->showLogFragment( $user, $wgOut );
  199. }
  200. /**
  201. * Normalize the input username, which may be local or remote, and
  202. * return a user (or proxy) object for manipulating it.
  203. *
  204. * Side effects: error output for invalid access
  205. * @return mixed User, UserRightsProxy, or null
  206. */
  207. function fetchUser( $username ) {
  208. global $wgOut, $wgUser, $wgUserrightsInterwikiDelimiter;
  209. $parts = explode( $wgUserrightsInterwikiDelimiter, $username );
  210. if( count( $parts ) < 2 ) {
  211. $name = trim( $username );
  212. $database = '';
  213. } else {
  214. list( $name, $database ) = array_map( 'trim', $parts );
  215. if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
  216. $wgOut->addWikiMsg( 'userrights-no-interwiki' );
  217. return null;
  218. }
  219. if( !UserRightsProxy::validDatabase( $database ) ) {
  220. $wgOut->addWikiMsg( 'userrights-nodatabase', $database );
  221. return null;
  222. }
  223. }
  224. if( $name == '' ) {
  225. $wgOut->addWikiMsg( 'nouserspecified' );
  226. return false;
  227. }
  228. if( $name{0} == '#' ) {
  229. // Numeric ID can be specified...
  230. // We'll do a lookup for the name internally.
  231. $id = intval( substr( $name, 1 ) );
  232. if( $database == '' ) {
  233. $name = User::whoIs( $id );
  234. } else {
  235. $name = UserRightsProxy::whoIs( $database, $id );
  236. }
  237. if( !$name ) {
  238. $wgOut->addWikiMsg( 'noname' );
  239. return null;
  240. }
  241. }
  242. if( $database == '' ) {
  243. $user = User::newFromName( $name );
  244. } else {
  245. $user = UserRightsProxy::newFromName( $database, $name );
  246. }
  247. if( !$user || $user->isAnon() ) {
  248. $wgOut->addWikiMsg( 'nosuchusershort', $username );
  249. return null;
  250. }
  251. return $user;
  252. }
  253. function makeGroupNameList( $ids ) {
  254. if( empty( $ids ) ) {
  255. return wfMsgForContent( 'rightsnone' );
  256. } else {
  257. return implode( ', ', $ids );
  258. }
  259. }
  260. function makeGroupNameListForLog( $ids ) {
  261. if( empty( $ids ) ) {
  262. return '';
  263. } else {
  264. return $this->makeGroupNameList( $ids );
  265. }
  266. }
  267. /**
  268. * Output a form to allow searching for a user
  269. */
  270. function switchForm() {
  271. global $wgOut, $wgScript;
  272. $wgOut->addHTML(
  273. Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
  274. Xml::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
  275. Xml::openElement( 'fieldset' ) .
  276. Xml::element( 'legend', array(), wfMsg( 'userrights-lookup-user' ) ) .
  277. Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget ) . ' ' .
  278. Xml::submitButton( wfMsg( 'editusergroup' ) ) .
  279. Xml::closeElement( 'fieldset' ) .
  280. Xml::closeElement( 'form' ) . "\n"
  281. );
  282. }
  283. /**
  284. * Go through used and available groups and return the ones that this
  285. * form will be able to manipulate based on the current user's system
  286. * permissions.
  287. *
  288. * @param $groups Array: list of groups the given user is in
  289. * @return Array: Tuple of addable, then removable groups
  290. */
  291. protected function splitGroups( $groups ) {
  292. list($addable, $removable, $addself, $removeself) = array_values( $this->changeableGroups() );
  293. $removable = array_intersect(
  294. array_merge( $this->isself ? $removeself : array(), $removable ),
  295. $groups ); // Can't remove groups the user doesn't have
  296. $addable = array_diff(
  297. array_merge( $this->isself ? $addself : array(), $addable ),
  298. $groups ); // Can't add groups the user does have
  299. return array( $addable, $removable );
  300. }
  301. /**
  302. * Show the form to edit group memberships.
  303. *
  304. * @param $user User or UserRightsProxy you're editing
  305. * @param $groups Array: Array of groups the user is in
  306. */
  307. protected function showEditUserGroupsForm( $user, $groups ) {
  308. global $wgOut, $wgUser, $wgLang;
  309. $list = array();
  310. foreach( $groups as $group )
  311. $list[] = self::buildGroupLink( $group );
  312. $grouplist = '';
  313. if( count( $list ) > 0 ) {
  314. $grouplist = wfMsgHtml( 'userrights-groupsmember' );
  315. $grouplist = '<p>' . $grouplist . ' ' . $wgLang->listToText( $list ) . '</p>';
  316. }
  317. $wgOut->addHTML(
  318. Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
  319. Xml::hidden( 'user', $this->mTarget ) .
  320. Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget ) ) .
  321. Xml::openElement( 'fieldset' ) .
  322. Xml::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
  323. wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
  324. wfMsgExt( 'userrights-groups-help', array( 'parse' ) ) .
  325. $grouplist .
  326. Xml::tags( 'p', null, $this->groupCheckboxes( $groups ) ) .
  327. Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
  328. "<tr>
  329. <td class='mw-label'>" .
  330. Xml::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
  331. "</td>
  332. <td class='mw-input'>" .
  333. Xml::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
  334. "</td>
  335. </tr>
  336. <tr>
  337. <td></td>
  338. <td class='mw-submit'>" .
  339. Xml::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups', 'accesskey' => 's' ) ) .
  340. "</td>
  341. </tr>" .
  342. Xml::closeElement( 'table' ) . "\n" .
  343. Xml::closeElement( 'fieldset' ) .
  344. Xml::closeElement( 'form' ) . "\n"
  345. );
  346. }
  347. /**
  348. * Format a link to a group description page
  349. *
  350. * @param $group string
  351. * @return string
  352. */
  353. private static function buildGroupLink( $group ) {
  354. static $cache = array();
  355. if( !isset( $cache[$group] ) )
  356. $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupName( $group ) );
  357. return $cache[$group];
  358. }
  359. /**
  360. * Returns an array of all groups that may be edited
  361. * @return array Array of groups that may be edited.
  362. */
  363. protected static function getAllGroups() {
  364. return User::getAllGroups();
  365. }
  366. /**
  367. * Adds a table with checkboxes where you can select what groups to add/remove
  368. *
  369. * @param $usergroups Array: groups the user belongs to
  370. * @return string XHTML table element with checkboxes
  371. */
  372. private function groupCheckboxes( $usergroups ) {
  373. $allgroups = $this->getAllGroups();
  374. $ret = '';
  375. $column = 1;
  376. $settable_col = '';
  377. $unsettable_col = '';
  378. foreach ($allgroups as $group) {
  379. $set = in_array( $group, $usergroups );
  380. # Should the checkbox be disabled?
  381. $disabled = !(
  382. ( $set && $this->canRemove( $group ) ) ||
  383. ( !$set && $this->canAdd( $group ) ) );
  384. # Do we need to point out that this action is irreversible?
  385. $irreversible = !$disabled && (
  386. ($set && !$this->canAdd( $group )) ||
  387. (!$set && !$this->canRemove( $group ) ) );
  388. $attr = $disabled ? array( 'disabled' => 'disabled' ) : array();
  389. $text = $irreversible
  390. ? wfMsgHtml( 'userrights-irreversible-marker', User::getGroupMember( $group ) )
  391. : User::getGroupMember( $group );
  392. $checkbox = Xml::checkLabel( $text, "wpGroup-$group",
  393. "wpGroup-$group", $set, $attr );
  394. $checkbox = $disabled ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkbox ) : $checkbox;
  395. if ($disabled) {
  396. $unsettable_col .= "$checkbox<br />\n";
  397. } else {
  398. $settable_col .= "$checkbox<br />\n";
  399. }
  400. }
  401. if ($column) {
  402. $ret .= Xml::openElement( 'table', array( 'border' => '0', 'class' => 'mw-userrights-groups' ) ) .
  403. "<tr>
  404. ";
  405. if( $settable_col !== '' ) {
  406. $ret .= xml::element( 'th', null, wfMsg( 'userrights-changeable-col' ) );
  407. }
  408. if( $unsettable_col !== '' ) {
  409. $ret .= xml::element( 'th', null, wfMsg( 'userrights-unchangeable-col' ) );
  410. }
  411. $ret.= "</tr>
  412. <tr>
  413. ";
  414. if( $settable_col !== '' ) {
  415. $ret .=
  416. " <td style='vertical-align:top;'>
  417. $settable_col
  418. </td>
  419. ";
  420. }
  421. if( $unsettable_col !== '' ) {
  422. $ret .=
  423. " <td style='vertical-align:top;'>
  424. $unsettable_col
  425. </td>
  426. ";
  427. }
  428. $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' );
  429. }
  430. return $ret;
  431. }
  432. /**
  433. * @param $group String: the name of the group to check
  434. * @return bool Can we remove the group?
  435. */
  436. private function canRemove( $group ) {
  437. // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
  438. // PHP.
  439. $groups = $this->changeableGroups();
  440. return in_array( $group, $groups['remove'] ) || ($this->isself && in_array( $group, $groups['remove-self'] ));
  441. }
  442. /**
  443. * @param $group string: the name of the group to check
  444. * @return bool Can we add the group?
  445. */
  446. private function canAdd( $group ) {
  447. $groups = $this->changeableGroups();
  448. return in_array( $group, $groups['add'] ) || ($this->isself && in_array( $group, $groups['add-self'] ));
  449. }
  450. /**
  451. * Returns an array of the groups that the user can add/remove.
  452. *
  453. * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) , 'add-self' => array( addablegroups to self), 'remove-self' => array( removable groups from self) )
  454. */
  455. function changeableGroups() {
  456. global $wgUser;
  457. if( $wgUser->isAllowed( 'userrights' ) ) {
  458. // This group gives the right to modify everything (reverse-
  459. // compatibility with old "userrights lets you change
  460. // everything")
  461. // Using array_merge to make the groups reindexed
  462. $all = array_merge( User::getAllGroups() );
  463. return array(
  464. 'add' => $all,
  465. 'remove' => $all,
  466. 'add-self' => array(),
  467. 'remove-self' => array()
  468. );
  469. }
  470. // Okay, it's not so simple, we will have to go through the arrays
  471. $groups = array(
  472. 'add' => array(),
  473. 'remove' => array(),
  474. 'add-self' => array(),
  475. 'remove-self' => array() );
  476. $addergroups = $wgUser->getEffectiveGroups();
  477. foreach ($addergroups as $addergroup) {
  478. $groups = array_merge_recursive(
  479. $groups, $this->changeableByGroup($addergroup)
  480. );
  481. $groups['add'] = array_unique( $groups['add'] );
  482. $groups['remove'] = array_unique( $groups['remove'] );
  483. $groups['add-self'] = array_unique( $groups['add-self'] );
  484. $groups['remove-self'] = array_unique( $groups['remove-self'] );
  485. }
  486. // Run a hook because we can
  487. wfRunHooks( 'UserrightsChangeableGroups', array( $this, $wgUser, $addergroups, &$groups ) );
  488. return $groups;
  489. }
  490. /**
  491. * Returns an array of the groups that a particular group can add/remove.
  492. *
  493. * @param $group String: the group to check for whether it can add/remove
  494. * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) , 'add-self' => array( addablegroups to self), 'remove-self' => array( removable groups from self) )
  495. */
  496. private function changeableByGroup( $group ) {
  497. global $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
  498. $groups = array( 'add' => array(), 'remove' => array(), 'add-self' => array(), 'remove-self' => array() );
  499. if( empty($wgAddGroups[$group]) ) {
  500. // Don't add anything to $groups
  501. } elseif( $wgAddGroups[$group] === true ) {
  502. // You get everything
  503. $groups['add'] = User::getAllGroups();
  504. } elseif( is_array($wgAddGroups[$group]) ) {
  505. $groups['add'] = $wgAddGroups[$group];
  506. }
  507. // Same thing for remove
  508. if( empty($wgRemoveGroups[$group]) ) {
  509. } elseif($wgRemoveGroups[$group] === true ) {
  510. $groups['remove'] = User::getAllGroups();
  511. } elseif( is_array($wgRemoveGroups[$group]) ) {
  512. $groups['remove'] = $wgRemoveGroups[$group];
  513. }
  514. // Re-map numeric keys of AddToSelf/RemoveFromSelf to the 'user' key for backwards compatibility
  515. if( empty($wgGroupsAddToSelf['user']) || $wgGroupsAddToSelf['user'] !== true ) {
  516. foreach($wgGroupsAddToSelf as $key => $value) {
  517. if( is_int($key) ) {
  518. $wgGroupsAddToSelf['user'][] = $value;
  519. }
  520. }
  521. }
  522. if( empty($wgGroupsRemoveFromSelf['user']) || $wgGroupsRemoveFromSelf['user'] !== true ) {
  523. foreach($wgGroupsRemoveFromSelf as $key => $value) {
  524. if( is_int($key) ) {
  525. $wgGroupsRemoveFromSelf['user'][] = $value;
  526. }
  527. }
  528. }
  529. // Now figure out what groups the user can add to him/herself
  530. if( empty($wgGroupsAddToSelf[$group]) ) {
  531. } elseif( $wgGroupsAddToSelf[$group] === true ) {
  532. // No idea WHY this would be used, but it's there
  533. $groups['add-self'] = User::getAllGroups();
  534. } elseif( is_array($wgGroupsAddToSelf[$group]) ) {
  535. $groups['add-self'] = $wgGroupsAddToSelf[$group];
  536. }
  537. if( empty($wgGroupsRemoveFromSelf[$group]) ) {
  538. } elseif( $wgGroupsRemoveFromSelf[$group] === true ) {
  539. $groups['remove-self'] = User::getAllGroups();
  540. } elseif( is_array($wgGroupsRemoveFromSelf[$group]) ) {
  541. $groups['remove-self'] = $wgGroupsRemoveFromSelf[$group];
  542. }
  543. return $groups;
  544. }
  545. /**
  546. * Show a rights log fragment for the specified user
  547. *
  548. * @param $user User to show log for
  549. * @param $output OutputPage to use
  550. */
  551. protected function showLogFragment( $user, $output ) {
  552. $output->addHTML( Xml::element( 'h2', null, LogPage::logName( 'rights' ) . "\n" ) );
  553. LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage()->getPrefixedText() );
  554. }
  555. }