Clarification for $wgRestrictionLevels
[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 This code is disgusting and needs a total rewrite
8 */
9
10 /** */
11 require_once( dirname(__FILE__) . '/HTMLForm.php');
12
13 /** Entry point */
14 function wfSpecialUserrights() {
15 global $wgRequest;
16 $form = new UserrightsForm($wgRequest);
17 $form->execute();
18 }
19
20 /**
21 * A class to manage user levels rights.
22 * @addtogroup SpecialPage
23 */
24 class UserrightsForm extends HTMLForm {
25 var $mPosted, $mRequest, $mSaveprefs;
26 /** Escaped local url name*/
27 var $action;
28
29 /** Constructor*/
30 function UserrightsForm ( &$request ) {
31 $this->mPosted = $request->wasPosted();
32 $this->mRequest =& $request;
33 $this->mName = 'userrights';
34
35 $titleObj = SpecialPage::getTitleFor( 'Userrights' );
36 $this->action = $titleObj->escapeLocalURL();
37 }
38
39 /**
40 * Manage forms to be shown according to posted data.
41 * Depending on the submit button used, call a form or a save function.
42 */
43 function execute() {
44 // show the general form
45 $this->switchForm();
46 if( $this->mPosted ) {
47 // show some more forms
48 if( $this->mRequest->getCheck( 'ssearchuser' ) ) {
49 $this->editUserGroupsForm( $this->mRequest->getVal( 'user-editname' ) );
50 }
51
52 // save settings
53 if( $this->mRequest->getCheck( 'saveusergroups' ) ) {
54 global $wgUser;
55 $username = $this->mRequest->getVal( 'user-editname' );
56 $reason = $this->mRequest->getVal( 'user-reason' );
57 if( $wgUser->matchEditToken( $this->mRequest->getVal( 'wpEditToken' ), $username ) ) {
58 $this->saveUserGroups( $username,
59 $this->mRequest->getArray( 'member' ),
60 $this->mRequest->getArray( 'available' ),
61 $reason );
62 }
63 }
64 }
65 }
66
67 /**
68 * Save user groups changes in the database.
69 * Data comes from the editUserGroupsForm() form function
70 *
71 * @param string $username Username to apply changes to.
72 * @param array $removegroup id of groups to be removed.
73 * @param array $addgroup id of groups to be added.
74 * @param string $reason Reason for group change
75 *
76 */
77 function saveUserGroups( $username, $removegroup, $addgroup, $reason = '' ) {
78 global $wgOut;
79 $u = User::newFromName($username);
80
81 if(is_null($u)) {
82 $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
83 return;
84 }
85
86 if($u->getID() == 0) {
87 $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
88 return;
89 }
90
91 $oldGroups = $u->getGroups();
92 $newGroups = $oldGroups;
93 // remove then add groups
94 if(isset($removegroup)) {
95 $newGroups = array_diff($newGroups, $removegroup);
96 foreach( $removegroup as $group ) {
97 $u->removeGroup( $group );
98 }
99 }
100 if(isset($addgroup)) {
101 $newGroups = array_merge($newGroups, $addgroup);
102 foreach( $addgroup as $group ) {
103 $u->addGroup( $group );
104 }
105 }
106 $newGroups = array_unique( $newGroups );
107
108 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
109 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
110
111 wfRunHooks( 'UserRights', array( &$u, $addgroup, $removegroup ) );
112 $log = new LogPage( 'rights' );
113 $log->addEntry( 'rights', Title::makeTitle( NS_USER, $u->getName() ), $reason, array( $this->makeGroupNameList( $oldGroups ),
114 $this->makeGroupNameList( $newGroups ) ) );
115 }
116
117 function makeGroupNameList( $ids ) {
118 return implode( ', ', $ids );
119 }
120
121 /**
122 * Output a form to allow searching for a user
123 */
124 function switchForm() {
125 global $wgOut, $wgRequest;
126 $username = $wgRequest->getText( 'user-editname' );
127 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->action, 'name' => 'uluser' ) );
128 $form .= '<fieldset><legend>' . wfMsgHtml( 'userrights-lookup-user' ) . '</legend>';
129 $form .= '<p>' . Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user-editname', 'username', 30, $username ) . '</p>';
130 $form .= '<p>' . Xml::submitButton( wfMsg( 'editusergroup' ), array( 'name' => 'ssearchuser' ) ) . '</p>';
131 $form .= '</fieldset>';
132 $form .= '</form>';
133 $wgOut->addHTML( $form );
134 }
135
136 /**
137 * Edit user groups membership
138 * @param string $username Name of the user.
139 */
140 function editUserGroupsForm($username) {
141 global $wgOut;
142
143 $user = User::newFromName($username);
144 if( is_null( $user ) ) {
145 $wgOut->addWikiText( wfMsg( 'nouserspecified' ) );
146 return;
147 } elseif( $user->getID() == 0 ) {
148 $wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
149 return;
150 }
151
152 $groups = $user->getGroups();
153 $this->showEditUserGroupsForm( $username, $groups );
154 }
155
156 function showEditUserGroupsForm( $username, $groups ) {
157 global $wgOut, $wgUser;
158 $wgOut->addHTML(
159 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->action, 'name' => 'editGroup' ) ) .
160 Xml::hidden( 'user-editname', $username ) .
161 Xml::hidden( 'wpEditToken', $wgUser->editToken( $username ) ) .
162 Xml::openElement( 'fieldset' ) .
163 Xml::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
164 $wgOut->parse( wfMsg( 'editinguser', $username ) ) .
165 "<table border='0'>
166 <tr>
167 <td></td>
168 <td>
169 <table width='400'>
170 <tr>
171 <td width='50%'>" . HTMLSelectGroups( 'member', $this->mName.'-groupsmember', $groups, true, 6 ) . "</td>
172 <td width='50%'>" . HTMLSelectGroups( 'available', $this->mName.'-groupsavailable', $groups, true, 6, true) . "</td>
173 </tr>
174 </table>
175 </tr>
176 <tr>
177 <td colspan='2'>" .
178 $wgOut->parse( wfMsg('userrights-groupshelp') ) .
179 "</td>
180 </tr>
181 <tr>
182 <td>" .
183 Xml::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
184 "</td>
185 <td>" .
186 Xml::input( 'user-reason', 60, false, array( 'id' => 'wpReason' ) ) .
187 "</td>
188 </tr>
189 <tr>
190 <td></td>
191 <td>" .
192 Xml::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
193 "</td>
194 </tr>
195 </table>\n" .
196 Xml::closeElement( 'fieldset' ) .
197 Xml::closeElement( 'form' ) . "\n"
198 );
199 }
200 } // end class UserrightsForm
201 ?>