3 * Provide an administration interface
4 * DO NOT USE: INSECURE.
8 require_once('HTMLForm.php');
9 require_once('Group.php');
12 function wfSpecialUserlevels($par=null) {
15 $form = new UserlevelsForm($wgRequest);
20 * A class to manage user levels rights.
22 class UserlevelsForm
extends HTMLForm
{
23 var $mPosted, $mRequest, $mSaveprefs;
24 /** Escaped local url name*/
28 function UserlevelsForm ( &$request ) {
29 $this->mPosted
= $request->wasPosted();
30 $this->mRequest
= $request;
31 $this->mName
= 'userlevels';
33 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlevels' );
34 $this->action
= $titleObj->escapeLocalURL();
38 * Manage forms to be shown according to posted datas.
39 * Depending on the submit button used : Call a form or a saving function.
42 // show the general form
44 if ( $this->mPosted
) {
45 // show some more forms
46 if($this->mRequest
->getCheck('seditgroup')) {
47 $this->editGroupForm( $this->mRequest
->getVal($this->mName
.'-group-edit') ); }
48 if($this->mRequest
->getCheck('saddgroup')) {
49 $this->editGroupForm( ); }
50 if($this->mRequest
->getCheck('ssearchuser')) {
51 $this->editUserGroupsForm( $this->mRequest
->getVal('user-editname')); }
54 if($this->mRequest
->getCheck('savegroup')) {
55 $this->saveGroup($this->mRequest
->getVal('editgroup-name'),
56 $this->mRequest
->getVal('editgroup-oldname'),
57 $this->mRequest
->getVal('editgroup-description'));
59 } elseif($this->mRequest
->getCheck('saveusergroups')) {
60 $this->saveUserGroups($this->mRequest
->getVal('user-editname'),
61 $this->mRequest
->getVal($this->mName
.'-groupsmember'),
62 $this->mRequest
->getVal($this->mName
.'-groupsavailable'));
70 * @param string $newname Group name.
71 * @param string $oldname Old (current) group name.
72 * @param string $description Group description.
73 * @todo FIXME : doesnt validate anything.
75 function saveGroup($newname, $oldname, $description) {
76 $newame = trim($newname);
79 // We create a new group
83 $g = Group
::newFromName($oldname);
87 $g->setName($newname);
88 $g->setDescription($description);
93 * Save user groups changes in the database.
94 * Datas comes from the editUserGroupsForm() form function
96 * @param string $username Username to apply changes to.
97 * @param array $removegroup id of groups to be removed.
98 * @param array $addgroup id of groups to be added.
100 function saveUserGroups($username,$removegroup,$addgroup) {
101 $u = User
::NewFromName($username);
106 print_r($removegroup);
109 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
112 $id = $u->idForName();
114 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
119 $groups = $u->getGroups();
120 // remove then add groups
121 $groups = array_diff($groups, $removegroup);
122 $groups = array_merge($groups, $addgroup);
123 // save groups in user object and database
124 $u->setGroups($groups);
130 * It allow a user to select or eventually add a group as well as looking up
133 function switchForm() {
137 $wgOut->addHTML( "<form name=\"ulgroup\" action=\"$this->action\" method=\"post\">" );
138 $wgOut->addHTML( $this->fieldset( 'lookup-group',
139 $this->HTMLSelectGroups('group-edit', array(0 => $this->mRequest
->getVal($this->mName
.'-group-edit')) ) .
140 ' <input type="submit" name="seditgroup" value="'.wfMsg('editgroup').'">' .
141 '<br /><input type="submit" name="saddgroup" value="'.wfMsg('addgroup').'">'
143 $wgOut->addHTML( "</form>" );
146 $wgOut->addHTML( "<form name=\"uluser\" action=\"$this->action\" method=\"post\">" );
147 $wgOut->addHTML( $this->fieldset( 'lookup-user',
148 $this->textbox( 'user-editname' ) .
149 '<input type="submit" name="ssearchuser" value="'.wfMsg('editusergroup').'">'
151 $wgOut->addHTML( "</form>" );
155 * Edit a group properties and rights.
156 * @param string $groupname Name of a group to be edited.
158 function editGroupForm($groupID = 0) {
161 if($this->mRequest
->getVal('seditgroup')) {
162 // fetch data if we edit a group
163 $g = Group
::newFromID($groupID);
164 $gName = $g->getName();
165 $gDescription = $g->getDescription();
166 $fieldname = 'editgroup';
168 // default datas when we add a group
171 $fieldname = 'addgroup';
174 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">".
175 '<input type="hidden" name="editgroup-oldname" value="'.$gName.'">');
176 $wgOut->addHTML( $this->fieldset( $fieldname,
177 $this->textbox( 'editgroup-name', $gName ) .
178 $this->textareabox( 'editgroup-description', $gDescription ) .
179 '<input type="submit" name="savegroup" value="'.wfMsg('savegroup').'">'
181 $wgOut->addHTML( "</form>" );
185 * Edit user groups membership
186 * @param string $username Name of the user.
188 function editUserGroupsForm($username) {
191 $user = User
::newFromName($username);
193 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
196 $id = $user->idForName();
198 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
203 $groups = $user->getGroups();
205 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">".
206 '<input type="hidden" name="user-editname" value="'.$username.'">');
207 $wgOut->addHTML( $this->fieldset( 'editusergroup',
208 wfMsg('editing', $this->mRequest
->getVal('user-editname')).'.<br />' .
209 '<table border="0"><tr><td>'.
210 $this->HTMLSelectGroups('groupsmember', $groups,true,6).
212 $this->HTMLSelectGroups('groupsavailable', $groups,true,6,true).
213 '</td></tr></table>'.
214 '<p>'.wfMsg('userlevels-groupshelp').'</p>'.
215 '<input type="submit" name="saveusergroups" value="'.wfMsg('saveusergroups').'">'
217 $wgOut->addHTML( "</form>" );
221 /** Build a select with all existent groups
222 * @param string $selectname Name of this element. Name of form is automaticly prefixed.
223 * @param array $selected Array of element selected when posted. Multiples will only show them.
224 * @param boolean $multiple A multiple elements select.
225 * @param integer $size Number of element to be shown ignored for non multiple (default 6).
226 * @param boolean $reverse If true, multiple select will hide selected elements (default false).
228 function HTMLSelectGroups($selectname, $selected=array(), $multiple=false, $size=6, $reverse=false) {
229 $selectname = $this->mName
.'-'.$selectname;
230 $dbr =& wfGetDB( DB_SLAVE
);
231 $sql = 'SELECT group_id, group_name FROM `group`';
232 $res = $dbr->query($sql,'wfSpecialAdmin');
234 $out = wfMsg($selectname);
235 $out .= '<select name="'.$selectname;
236 if($multiple) { $out.='[]" multiple size="'.$size; }
239 while($g = $dbr->fetchObject( $res ) ) {
241 // for multiple will only show the things we want
242 if(in_array($g->group_id
, $selected) xor $reverse) {
243 $out .= '<option value="'.$g->group_id
.'">'.$g->group_name
.'</option>';
247 if(in_array($g->group_id
, $selected)) { $out .= 'selected '; }
248 $out .= 'value="'.$g->group_id
.'">'.$g->group_name
.'</option>';