Add a new user and make him/her a member of a group.
[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.3 $
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.3  2003-02-21 08:44:19  arjen
33    Add a new user and make him/her a member of a group.
34    Change of passwords added.
35
36    Revision 1.2  2003/02/13 09:01:29  arjen
37    All web interface pages use the page class.
38
39 ******************************/
40
41 // RCSID = "$Id: users.php,v 1.3 2003-02-21 08:44:19 arjen Exp $";
42
43 ini_set('include_path', '.:./classes:../phpclasses');
44
45 require_once('page.class.php');
46
47 function clientscripts()
48 {
49
50 ?>
51 <script language='JavaScript'>
52 function CheckCreate(f)
53 {
54    if (f.username.value == "")
55    {
56       alert("You must supply a username");
57       return false;
58    }
59    if (f.passwd.value == "")
60    {
61       alert("You must supply a password");
62       return false;
63    }
64    if (f.passwd.value != f.pwverify.value)
65    {
66       alert("Passwords don't match");
67       return false;
68    }
69    return true;
70 }
71
72 function CheckRemove(f)
73 {
74    var message = "Are you sure you want to remove user ";
75    message += f.username.value;
76    message += " ?";
77
78    return confirm(message);
79 }
80
81 function CheckPW(f)
82 {
83    if (f.passwd.value == "")
84    {
85       alert("You must supply a password");
86       return false;
87    }
88    if (f.passwd.value != f.pwverify.value)
89    {
90       alert("Passwords don't match");
91       return false;
92    }
93    return true;
94 }
95 </script>
96
97 <?php
98 }
99
100 class user_page extends page
101 {
102
103    function Body()
104    {
105    echo "<h1>User Administration</h1><hr>";
106
107    if (isset($_POST['action']) && $_POST['action'] == 'Create' && !empty($_POST['username']))
108    {
109       $query = "CREATE USER " . $_POST['username'] . " PASSWORD '"
110                       . $_POST['passwd'] . "' IN GROUP " . $_POST['group'];
111       if (pg_exec($this->database, $query) == FALSE)
112       {
113          echo "You can not create a new user.<br>";
114       }
115       else
116       {
117          pg_exec($this->database, "INSERT INTO usr (username, security_level) VALUES ('"
118                      . $_POST['username'] . "','" . $_POST['seclevel'] . "')");
119       }
120    }
121
122    if (isset($_POST['action']) && $_POST['action'] == 'Remove' && !empty($_POST['username']))
123    {
124       pg_exec($this->database, "DELETE FROM usr WHERE username='" . $_POST['username'] . "'");
125       pg_exec($this->database, "DROP USER " . $_POST['username']);
126    }
127
128    if (isset($_POST['action']) && $_POST['action'] == 'Change Password')
129    {
130       pg_exec($this->database, "ALTER USER " . $_SESSION['username'] .
131                                " PASSWORD '" . $_POST['passwd'] . "'");
132    }
133
134    $res = pg_exec($this->database, "SELECT username, security_level FROM usr ORDER BY username");
135
136    echo "<table>";
137    $usr = 0;
138    while ($usr < pg_numrows($res))
139    {
140       $u = pg_fetch_object($res, $usr);
141       ?>
142       <tr><td align='center'><img src='user.png'><br>
143              <b><?php echo $u->username ?></b>
144       </td><td>
145          Sec. Level <?php echo $u->security_level ?>
146       </td><td>
147           <?php if ($_SESSION['username'] != $u->username)
148           {
149           ?>
150           <form action='users.php' method='post' onSubmit='return CheckRemove(this)'>
151               <input type='hidden' name='username' value='<?php echo $u->username ?>'>
152               <input type='submit' name='action' value='Remove'>
153           </form>
154           <?php
155           }
156           ?>
157       </td></tr>
158       <?php
159       $usr++;
160    }
161    echo "</table>";
162
163 ?>
164
165 <h2>Create new user:</h2>
166 <p>
167
168 <form action='users.php' method='post' onSubmit='return CheckCreate(this)'>
169 User name: <input name='username' type='text'>
170 Group: <select name='group'>
171 <option value='view'>View</option>
172 <option value='ops'>Operator</option>
173 <option value='admin'>Admin</option>
174 </select>
175 Security level: <select name='seclevel'>
176 <option value='1'>1</option>
177 <option value='2'>2</option>
178 <option value='3'>3</option>
179 <option value='4'>4</option>
180 <option value='5'>5</option>
181 </select>
182 <br>
183 Password: <input type='password' name='passwd'>
184 Verify password: <input type='password' name='pwverify'>
185 <br>
186 <input type='submit' name='action' value='Create'>
187 </form>
188 </p>
189
190 <h2>Change your password:</h2>
191 <form action='users.php' method='post' onSubmit='return CheckPW(this)'>
192 New Password: <input type='password' name='passwd'>
193 Verify password: <input type='password' name='pwverify'>
194 <br>
195 <input type='submit' name='action' value='Change Password'>
196 </form>
197 <?php
198    }
199 }
200
201 $page = new user_page("Gnucomo User Administration");
202
203 $page->Showpage();
204
205 ?>