Handle automatic reassignment of edits from duplicate accounts to the main
[lhc/web/wiklou.git] / includes / SpecialUserrights.php
1 <?php
2 /**
3 * Provide an administration interface
4 * DO NOT USE: INSECURE.
5 *
6 * TODO : remove everything related to group editing (SpecialGrouplevels.php)
7 * @package MediaWiki
8 * @subpackage SpecialPage
9 */
10
11 /** */
12 require_once('HTMLForm.php');
13 require_once('Group.php');
14
15 /** Entry point */
16 function wfSpecialUserrights() {
17 global $wgRequest;
18 $form = new UserrightsForm($wgRequest);
19 $form->execute();
20 }
21
22 /**
23 * A class to manage user levels rights.
24 * @package MediaWiki
25 * @subpackage SpecialPage
26 */
27 class UserrightsForm extends HTMLForm {
28 var $mPosted, $mRequest, $mSaveprefs;
29 /** Escaped local url name*/
30 var $action;
31
32 /** Constructor*/
33 function UserrightsForm ( &$request ) {
34 $this->mPosted = $request->wasPosted();
35 $this->mRequest =& $request;
36 $this->mName = 'userrights';
37
38 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userrights' );
39 $this->action = $titleObj->escapeLocalURL();
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 function execute() {
47 // show the general form
48 $this->switchForm();
49 if ( $this->mPosted ) {
50 // show some more forms
51 if($this->mRequest->getCheck('ssearchuser')) {
52 $this->editUserGroupsForm( $this->mRequest->getVal('user-editname')); }
53
54 // save settings
55 if($this->mRequest->getCheck('saveusergroups')) {
56 $this->saveUserGroups($this->mRequest->getVal('user-editname'),
57 $this->mRequest->getArray('member'),
58 $this->mRequest->getArray('available'));
59 }
60 }
61 }
62
63 /**
64 * Save user groups changes in the database.
65 * Data comes from the editUserGroupsForm() form function
66 *
67 * @param string $username Username to apply changes to.
68 * @param array $removegroup id of groups to be removed.
69 * @param array $addgroup id of groups to be added.
70 *
71 */
72 function saveUserGroups($username,$removegroup,$addgroup) {
73 $u = User::newFromName($username);
74
75 if(is_null($u)) {
76 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
77 return;
78 }
79
80 if($u->getID() == 0) {
81 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
82 return;
83 }
84
85 $oldGroups = $u->getGroups();
86 $newGroups = $oldGroups;
87 $logcomment = ' ';
88 // remove then add groups
89 if(isset($removegroup)) {
90 $newGroups = array_diff($newGroups, $removegroup);
91 }
92 if(isset($addgroup)) {
93 $newGroups = array_merge($newGroups, $addgroup);
94 }
95 $newGroups = array_unique( $newGroups );
96
97 // save groups in user object and database
98 $u->setGroups($newGroups);
99 $u->saveSettings();
100
101 $log = new LogPage( 'rights' );
102 $log->addEntry( 'rights', Title::makeTitle( NS_USER, $u->getName() ), '', array( $this->makeGroupNameList( $oldGroups ),
103 $this->makeGroupNameList( $newGroups ) ) );
104 }
105
106 function makeGroupNameList( $ids ) {
107 $s = '';
108 foreach( $ids as $id ) {
109 if ( $s != '' ) {
110 $s .= ', ';
111 }
112 $groupObj = Group::newFromId( $id );
113 $s .= $groupObj->getExpandedName();
114 }
115 return $s;
116 }
117
118 /**
119 * The entry form
120 * It allows a user to look for a username and edit its groups membership
121 */
122 function switchForm() {
123 global $wgOut;
124
125 // user selection
126 $wgOut->addHTML( "<form name=\"uluser\" action=\"$this->action\" method=\"post\">\n" );
127 $wgOut->addHTML( $this->fieldset( 'lookup-user',
128 $this->textbox( 'user-editname' ) .
129 '<input type="submit" name="ssearchuser" value="'.wfMsg('editusergroup').'" />'
130 ));
131 $wgOut->addHTML( "</form>\n" );
132 }
133
134 /**
135 * Edit user groups membership
136 * @param string $username Name of the user.
137 */
138 function editUserGroupsForm($username) {
139 global $wgOut;
140
141 $user = User::newFromName($username);
142 $encUser = htmlspecialchars( $username );
143 if(is_null($user)) {
144 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort', $encUser).'</p>');
145 return;
146 }
147
148 if($user->getID() == 0) {
149 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort', $encUser).'</p>');
150 return;
151 }
152
153 $groups = $user->getGroups();
154
155 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
156 '<input type="hidden" name="user-editname" value="'.$encUser.'" />');
157 $wgOut->addHTML( $this->fieldset( 'editusergroup',
158 wfMsg('editing', $this->mRequest->getVal('user-editname')).".<br />\n" .
159 '<table border="0" align="center"><tr><td>'.
160 HTMLSelectGroups('member', $this->mName.'-groupsmember', $groups,true,6).
161 '</td><td>'.
162 HTMLSelectGroups('available', $this->mName.'-groupsavailable', $groups,true,6,true).
163 '</td></tr></table>'."\n".
164 '<p>'.wfMsg('userrights-groupshelp').'</p>'."\n".
165 '<input type="submit" name="saveusergroups" value="'.wfMsg('saveusergroups').'" />'
166 ));
167 $wgOut->addHTML( "</form>\n" );
168 }
169 } // end class UserrightsForm
170 ?>