* 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
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
/**\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
--- /dev/null
+<?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