300c4fbc3688a584869aba4499facb2d65a87412
[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      : user.php
11 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
12 **      VERSION NUMBER : $Revision: 1.2 $
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 08, 2003
27 **      MODIFICATIONS   : 
28 **************************************************************************/
29
30 /*****************************
31    $Log: users.php,v $
32    Revision 1.2  2003-02-13 09:01:29  arjen
33    All web interface pages use the page class.
34
35 ******************************/
36
37 // RCSID = "$Id: users.php,v 1.2 2003-02-13 09:01:29 arjen Exp $";
38
39 ini_set('include_path', '.:./classes:../phpclasses');
40
41 require_once('page.class.php');
42
43 function clientscripts()
44 {
45
46 ?>
47 <script language='JavaScript'>
48 function CheckCreate(f)
49 {
50    if (f.username.value == "")
51    {
52       alert("You must supply a username");
53       return false;
54    }
55    if (f.passwd.value == "")
56    {
57       alert("You must supply a password");
58       return false;
59    }
60    if (f.passwd.value != f.pwverify.value)
61    {
62       alert("Passwords don't match");
63       return false;
64    }
65    return true;
66 }
67
68 function CheckRemove(f)
69 {
70    var message = "Are you sure you want to remove user ";
71    message += f.username.value;
72    message += " ?";
73
74    return confirm(message);
75 }
76
77 </script>
78
79 <?php
80 }
81
82 class user_page extends page
83 {
84
85    function Body()
86    {
87    echo "<h1>User Administration</h1><hr>";
88
89    if (isset($_POST['action']) && $_POST['action'] == 'Create' && !empty($_POST['username']))
90    {
91       pg_exec($this->database, "CREATE USER " . $_POST['username'] . " PASSWORD '"
92                       . $_POST['passwd'] . "'");
93       pg_exec($this->database, "INSERT INTO usr (username, security_level) VALUES ('"
94                      . $_POST['username'] . "','" . $_POST['seclevel'] . "')");
95    }
96
97    if (isset($_POST['action']) && $_POST['action'] == 'Remove' && !empty($_POST['username']))
98    {
99       pg_exec($this->database, "DELETE FROM usr WHERE username='" . $_POST['username'] . "'");
100       pg_exec($this->database, "DROP USER " . $_POST['username']);
101    }
102
103    $res = pg_exec($this->database, "SELECT username, security_level FROM usr");
104
105    echo "<table>";
106    $usr = 0;
107    while ($usr < pg_numrows($res))
108    {
109       $u = pg_fetch_object($res, $usr);
110       ?>
111       <tr><td align='center'><img src='user.png'><br>
112              <b><?php echo $u->username ?></b>
113       </td><td>
114          Sec. Level <?php echo $u->security_level ?>
115       </td><td>
116           <?php if ($_SESSION['username'] != $u->username)
117           {
118           ?>
119           <form action='users.php' method='post' onSubmit='return CheckRemove(this)'>
120               <input type='hidden' name='username' value='<?php echo $u->username ?>'>
121               <input type='submit' name='action' value='Remove'>
122           </form>
123           <?php
124           }
125           ?>
126       </td></tr>
127       <?php
128       $usr++;
129    }
130    echo "</table>";
131
132 ?>
133
134 <h2>Create new user:</h2>
135 <p>
136
137 <form action='users.php' method='post' onSubmit='return CheckCreate(this)'>
138 User name: <input name='username' type='text'>
139 Security level: <select name='seclevel'>
140 <option value='1'>1</option>
141 <option value='2'>2</option>
142 <option value='3'>3</option>
143 <option value='4'>4</option>
144 <option value='5'>5</option>
145 </select>
146 <br>
147 Password: <input type='password' name='passwd'>
148 Verify password: <input type='password' name='pwverify'>
149 <br>
150 <input type='submit' name='action' value='Create'>
151 </form>
152 </p>
153
154 <?php
155    }
156 }
157
158 $page = new user_page("Gnucomo User Administration");
159
160 $page->Showpage();
161
162 ?>