82631514b2073ee50316edc86c7b0fe2e810b49f
[gnucomo.git] / src / web / users.php
1 <?php 
2
3 /**************************************************************************
4 ** This is free software; you can redistribute it and/or modify it under the
5 ** terms of the GNU General Public License, see the file COPYING.
6 ***************************************************************************/
7
8 /*
9  *
10  * User Administration page.
11  * Input parameters: action (POST) : empty, 'Create'
12  *                   username (POST) : name of the user to create or remove
13  */
14
15 session_start();
16 require_once('classes/gnucomo_config.php');
17 ?>
18
19 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
20 <html>
21 <head>
22 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
23 <link rel='stylesheet' href='gnucomo.css' type='text/css'>
24 <title>GNUCoMo login</title>
25
26 <script language='JavaScript'>
27 function CheckCreate(f)
28 {
29    if (f.username.value == "")
30    {
31       alert("You must supply a username");
32       return false;
33    }
34    if (f.passwd.value == "")
35    {
36       alert("You must supply a password");
37       return false;
38    }
39    if (f.passwd.value != f.pwverify.value)
40    {
41       alert("Passwords don't match");
42       return false;
43    }
44    return true;
45 }
46
47 function CheckRemove(f)
48 {
49    var message = "Are you sure you want to remove user ";
50    message += f.username.value;
51    message += " ?";
52
53    return confirm(message);
54 }
55
56 </script>
57
58 </head>
59 <body>
60 <?php
61 if (empty($_SESSION['username']))
62 {
63    echo "Please log in first.";
64 }
65 else
66 {
67    echo "<h1>User Administration</h1><hr>";
68
69    $config = new gnucomo_config;
70
71    $config->read("gnucomo");
72
73    //  Connect to the database
74    $conn = pg_connect($config->Database($_SESSION['username'], $_SESSION['password']));
75
76
77    if (isset($_POST['action']) && $_POST['action'] == 'Create' && !empty($_POST['username']))
78    {
79       pg_exec($conn, "CREATE USER " . $_POST['username'] . " PASSWORD '"
80                       . $_POST['passwd'] . "'");
81       pg_exec($conn, "INSERT INTO usr (username, security_level) VALUES ('"
82                      . $_POST['username'] . "','" . $_POST['seclevel'] . "')");
83    }
84
85    if (isset($_POST['action']) && $_POST['action'] == 'Remove' && !empty($_POST['username']))
86    {
87       pg_exec($conn, "DELETE FROM usr WHERE username='" . $_POST['username'] . "'");
88       pg_exec($conn, "DROP USER " . $_POST['username']);
89    }
90
91    $res = pg_exec($conn, "SELECT username, security_level FROM usr");
92
93    echo "<table>";
94    $usr = 0;
95    while ($usr < pg_numrows($res))
96    {
97       $u = pg_fetch_object($res, $usr);
98       ?>
99       <tr><td align='center'><img src='user.png'><br>
100              <b><?php echo $u->username ?></b>
101       </td><td>
102          Sec. Level <?php echo $u->security_level ?>
103       </td><td>
104           <?php if ($_SESSION['username'] != $u->username)
105           {
106           ?>
107           <form action='users.php' method='post' onSubmit='return CheckRemove(this)'>
108               <input type='hidden' name='username' value='<?php echo $u->username ?>'>
109               <input type='submit' name='action' value='Remove'>
110           </form>
111           <?php
112           }
113           ?>
114       </td></tr>
115       <?php
116       $usr++;
117    }
118    echo "</table>";
119
120 }
121 ?>
122
123 <h2>Create new user:</h2>
124 <p>
125
126 <form action='users.php' method='post' onSubmit='return CheckCreate(this)'>
127 User name: <input name='username' type='text'>
128 Security level: <select name='seclevel'>
129 <option value='1'>1</option>
130 <option value='2'>2</option>
131 <option value='3'>3</option>
132 <option value='4'>4</option>
133 <option value='5'>5</option>
134 </select>
135 <br>
136 Password: <input type='password' name='passwd'>
137 Verify password: <input type='password' name='pwverify'>
138 <br>
139 <input type='submit' name='action' value='Create'>
140 </form>
141 </p>
142 </body>
143 </html>