Guest User

Untitled

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