The buttonbar at the top of each page is now a fixed 'div' element
[gnucomo.git] / src / web / users.php
1 <?php 
2
3 /**************************************************************************
4 **  (c) Copyright 2003, Andromeda Technology & Automation
5 ** This is free software; you can redistribute it and/or modify it under the
6 ** terms of the GNU General Public License, see the file COPYING.
7 ***************************************************************************
8 ** MODULE INFORMATION *
9 ***********************
10 **      FILE NAME      : users.php
11 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
12 **      VERSION NUMBER : $Revision: 1.5 $
13 **
14 **  DESCRIPTION      :  User Administration page.
15 **                      Input parameters: action (POST) : empty, 'Create'
16 **                                 username (POST) : name of the user to create or remove
17 **
18 **  EXPORTED OBJECTS : 
19 **  LOCAL    OBJECTS : 
20 **  MODULES  USED    :
21 ***************************************************************************
22 **  ADMINISTRATIVE INFORMATION *
23 ********************************
24 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
25 **      CREATION DATE   : Dec 04, 2002
26 **      LAST UPDATE     : Feb 14, 2003
27 **      MODIFICATIONS   : 
28 **************************************************************************/
29
30 /*****************************
31    $Log: users.php,v $
32    Revision 1.5  2007-11-21 14:38:06  arjen
33    The buttonbar at the top of each page is now a fixed 'div' element
34    instead of a framed page.
35    Contributed by Edwin Nadorp.
36
37    Revision 1.4  2004/01/10 20:03:02  arjen
38    *** empty log message ***
39
40    Revision 1.3  2003/02/21 08:44:19  arjen
41    Add a new user and make him/her a member of a group.
42    Change of passwords added.
43
44    Revision 1.2  2003/02/13 09:01:29  arjen
45    All web interface pages use the page class.
46
47 ******************************/
48
49 // RCSID = "$Id: users.php,v 1.5 2007-11-21 14:38:06 arjen Exp $";
50
51 ini_set('include_path', '.:./classes:../phpclasses');
52
53 require_once('page.class.php');
54
55 function clientscripts()
56 {
57
58 ?>
59 <script type='text/ecmascript'>
60 function CheckCreate(f)
61 {
62    if (f.username.value == "")
63    {
64       alert("You must supply a username");
65       return false;
66    }
67    if (f.passwd.value == "")
68    {
69       alert("You must supply a password");
70       return false;
71    }
72    if (f.passwd.value != f.pwverify.value)
73    {
74       alert("Passwords don't match");
75       return false;
76    }
77    return true;
78 }
79
80 function CheckRemove(f)
81 {
82    var message = "Are you sure you want to remove user ";
83    message += f.username.value;
84    message += " ?";
85
86    return confirm(message);
87 }
88
89 function CheckPW(f)
90 {
91    if (f.passwd.value == "")
92    {
93       alert("You must supply a password");
94       return false;
95    }
96    if (f.passwd.value != f.pwverify.value)
97    {
98       alert("Passwords don't match");
99       return false;
100    }
101    return true;
102 }
103 </script>
104
105 <?php
106 }
107
108 class user_page extends page
109 {
110
111    function Body()
112    {
113
114    if (isset($_POST['action']) && $_POST['action'] == 'Create' && !empty($_POST['username']))
115    {
116       $query = "CREATE USER " . $_POST['username'] . " PASSWORD '"
117                       . $_POST['passwd'] . "' IN GROUP " . $_POST['group'];
118       if (pg_exec($this->database, $query) == FALSE &&
119                   strstr(pg_errormessage($this->database), "already exists") == false)
120       {
121          echo "You can not create a new user: " . pg_errormessage($this->database) . ".<br>";
122       }
123       else
124       {
125          pg_exec($this->database, "INSERT INTO usr (username, security_level) VALUES ('"
126                      . $_POST['username'] . "','" . $_POST['seclevel'] . "')");
127       }
128    }
129
130    if (isset($_POST['action']) && $_POST['action'] == 'Remove' && !empty($_POST['username']))
131    {
132       pg_exec($this->database, "DELETE FROM usr WHERE username='" . $_POST['username'] . "'");
133       pg_exec($this->database, "DROP USER " . $_POST['username']);
134    }
135
136    if (isset($_POST['action']) && $_POST['action'] == 'Change Password')
137    {
138       pg_exec($this->database, "ALTER USER " . $_SESSION['username'] .
139                                " PASSWORD '" . $_POST['passwd'] . "'");
140    }
141
142    if (isset($_GET['username']))
143    {
144       echo "<script type='text/ecmascript'>
145               document.getElementById('menu_title').innerHTML =
146               '<h1>Detailed information for user " . $_GET['username'] . "<\/h1>'</script><br>";
147       if (isset($_POST['action']) && $_POST['action'] == 'Save Changes')
148       {
149          $qry = "UPDATE usr SET display_name='" . $_POST['dspname'] . "'";
150          $qry .= ", email='" . $_POST['email'] . "'";
151          $qry .= ", security_level='" . $_POST['seclevel'] . "'";
152          $qry .= " WHERE username='" . $_GET['username'] . "'";
153
154          pg_exec($this->database, $qry);
155       }
156       $res = pg_exec($this->database, "SELECT * from usr
157                                        WHERE username='" . $_GET['username'] . "'");
158       $usr = pg_fetch_object($res, 0);
159
160       echo "<form action='users.php?username=" . $usr->username . "' method='POST'>";
161       echo "<table>";
162
163       echo "<tr><td>Display name</td><td><input name='dspname' type='text' value='";
164       echo $usr->display_name . "'></td></tr>";
165       echo "<tr><td>Email address</td><td><input name='email' type='text' value='";
166       echo $usr->email . "'></td></tr>";
167
168       echo "<tr><td>Security level</td><td><select name='seclevel'>";
169       for ($seclevel = 1; $seclevel < 6; $seclevel++)
170       {
171          echo "<option value='$seclevel'";
172          if ($seclevel == $usr->security_level)
173          {
174             echo " SELECTED";
175          }
176          echo ">$seclevel</option>\n";
177       }
178       echo "</select></td></tr>";
179       echo "</table>";
180       echo "<input type='submit' name='action' value='Save Changes'>";
181       echo "</form>";
182    }
183    else
184    {
185    echo "<script type='text/ecmascript'>document.getElementById('menu_title').innerHTML = '<h1>User Administration<\/h1>'</script><br>";
186    $res = pg_exec($this->database, "SELECT username, display_name, email, security_level
187                                     FROM usr ORDER BY username");
188
189    echo "<table>";
190    $usr = 0;
191    while ($usr < pg_numrows($res))
192    {
193       $u = pg_fetch_object($res, $usr);
194       ?>
195       <tr><td align='center'><a href='users.php?username=<?php echo $u->username ?>'><img src='user.png' alt='users'></a><br>
196              <b><?php echo $u->username ?></b>
197       </td><td>
198          <?php echo $u->display_name ?>
199       </td><td>
200          <?php echo $u->email ?>
201       </td><td>
202          Sec. Level <?php echo $u->security_level ?>
203       </td><td>
204           <?php if ($_SESSION['username'] != $u->username)
205           {
206           ?>
207           <form action='users.php' method='post' onSubmit='return CheckRemove(this)'>
208               <input type='hidden' name='username' value='<?php echo $u->username ?>'>
209               <input type='submit' name='action' value='Remove'>
210           </form>
211           <?php
212           }
213           ?>
214       </td></tr>
215       <?php
216       $usr++;
217    }
218    echo "</table>";
219
220 ?>
221
222 <h2>Create new user:</h2>
223
224 <form action='users.php' method='post' onSubmit='return CheckCreate(this)'>
225 User name: <input name='username' type='text'>
226 Group: <select name='group'>
227 <option value='view'>View</option>
228 <option value='ops'>Operator</option>
229 <option value='admin'>Admin</option>
230 </select>
231 Security level: <select name='seclevel'>
232 <option value='1'>1</option>
233 <option value='2'>2</option>
234 <option value='3'>3</option>
235 <option value='4'>4</option>
236 <option value='5'>5</option>
237 </select>
238 <br>
239 Password: <input type='password' name='passwd'>
240 Verify password: <input type='password' name='pwverify'>
241 <br>
242 <input type='submit' name='action' value='Create'>
243 </form>
244
245 <h2>Change your password:</h2>
246 <form action='users.php' method='post' onSubmit='return CheckPW(this)'>
247 New Password: <input type='password' name='passwd'>
248 Verify password: <input type='password' name='pwverify'>
249 <br>
250 <input type='submit' name='action' value='Change Password'>
251 </form>
252 <?php
253    }
254    }
255 }
256
257 $page = new user_page("Gnucomo User Administration");
258
259 $page->Showpage();
260
261 ?>