Rework the user_groups system, again, into something that seems to actually
[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
14 /** Entry point */
15 function wfSpecialUserrights() {
16 global $wgRequest;
17 $form = new UserrightsForm($wgRequest);
18 $form->execute();
19 }
20
21 /**
22 * A class to manage user levels rights.
23 * @package MediaWiki
24 * @subpackage SpecialPage
25 */
26 class UserrightsForm extends HTMLForm {
27 var $mPosted, $mRequest, $mSaveprefs;
28 /** Escaped local url name*/
29 var $action;
30
31 /** Constructor*/
32 function UserrightsForm ( &$request ) {
33 $this->mPosted = $request->wasPosted();
34 $this->mRequest =& $request;
35 $this->mName = 'userrights';
36
37 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userrights' );
38 $this->action = $titleObj->escapeLocalURL();
39 }
40
41 /**
42 * Manage forms to be shown according to posted data.
43 * Depending on the submit button used, call a form or a save function.
44 */
45 function execute() {
46 // show the general form
47 $this->switchForm();
48 if ( $this->mPosted ) {
49 // show some more forms
50 if($this->mRequest->getCheck('ssearchuser')) {
51 $this->editUserGroupsForm( $this->mRequest->getVal('user-editname')); }
52
53 // save settings
54 if($this->mRequest->getCheck('saveusergroups')) {
55 $this->saveUserGroups($this->mRequest->getVal('user-editname'),
56 $this->mRequest->getArray('member'),
57 $this->mRequest->getArray('available'));
58 }
59 }
60 }
61
62 /**
63 * Save user groups changes in the database.
64 * Data comes from the editUserGroupsForm() form function
65 *
66 * @param string $username Username to apply changes to.
67 * @param array $removegroup id of groups to be removed.
68 * @param array $addgroup id of groups to be added.
69 *
70 */
71 function saveUserGroups($username,$removegroup,$addgroup) {
72 $u = User::newFromName($username);
73
74 if(is_null($u)) {
75 $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
76 return;
77 }
78
79 if($u->getID() == 0) {
80 $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
81 return;
82 }
83
84 $oldGroups = $u->getGroups();
85 $newGroups = $oldGroups;
86 $logcomment = ' ';
87 // remove then add groups
88 if(isset($removegroup)) {
89 $newGroups = array_diff($newGroups, $removegroup);
90 }
91 if(isset($addgroup)) {
92 $newGroups = array_merge($newGroups, $addgroup);
93 }
94 $newGroups = array_unique( $newGroups );
95
96 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
97 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
98
99 // save groups in user object and database
100 foreach( $removegroup as $group ) {
101 $u->removeGroup( $group );
102 }
103 foreach( $addgroup as $group ) {
104 $u->addGroup( $group );
105 }
106
107 $log = new LogPage( 'rights' );
108 $log->addEntry( 'rights', Title::makeTitle( NS_USER, $u->getName() ), '', array( $this->makeGroupNameList( $oldGroups ),
109 $this->makeGroupNameList( $newGroups ) ) );
110 }
111
112 function makeGroupNameList( $ids ) {
113 return implode( ', ', $ids );
114 }
115
116 /**
117 * The entry form
118 * It allows a user to look for a username and edit its groups membership
119 */
120 function switchForm() {
121 global $wgOut;
122
123 // user selection
124 $wgOut->addHTML( "<form name=\"uluser\" action=\"$this->action\" method=\"post\">\n" );
125 $wgOut->addHTML( $this->fieldset( 'lookup-user',
126 $this->textbox( 'user-editname' ) .
127 wfElement( 'input', array(
128 'type' => 'submit',
129 'name' => 'ssearchuser',
130 'value' => wfMsg( 'editusergroup' ) ) )
131 ));
132 $wgOut->addHTML( "</form>\n" );
133 }
134
135 /**
136 * Edit user groups membership
137 * @param string $username Name of the user.
138 */
139 function editUserGroupsForm($username) {
140 global $wgOut;
141
142 $user = User::newFromName($username);
143 if( is_null( $user ) || $user->getID() == 0 ) {
144 $wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
145 return;
146 }
147
148 $groups = $user->getGroups();
149
150 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
151 wfElement( 'input', array(
152 'type' => 'hidden',
153 'name' => 'user-editname',
154 'value' => $username ) ) .
155 $this->fieldset( 'editusergroup',
156 $wgOut->parse( wfMsg('editing', $username ) ) .
157 '<table border="0" align="center"><tr><td>'.
158 HTMLSelectGroups('member', $this->mName.'-groupsmember', $groups,true,6).
159 '</td><td>'.
160 HTMLSelectGroups('available', $this->mName.'-groupsavailable', $groups,true,6,true).
161 '</td></tr></table>'."\n".
162 $wgOut->parse( wfMsg('userrights-groupshelp') ) .
163 wfElement( 'input', array(
164 'type' => 'submit',
165 'name' => 'saveusergroups',
166 'value' => wfMsg( 'saveusergroups' ) ) )
167 ));
168 $wgOut->addHTML( "</form>\n" );
169 }
170 } // end class UserrightsForm
171 ?>