Basic user admin support. Doesn't do password changing yet.
authorchriskl <chriskl>
Wed, 1 May 2002 09:37:30 +0000 (09:37 +0000)
committerchriskl <chriskl>
Wed, 1 May 2002 09:37:30 +0000 (09:37 +0000)
classes/database/ADODB_base.php
classes/database/Postgres71.php
lang/template.php
public_html/topbar.php
public_html/users.php [new file with mode: 0644]

index 79c4148b06853ac30af4469b17906e38d3ee2c03..9988bdd2ed39bce9fd8c3e116e29d486b2fe69c5 100644 (file)
@@ -3,7 +3,7 @@
 /*
  * Parent class of all ADODB objects.
  *
- * $Id: ADODB_base.php,v 1.2 2002/02/18 09:46:49 chriskl Exp $
+ * $Id: ADODB_base.php,v 1.3 2002/05/01 09:37:30 chriskl Exp $
  */
 
 include_once('../libraries/adodb/adodb-errorhandler.inc.php');
@@ -33,6 +33,18 @@ class ADODB_base {
                return $str;
        }
 
+       /**
+        * Cleans (escapes) an array
+        * @param $arr The array to clean, by reference
+        * @return The cleaned array
+        */
+       function arrayClean(&$arr) {
+               reset($arr);
+               while(list($k, $v) = each($arr))
+                       $arr[$k] = addslashes($v);
+               return $arr;
+       }
+       
        /**
         * Executes a query on the underlying connection
         * @param $sql The SQL query to execute
index ef0a92c166b77960161f811d3c85be99f9cc8261..c35fbe3c3cc5e20b834a68ca760cc502295bf09e 100644 (file)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres\r
  * Note: This class uses ADODB and returns RecordSets.\r
  *\r
- * $Id: Postgres71.php,v 1.7 2002/04/15 12:16:35 chriskl Exp $\r
+ * $Id: Postgres71.php,v 1.8 2002/05/01 09:37:30 chriskl Exp $\r
  */\r
 \r
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???\r
@@ -16,6 +16,7 @@ class Postgres71 extends BaseDB {
        var $dbFields = array('dbname' => 'datname', 'dbcomment' => 'description');\r
        var $tbFields = array('tbname' => 'tablename', 'tbowner' => 'tableowner');\r
        var $vwFields = array('vwname' => 'viewname', 'vwowner' => 'viewowner', 'vwdef' => 'definition');\r
+       var $uFields = array('uname' => 'usename', 'usuper' => 'usesuper', 'ucreatedb' => 'usecreatedb', 'uexpires' => 'valuntil');\r
 \r
        // @@ Should we bother querying for this?\r
        var $_lastSystemOID = 18539;\r
@@ -556,6 +557,94 @@ class Postgres71 extends BaseDB {
        /**\r
         * Creates a new operator\r
         */\r
+\r
+       // User and group functions\r
+       \r
+       /**\r
+        * Returns all users in the database cluster\r
+        * @return All users\r
+        */\r
+       function &getUsers() {\r
+               $sql = "SELECT usename, usesuper, usecreatedb, valuntil FROM pg_shadow ORDER BY usename";\r
+               \r
+               return $this->selectSet($sql);\r
+       }\r
+       \r
+       /**\r
+        * Return information about a single user\r
+        * @param $username The username of the user to retrieve\r
+        * @return The user's data\r
+        */\r
+       function &getUser($username) {\r
+               $this->clean($username);\r
+               \r
+               $sql = "SELECT usename, usesuper, usecreatedb, valuntil FROM pg_shadow WHERE usename='{$username}'";\r
+               \r
+               return $this->selectSet($sql);\r
+       }\r
+       \r
+       /**\r
+        * Creates a new user\r
+        * @param $username The username of the user to create\r
+        * @param $password A password for the user\r
+        * @param $createdb boolean Whether or not the user can create databases\r
+        * @param $createuser boolean Whether or not the user can create other users\r
+        * @param $expiry string Format 'YYYY-MM-DD HH:MM:SS'.  When the account expires.\r
+        * @param $group (array) The groups to create the user in\r
+        * @return 0 success\r
+        */\r
+       function createUser($username, $password, $createdb, $createuser, $expiry, $groups) {\r
+               $this->clean($username);\r
+               // @@ THIS IS A PROBLEM FOR TRIMMING PASSWORD!!!\r
+               $this->clean($password);\r
+               $this->clean($expiry);\r
+               $this->arrayClean($groups);             \r
+               \r
+               $sql = "CREATE USER \"{$username}\"";\r
+               if ($password != '') $sql .= " WITH PASSWORD '{$password}'";\r
+               $sql .= ($createdb) ? ' CREATEDB' : ' NOCREATEDB';\r
+               $sql .= ($createuser) ? ' CREATEUSER' : ' NOCREATEUSER';\r
+               if (is_array($groups) && sizeof($groups) > 0) $sql .= " IN GROUP '" . join("', '", $groups) . "'";\r
+               if ($expiry != '') $sql .= " VALID UNTIL '{$expiry}'";\r
+               \r
+               return $this->execute($sql);\r
+       }       \r
+       \r
+       /**\r
+        * Adjusts a user's info\r
+        * @param $username The username of the user to modify\r
+        * @param $password A new password for the user\r
+        * @param $createdb boolean Whether or not the user can create databases\r
+        * @param $createuser boolean Whether or not the user can create other users\r
+        * @param $expiry string Format 'YYYY-MM-DD HH:MM:SS'.  When the account expires.\r
+        * @return 0 success\r
+        */\r
+       function setUser($username, $password, $createdb, $createuser, $expiry) {\r
+               $this->clean($username);\r
+               $this->clean($password);\r
+               $this->clean($expiry);\r
+               \r
+               $sql = "ALTER USER \"{$username}\"";\r
+               if ($password != '') $sql .= " WITH PASSWORD '{$password}'";\r
+               $sql .= ($createdb) ? ' CREATEDB' : ' NOCREATEDB';\r
+               $sql .= ($createuser) ? ' CREATEUSER' : ' NOCREATEUSER';\r
+               if ($expiry != '') $sql .= " VALID UNTIL '{$expiry}'";\r
+               \r
+               return $this->execute($sql);\r
+       }       \r
+       \r
+       /**\r
+        * Removes a user\r
+        * @param $username The username of the user to drop\r
+        * @return 0 success\r
+        */\r
+       function dropUser($username) {\r
+               $this->clean($username);\r
+               \r
+               $sql = "DROP USER \"{$username}\"";\r
+               \r
+               return $this->execute($sql);\r
+       }\r
         \r
        // Capabilities\r
        function hasTables() { return true; }\r
index 504e593ad32d9ce784404bde548f8c9bf2514816..67107a4e0746f35ea7068ee78c79d8ee358c6f6f 100644 (file)
@@ -4,7 +4,7 @@
         * Language template file for WebDB.  Use this to base language\r
         * files.\r
         *\r
-        * $Id: template.php,v 1.5 2002/04/15 11:57:29 chriskl Exp $\r
+        * $Id: template.php,v 1.6 2002/05/01 09:37:30 chriskl Exp $\r
         */\r
 \r
        $appLang = 'english';\r
        $strTypes = 'Types';\r
        $strAggregates = 'Aggregates';\r
        \r
+       // Users\r
+       $strUsername = 'Username';\r
+       $strPassword = 'Password';\r
+       $strSuper = 'Superuser?';\r
+       $strCreateDB = 'Create DB?';\r
+       $strExpires = 'Expires';        \r
+       $strNoUsers = 'No users found.';\r
+       \r
 ?>
\ No newline at end of file
index f65b80c6545cf351833f4071cc6ba4b29b295d4d..5888f98bf216eaa0362f986575affbcbf4a7bc05 100755 (executable)
@@ -3,7 +3,7 @@
        /**\r
         * Top menu for WebDB\r
         *\r
-        * $Id: topbar.php,v 1.3 2002/04/10 04:09:47 chriskl Exp $\r
+        * $Id: topbar.php,v 1.4 2002/05/01 09:37:30 chriskl Exp $\r
         */\r
 \r
        // Include application functions\r
@@ -23,8 +23,8 @@
        </tr>\r
        <tr>\r
                <td>\r
-                       <a class=toplink href="usrlist.php">User Admin</a> | \r
-                       <a class=toplink href="grplist.php">Group Admin</a> | \r
+                       <a class=toplink href="users.php" target="detail">User Admin</a> | \r
+                       <a class=toplink href="groups.php" target="detail">Group Admin</a> | \r
                        <a class=toplink href="login.php?mode=logout" target="_parent">Logout</a>\r
                </td>\r
        </tr>\r
diff --git a/public_html/users.php b/public_html/users.php
new file mode 100644 (file)
index 0000000..4041c3d
--- /dev/null
@@ -0,0 +1,242 @@
+<?php\r
+\r
+       /**\r
+        * Manage users in a database cluster\r
+        *\r
+        * $Id: users.php,v 1.1 2002/05/01 09:37:30 chriskl Exp $\r
+        */\r
+\r
+       // Include application functions\r
+       include_once('../conf/config.inc.php');\r
+       \r
+       if (!isset($action)) $action = '';\r
+       if (!isset($msg)) $msg = '';\r
+\r
+       /** \r
+        * Function to save after editing a user\r
+        */\r
+       function doSaveEdit() {\r
+               global $data, $username, $formCreateDB, $formSuper, $formExpires, $form;\r
+               \r
+               $status = $data->setUser($username, '', isset($formCreateDB), isset($formSuper), $formExpires);\r
+               if ($status == 0)\r
+                       doProperties('User updated.');\r
+               else\r
+                       doEdit('User update failed.');\r
+       }\r
+       \r
+       /**\r
+        * Function to allow editing of a user\r
+        */\r
+       function doEdit($msg = '') {\r
+               global $data, $misc, $username;\r
+               global $PHP_SELF, $strUsername, $strSuper, $strCreateDB, $strExpires, $strActions, $strNoUsers;\r
+       \r
+               echo "<h2>Users: ", htmlspecialchars($username), ": Edit</h2>\n";\r
+               $misc->printMsg($msg);\r
+               \r
+               $userdata = &$data->getUser($username);\r
+               \r
+               if ($userdata->recordCount() > 0) {\r
+                       $userdata->f[$data->uFields['ucreatedb']] = $data->phpBool($userdata->f[$data->uFields['ucreatedb']]);\r
+                       $userdata->f[$data->uFields['usuper']] = $data->phpBool($userdata->f[$data->uFields['usuper']]);\r
+                       echo "<form action=\"$PHP_SELF\" method=post>\n";\r
+                       echo "<table>\n";\r
+                       echo "<tr><th class=data>{$strUsername}</th><th class=data>{$strSuper}</th><th class=data>{$strCreateDB}</th><th class=data>{$strExpires}</th></tr>\n";\r
+                       echo "<tr><td class=data1>", htmlspecialchars($userdata->f[$data->uFields['uname']]), "</td>\n";\r
+                       echo "<td class=data1><input type=checkbox name=formSuper", \r
+                               ($userdata->f[$data->uFields['usuper']]) ? ' checked' : '', "></td>\n";\r
+                       echo "<td class=data1><input type=checkbox name=formCreateDB", \r
+                               ($userdata->f[$data->uFields['ucreatedb']]) ? ' checked' : '', "></td>\n";\r
+                       echo "<td class=data1><input size=30 name=formExpires value=\"", htmlspecialchars($userdata->f[$data->uFields['uexpires']]), "\"></td></tr>\n";\r
+                       echo "</table>\n";\r
+                       echo "<input type=hidden name=action value=save_edit>\n";\r
+                       echo "<input type=hidden name=username value=\"", htmlspecialchars($username), "\">\n";\r
+                       echo "<input type=submit value=Save> <input type=reset>\n";\r
+                       echo "</form>\n";\r
+               }\r
+               else echo "<p>No data.</p>\n";\r
+               \r
+               echo "<p><a class=navlink href=\"$PHP_SELF\">Show All Users</a> |\n";\r
+               echo "<a class=navlink href=\"$PHP_SELF?action=properties&username=", \r
+                       urlencode($username), "\">Properties</a></p>\n";\r
+       }\r
+       \r
+       /**\r
+        * Show read only properties for a user\r
+        */\r
+       function doProperties($msg = '') {\r
+               global $data, $misc, $username;\r
+               global $PHP_SELF, $strUsername, $strSuper, $strCreateDB, $strExpires, $strActions, $strNoUsers;\r
+       \r
+               echo "<h2>Users: ", htmlspecialchars($username), ": Properties</h2>\n";\r
+               $misc->printMsg($msg);\r
+               \r
+               $userdata = &$data->getUser($username);\r
+               \r
+               if ($userdata->recordCount() > 0) {\r
+                       echo "<table>\n";\r
+                       echo "<tr><th class=data>{$strUsername}</th><th class=data>{$strSuper}</th><th class=data>{$strCreateDB}</th><th class=data>{$strExpires}</th></tr>\n";\r
+                       echo "<tr><td class=data1>", htmlspecialchars($userdata->f[$data->uFields['uname']]), "</td>\n";\r
+                       echo "<td class=data1>", $userdata->f[$data->uFields['usuper']], "</td>\n";\r
+                       echo "<td class=data1>", $userdata->f[$data->uFields['ucreatedb']], "</td>\n";\r
+                       echo "<td class=data1>", htmlspecialchars($userdata->f[$data->uFields['uexpires']]), "</td></tr>\n";\r
+                       echo "</table>\n";\r
+               }\r
+               else echo "<p>No data.</p>\n";\r
+               \r
+               echo "<p><a class=navlink href=\"$PHP_SELF\">Show All Users</a> |\n";\r
+               echo "<a class=navlink href=\"$PHP_SELF?action=edit&username=", \r
+                       urlencode($username), "\">Edit</a></p>\n";\r
+       }\r
+       \r
+       /**\r
+        * Show confirmation of drop and perform actual drop\r
+        */\r
+       function doDrop($confirm) {\r
+               global $data, $username;\r
+               global $PHP_SELF;\r
+\r
+               if ($confirm) { \r
+                       echo "<h2>Users: ", htmlspecialchars($username), ": Drop</h2>\n";\r
+                       \r
+                       echo "<p>Are you sure you want to drop the user \"", htmlspecialchars($username), "\"?</p>\n";\r
+                       \r
+                       echo "<form action=\"$PHP_SELF\" method=\"post\">\n";\r
+                       echo "<input type=hidden name=action value=drop>\n";\r
+                       echo "<input type=hidden name=username value=\"", htmlspecialchars($username), "\">\n";\r
+                       echo "<input type=submit name=choice value=\"Yes\"> <input type=submit name=choice value=\"No\">\n";\r
+                       echo "</form>\n";\r
+               }\r
+               else {\r
+                       $status = $data->dropUser($username);\r
+                       if ($status == 0)\r
+                               doDefault('User dropped.');\r
+                       else\r
+                               doDefault('User drop failed.');\r
+               }               \r
+       }\r
+       \r
+       /**\r
+        * Displays a screen where they can enter a new user\r
+        */\r
+       function doCreate($msg = '') {\r
+               global $data, $misc, $username;\r
+               global $formUsername, $formPassword, $formSuper, $formCreateDB, $formExpires;\r
+               global $PHP_SELF, $strUsername, $strPassword, $strSuper, $strCreateDB, $strExpires, $strActions, $strNoUsers;\r
+               \r
+               if (!isset($formUsername)) $formUsername = '';\r
+               if (!isset($formUsername)) $formPassword = '';\r
+               if (!isset($formExpires)) $formExpires = '';\r
+               \r
+               echo "<h2>Users: Create User</h2>\n";\r
+               $misc->printMsg($msg);\r
+\r
+               echo "<form action=\"$PHP_SELF\" method=post>\n";\r
+               echo "<table>\n";\r
+               echo "<tr><th class=data>{$strUsername}</th><th class=data>{$strPassword}</th><th class=data>{$strSuper}</th><th class=data>{$strCreateDB}</th><th class=data>{$strExpires}</th></tr>\n";\r
+               echo "<tr><td class=data1><input size=15 name=formUsername value=\"", htmlspecialchars($formUsername), "\"></td>\n";\r
+               echo "<td class=data1><input size=15 name=formPassword value=\"", htmlspecialchars($formPassword), "\"></td>\n";\r
+               echo "<td class=data1><input type=checkbox name=formSuper", \r
+                       (isset($formSuper)) ? ' checked' : '', "></td>\n";\r
+               echo "<td class=data1><input type=checkbox name=formCreateDB", \r
+                       (isset($formCreateDB)) ? ' checked' : '', "></td>\n";\r
+               echo "<td class=data1><input size=30 name=formExpires value=\"", htmlspecialchars($formExpires), "\"></td></tr>\n";\r
+               echo "</table>\n";\r
+               echo "<input type=hidden name=action value=save_create>\n";\r
+               echo "<input type=submit value=Save> <input type=reset>\n";\r
+               echo "</form>\n";\r
+               \r
+               echo "<p><a class=navlink href=\"$PHP_SELF\">Show All Users</a></p>\n";\r
+       }\r
+       \r
+       /**\r
+        * Actually creates the new view in the database\r
+        */\r
+       function doSaveCreate() {\r
+               global $data, $formUsername, $formPassword, $formSuper, $formCreateDB, $formExpires;\r
+               \r
+               // @@ NOTE: No groups handled yet\r
+               $status = $data->createUser($formUsername, $formPassword, isset($formSuper), isset($formCreateDB), $formExpires, array());\r
+               if ($status == 0)\r
+                       doDefault('User created.');\r
+               else\r
+                       doCreate('User creation failed.');\r
+       }       \r
+\r
+       /**\r
+        * Show default list of users in the database\r
+        */\r
+       function doDefault($msg = '') {\r
+               global $data, $misc;\r
+               global $PHP_SELF, $strUsername, $strSuper, $strCreateDB, $strExpires, $strActions, $strNoUsers;\r
+               \r
+               echo "<h2>Users</h2>\n";\r
+               $misc->printMsg($msg);\r
+               \r
+               $users = &$data->getUsers();\r
+               \r
+               if ($users->recordCount() > 0) {\r
+                       echo "<table>\n";\r
+                       echo "<tr><th class=data>{$strUsername}</th><th class=data>{$strSuper}</th>";\r
+                       echo "<th class=data>{$strCreateDB}</th><th class=data>{$strExpires}</th><th colspan=2 class=data>{$strActions}</th>\n";\r
+                       $i = 0;\r
+                       while (!$users->EOF) {\r
+                               $id = (($i % 2) == 0 ? '1' : '2');\r
+                               echo "<tr><td class=data{$id}>", htmlspecialchars($users->f[$data->uFields['uname']]), "</td>\n";\r
+                               echo "<td class=data{$id}>", htmlspecialchars($users->f[$data->uFields['usuper']]), "</td>\n";\r
+                               echo "<td class=data{$id}>", htmlspecialchars($users->f[$data->uFields['ucreatedb']]), "</td>\n";\r
+                               echo "<td class=data{$id}>", htmlspecialchars($users->f[$data->uFields['uexpires']]), "</td>\n";\r
+                               echo "<td class=opbutton{$id}><a href=\"$PHP_SELF?action=properties&username=", \r
+                                       urlencode($users->f[$data->uFields['uname']]), "\">Properties</a></td>\n";\r
+                               echo "<td class=opbutton{$id}><a href=\"$PHP_SELF?action=confirm_drop&username=", \r
+                                       urlencode($users->f[$data->uFields['uname']]), "\">Drop</a></td>\n";\r
+                               echo "</tr>\n";\r
+                               $users->moveNext();\r
+                               $i++;\r
+                       }\r
+                       echo "</table>\n";\r
+               }\r
+               else {\r
+                       echo "<p>{$strNoUsers}</p>\n";\r
+               }\r
+               \r
+               echo "<p><a class=navlink href=\"$PHP_SELF?action=create\">Create User</a></p>\n";\r
+\r
+       }\r
+\r
+       echo "<html>\n";\r
+       echo "<body>\n";\r
+       \r
+       switch ($action) {\r
+               case 'save_create':\r
+                       doSaveCreate();\r
+                       break;\r
+               case 'create':\r
+                       doCreate();\r
+                       break;\r
+               case 'drop':\r
+                       if ($choice == 'Yes') doDrop(false);\r
+                       else doDefault();\r
+                       break;\r
+               case 'confirm_drop':\r
+                       doDrop(true);\r
+                       break;                  \r
+               case 'save_edit':\r
+                       doSaveEdit();\r
+                       break;\r
+               case 'edit':\r
+                       doEdit();\r
+                       break;\r
+               case 'properties':\r
+                       doProperties();\r
+                       break;\r
+               default:\r
+                       doDefault();\r
+                       break;\r
+       }       \r
+\r
+       echo "</body>\n";\r
+       echo "</html>\n";\r
+       \r
+?>
\ No newline at end of file