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