add revoke support. massive improvements so now you can grant or revoke to multiple...
authorchriskl <chriskl>
Wed, 21 May 2003 09:06:23 +0000 (09:06 +0000)
committerchriskl <chriskl>
Wed, 21 May 2003 09:06:23 +0000 (09:06 +0000)
BUGS
HISTORY
TODO
classes/database/Postgres.php
lang/english.php
lang/recoded/english.php
privileges.php

diff --git a/BUGS b/BUGS
index 19b195f7bec8ca90dbc4ae64d18bc87080feb790..79c264ad1bc3cba6510a73ca741350a6459d7626 100644 (file)
--- a/BUGS
+++ b/BUGS
@@ -1,2 +1,5 @@
 * Lots more printVal()ing needs to be done.  Whenever displaying user data, 
   it should use $misc->printVal($var) instead of htmlspecialchars($var).
+* Add revoke support
+* Fix grant option/grantor stuff
+* Schema support for privileges
diff --git a/HISTORY b/HISTORY
index 25ed4c134501af8f50e6946db49e91d1b86dd28f..252c582418d950a7af3940a781754c151c3a7f3e 100644 (file)
--- a/HISTORY
+++ b/HISTORY
@@ -25,6 +25,8 @@ Version 3.0-beta-1
          on field name
        - Trim long strings.  Use 'expand' and 'collapse' to
          see full strings or trimmed strings.
+* Revoke on objects.  Grant or revoke to multiple groups
+  and users at once.
 
 Version 3.0.0-dev-4
 -------------------
diff --git a/TODO b/TODO
index 0d42fe4fa3e18f9cc93eef1df76196c218df0549..376e140f418d75c21710479edcbf3d299236366b 100644 (file)
--- a/TODO
+++ b/TODO
@@ -25,7 +25,6 @@ Groups
 Permissions
 -----------
 
-* Allow revoking of perms
 * Allow display of grants for a user
 * Allow display of grants for a group
 
index c4cc669d6079c4c0db9fd8d5347e01345a0f3bc8..46a8fe70fd2da03f6c355c53f29fdacbaeccee54 100755 (executable)
@@ -4,7 +4,7 @@
  * A class that implements the DB interface for Postgres
  * Note: This class uses ADODB and returns RecordSets.
  *
- * $Id: Postgres.php,v 1.113 2003/05/20 03:54:04 chriskl Exp $
+ * $Id: Postgres.php,v 1.114 2003/05/21 09:06:23 chriskl Exp $
  */
 
 // @@@ THOUGHT: What about inherits? ie. use of ONLY???
@@ -1998,20 +1998,35 @@ class Postgres extends BaseDB {
        
        /**
         * Grants a privilege to a user, group or public
+        * @param $mode 'GRANT' or 'REVOKE';
         * @param $type The type of object
         * @param $object The name of the object
-        * @param $entity The type of entity (eg. USER, GROUP or PUBLIC)
-        * @param $name The username or groupname to grant privs to. Ignored for PUBLIC.
-        * @param $privilege The privilege to grant (eg. SELECT, ALL PRIVILEGES, etc.)
+        * @param $public True to grant to public, false otherwise
+        * @param $usernames The array of usernames to grant privs to.
+        * @param $groupnames The array of group names to grant privs to.        
+        * @param $privileges The array of privileges to grant (eg. ('SELECT', 'ALL PRIVILEGES', etc.) )
         * @return 0 success
         * @return -1 invalid type
         * @return -2 invalid entity
+        * @return -3 invalid privileges
+        * @return -4 not granting to anything
+        * @return -4 invalid mode
         */
-       function grantPrivileges($type, $object, $entity, $name, $privilege) {
+       function setPrivileges($mode, $type, $object, $public, $usernames, $groupnames, $privileges) {
                $this->fieldClean($object);
-               $this->fieldClean($name);
+               $this->fieldArrayClean($usernames);
+               $this->fieldArrayClean($groupnames);
+
+               // Input checking
+               if (!is_array($privileges) || sizeof($privileges) == 0) return -3;
+               if (!is_array($usernames) || !is_array($groupnames) || 
+                       (!$public && sizeof($usernames) == 0 && sizeof($groupnames) == 0)) return -4;
+               if ($mode != 'GRANT' && $mode != 'REVOKE') return -5;
 
-               $sql = "GRANT {$privilege} ON";
+               if (in_array('ALL PRIVILEGES', $privileges))
+                       $sql = "{$mode} ALL PRIVILEGES ON";
+               else
+                       $sql = "{$mode} " . join(', ', $privileges) . " ON";
                // @@ WE NEED SCHEMA SUPPORT BELOW
                switch ($type) {
                        case 'table':
@@ -2038,20 +2053,34 @@ class Postgres extends BaseDB {
                                return -1;
                }
                
-               switch ($entity) {
-                       case 'USER':
-                               $sql .= " TO \"{$name}\"";
-                               break;
-                       case 'GROUP':
-                               $sql .= " TO GROUP \"{$name}\"";
-                               break;
-                       case 'PUBLIC':
-                               $sql .= " TO PUBLIC";
-                               break;
-                       default:
-                               return -2;
+               // Dump PUBLIC
+               $first = true;
+               $sql .= ($mode == 'GRANT') ? ' TO ' : ' FROM ';
+               if ($public) {
+                       $sql .= 'PUBLIC';
+                       $first = false;
                }
-               
+               // Dump users
+               foreach ($usernames as $v) {
+                       if ($first) {
+                               $sql .= "\"{$v}\"";
+                               $first = false;
+                       }
+                       else {
+                               $sql .= ", \"{$v}\"";
+                       }
+               }                       
+               // Dump groups
+               foreach ($groupnames as $v) {
+                       if ($first) {
+                               $sql .= "GROUP \"{$v}\"";
+                               $first = false;
+                       }
+                       else {
+                               $sql .= ", GROUP \"{$v}\"";
+                       }
+               }                       
+
                return $this->execute($sql);
        }
  
index f6382f12a556e6698c3fbad71d7c843ce9bfaebd..d1b84d72879fcb68f78d62e01e24769b63f90fef 100755 (executable)
@@ -4,7 +4,7 @@
         * English language file for phpPgAdmin.  Use this as a basis
         * for new translations.
         *
-        * $Id: english.php,v 1.79 2003/05/20 09:01:58 chriskl Exp $
+        * $Id: english.php,v 1.80 2003/05/21 09:06:24 chriskl Exp $
         */
 
        // Language and character set
        $lang['strnoprivileges'] = 'This object has default owner privileges.';
        $lang['strgrant'] = 'Grant';
        $lang['strrevoke'] = 'Revoke';
-       $lang['strgranted'] = 'Privileges granted.';
-       $lang['strgrantfailed'] = 'Failed to grant privileges.';
-       $lang['strgrantuser'] = 'Grant User';
-       $lang['strgrantgroup'] = 'Grant Group';
+       $lang['strgranted'] = 'Privileges changed.';
+       $lang['strgrantfailed'] = 'Failed to change privileges.';
+       $lang['strgrantbad'] = 'You must specify at least one user or group and at least one privilege.';
+       $lang['stralterprivs'] = 'Alter Privileges';
 
        // Databases
        $lang['strdatabase'] = 'Database';
index 0ce7c213996dd8f0323dc156a621f0acd3edc749..b758e22423759f22a7dd2304dd4d108a8ca9119f 100644 (file)
@@ -4,7 +4,7 @@
         * English language file for phpPgAdmin.  Use this as a basis
         * for new translations.
         *
-        * $Id: english.php,v 1.31 2003/05/20 09:01:58 chriskl Exp $
+        * $Id: english.php,v 1.32 2003/05/21 09:06:24 chriskl Exp $
         */
 
        // Language and character set
        $lang['strnoprivileges'] = 'This object has default owner privileges.';
        $lang['strgrant'] = 'Grant';
        $lang['strrevoke'] = 'Revoke';
-       $lang['strgranted'] = 'Privileges granted.';
-       $lang['strgrantfailed'] = 'Failed to grant privileges.';
-       $lang['strgrantuser'] = 'Grant User';
-       $lang['strgrantgroup'] = 'Grant Group';
+       $lang['strgranted'] = 'Privileges changed.';
+       $lang['strgrantfailed'] = 'Failed to change privileges.';
+       $lang['strgrantbad'] = 'You must specify at least one user or group and at least one privilege.';
+       $lang['stralterprivs'] = 'Alter Privileges';
 
        // Databases
        $lang['strdatabase'] = 'Database';
index 3f179f2347614251c4d921772444e7449311a473..ad89c07f844685d4802f7f69187d3ee1ba573e57 100644 (file)
@@ -3,7 +3,7 @@
        /**
         * Manage privileges in a database
         *
-        * $Id: privileges.php,v 1.11 2003/05/21 07:02:12 chriskl Exp $
+        * $Id: privileges.php,v 1.12 2003/05/21 09:06:23 chriskl Exp $
         */
 
        // Include application functions
         * @peram $confirm To show entry screen
         * @param $msg (optional) A message to show
         */
-       function doGrantUser($confirm, $msg = '') {
+       function doAlter($confirm, $msg = '') {
                global $data, $localData, $misc;
                global $PHP_SELF, $lang;
 
-               if (!isset($_REQUEST['username'])) $_REQUEST['username'] = '';
-               if (!isset($_REQUEST['privilege'])) $_REQUEST['privilege'] = '';
+               if (!isset($_REQUEST['username'])) $_REQUEST['username'] = array();
+               if (!isset($_REQUEST['groupname'])) $_REQUEST['groupname'] = array();
+               if (!isset($_REQUEST['privilege'])) $_REQUEST['privilege'] = array();
                
                // Set name
                switch ($_REQUEST['type']) {
                if ($confirm) {
                        // Get users from the database
                        $users = &$localData->getUsers();
+                       // Get groups from the database
+                       $groups = &$localData->getGroups();
 
-                       echo "<h2>{$lang['strprivileges']}: ", $misc->printVal($name), ": {$lang['strgrant']}</h2>\n";
+                       echo "<h2>{$lang['strprivileges']}: ", $misc->printVal($name), ": {$lang['stralterprivs']}</h2>\n";
                        $misc->printMsg($msg);
 
                        echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
                        echo "<table>\n";
-                       echo "<tr><th class=\"data\">{$lang['struser']}</th>\n";
-                       echo "<td class=\"data1\"><select name=\"username\">\n";
-                       // Automatically prepend PUBLIC to the list of users
-                       echo "<option value=\"PUBLIC\"",
-                               ('PUBLIC' == $_REQUEST['username']) ? ' selected="selected"' : '', ">PUBLIC</option>\n";
+                       echo "<tr><th class=\"data\">{$lang['strusers']}</th>\n";
+                       echo "<td class=\"data1\"><select name=\"username[]\" multiple=\"multiple\" size=\"6\">\n";
                        while (!$users->EOF) {
                                $uname = htmlspecialchars($users->f[$data->uFields['uname']]);
                                echo "<option value=\"{$uname}\"",
                                $users->moveNext();
                        }
                        echo "</select></td></tr>\n";
-                       echo "<tr><th class=\"data\">{$lang['strprivilege']}</th>\n";
-                       echo "<td class=\"data1\"><select name=\"privilege\">\n";
-                       foreach ($data->privlist[$_REQUEST['type']] as $v) {
-                               $v = htmlspecialchars($v);
-                               echo "<option value=\"{$v}\"",
-                                       ($v == $_REQUEST['privilege']) ? ' selected="selected"' : '', ">{$v}</option>\n";
-                       }
-                       echo "</select></td></tr>\n";
-                       echo "</table>\n";
-
-                       echo "<input type=\"hidden\" name=\"action\" value=\"savegrantuser\" />\n";
-                       echo "<input type=\"hidden\" name=\"type\" value=\"", htmlspecialchars($_REQUEST['type']), "\" />\n";
-                       echo "<input type=\"hidden\" name=\"object\" value=\"", htmlspecialchars($_REQUEST['object']), "\" />\n";
-                       switch ($_REQUEST['type']) {
-                               case 'table':
-                                       echo "<input type=\"hidden\" name=\"table\" value=\"", htmlspecialchars($_REQUEST['table']), "\" />\n";
-                                       break;
-                               case 'function':
-                                       echo "<input type=\"hidden\" name=\"function\" value=\"", htmlspecialchars($_REQUEST['function']), "\" />\n";
-                                       break;
-                               default:
-                       }
-                       echo $misc->form;
-                       echo "<p><input type=\"submit\" name=\"confirm\" value=\"{$lang['strgrant']}\" />\n";
-                       echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
-                       echo "</form>\n";
-               }
-               else {
-                       $status = $localData->grantPrivileges($_REQUEST['type'], $name,
-                               ($_REQUEST['username'] == 'PUBLIC') ? 'PUBLIC' : 'USER',
-                               $_REQUEST['username'], $_REQUEST['privilege']);
-                       if ($status == 0)
-                               doDefault($lang['strgranted']);
-                       else
-                               doDefault($lang['strgrantfailed']);
-               }
-       }
-
-       /**
-        * Grant permissions on an object to a group
-        * @peram $confirm To show entry screen
-        * @param $msg (optional) A message to show
-        */
-       function doGrantGroup($confirm, $msg = '') {
-               global $data, $localData, $misc;
-               global $PHP_SELF, $lang;
-
-               if (!isset($_REQUEST['groupname'])) $_REQUEST['groupname'] = '';
-               if (!isset($_REQUEST['privilege'])) $_REQUEST['privilege'] = '';
-
-               // Set name
-               switch ($_REQUEST['type']) {
-                       case 'function':
-                               $name = $_REQUEST['function'];
-                               break;
-                       default:
-                               $name = $_REQUEST['object'];
-               }
-
-               if ($confirm) {
-                       // Get groups from the database
-                       $groups = &$localData->getGroups();
-
-                       echo "<h2>{$lang['strprivileges']}: ", $misc->printVal($name), ": {$lang['strgrant']}</h2>\n";
-                       $misc->printMsg($msg);
-
-                       echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
-                       echo "<table>\n";
-                       echo "<tr><th class=\"data\">{$lang['strgroup']}</th>\n";
-                       echo "<td class=\"data1\"><select name=\"groupname\">\n";
-                       // Automatically prepend PUBLIC to the list of groups
-                       echo "<option value=\"PUBLIC\"",
-                               ('PUBLIC' == $_REQUEST['groupname']) ? ' selected="selected"' : '', ">PUBLIC</option>\n";
+                       echo "<tr><th class=\"data\">{$lang['strgroups']}</th>\n";
+                       echo "<td class=\"data1\">\n";
+                       echo "<input type=\"checkbox\" name=\"public\"", (isset($_REQUEST['public']) ? ' selected="selected"' : ''), " />PUBLIC<br />\n";
+                       echo "<select name=\"groupname[]\" multiple=\"multiple\" size=\"6\">\n";
                        while (!$groups->EOF) {
                                $gname = htmlspecialchars($groups->f[$data->grpFields['groname']]);
                                echo "<option value=\"{$gname}\"",
                                $groups->moveNext();
                        }
                        echo "</select></td></tr>\n";
-                       echo "<tr><th class=\"data\">{$lang['strprivilege']}</th>\n";
-                       echo "<td class=\"data1\"><select name=\"privilege\">\n";
+                       echo "<tr><th class=\"data\">{$lang['strprivileges']}</th>\n";
+                       echo "<td class=\"data1\">\n";
                        foreach ($data->privlist[$_REQUEST['type']] as $v) {
                                $v = htmlspecialchars($v);
-                               echo "<option value=\"{$v}\"",
-                                       ($v == $_REQUEST['privilege']) ? ' selected="selected"' : '', ">{$v}</option>\n";
+                               echo "<input type=\"checkbox\" name=\"privilege[$v]\"", 
+                                                       isset($_REQUEST['privilege'][$v]) ? ' selected="selected"' : '', ">{$v}<br />\n";
                        }
-                       echo "</select></td></tr>\n";
+                       echo "</td></tr>\n";
                        echo "</table>\n";
 
-                       echo "<input type=\"hidden\" name=\"action\" value=\"savegrantgroup\" />\n";
+                       echo "<input type=\"hidden\" name=\"action\" value=\"save\" />\n";
                        echo "<input type=\"hidden\" name=\"type\" value=\"", htmlspecialchars($_REQUEST['type']), "\" />\n";
                        echo "<input type=\"hidden\" name=\"object\" value=\"", htmlspecialchars($_REQUEST['object']), "\" />\n";
                        switch ($_REQUEST['type']) {
                                default:
                        }
                        echo $misc->form;
-                       echo "<p><input type=\"submit\" name=\"confirm\" value=\"{$lang['strgrant']}\" />\n";
+                       echo "<p><input type=\"submit\" name=\"grant\" value=\"{$lang['strgrant']}\" />\n";
+                       echo "<input type=\"submit\" name=\"revoke\" value=\"{$lang['strrevoke']}\" />\n";
                        echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n";
                        echo "</form>\n";
                }
                else {
-                       $status = $localData->grantPrivileges($_REQUEST['type'], $name,
-                               ($_REQUEST['groupname'] == 'PUBLIC') ? 'PUBLIC' : 'GROUP',
-                               $_REQUEST['groupname'], $_REQUEST['privilege']);
+                       $status = $localData->setPrivileges(isset($_REQUEST['grant']) ? 'GRANT' : 'REVOKE', $_REQUEST['type'], $name,
+                               isset($_REQUEST['public']), $_REQUEST['username'], $_REQUEST['groupname'], array_keys($_REQUEST['privilege']));
                        if ($status == 0)
                                doDefault($lang['strgranted']);
+                       elseif ($status == -3 || $status == -4)
+                               doAlter(true, $lang['strgrantbad']);
                        else
-                               doDefault($lang['strgrantfailed']);
+                               doAlter(true, $lang['strgrantfailed']);
                }
        }
 
                // Links for granting to a user or group
                switch ($_REQUEST['type']) {
                        case 'table':
-                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=grantuser&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "&table=", urlencode($_REQUEST['table']), "\">{$lang['strgrantuser']}</a> |\n";
-                               echo "<a class=\"navlink\" href=\"{$PHP_SELF}?action=grantgroup&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "&table=", urlencode($_REQUEST['table']), "\">{$lang['strgrantgroup']}</a>\n";
+                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=alter&{$misc->href}&type={$_REQUEST['type']}&object=",
+                                       urlencode($_REQUEST['object']), "&table=", urlencode($_REQUEST['table']), "\">{$lang['stralterprivs']}</a></p>\n";
                                break;
                        case 'view':
-                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=grantuser&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "\">{$lang['strgrantuser']}</a> |\n";
-                               echo "<a class=\"navlink\" href=\"{$PHP_SELF}?action=grantgroup&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "\">{$lang['strgrantgroup']}</a>\n";
+                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=alter&{$misc->href}&type={$_REQUEST['type']}&object=",
+                                       urlencode($_REQUEST['object']), "\">{$lang['stralterprivs']}</a> |\n";
                                echo "| <a class=\"navlink\" href=\"views.php?{$misc->href}\">{$lang['strshowallviews']}</a></p>\n";
                                break;
                        case 'sequence':
-                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=grantuser&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "\">{$lang['strgrantuser']}</a> |\n";
-                               echo "<a class=\"navlink\" href=\"{$PHP_SELF}?action=grantgroup&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "\">{$lang['strgrantgroup']}</a>\n";
+                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=alter&{$misc->href}&type={$_REQUEST['type']}&object=",
+                                       urlencode($_REQUEST['object']), "\">{$lang['stralterprivs']}</a> |\n";
                                echo "| <a class=\"navlink\" href=\"sequences.php?{$misc->href}\">{$lang['strshowallsequences']}</a></p>\n";
                                break;
                        case 'database':
-                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=grantuser&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "\">{$lang['strgrantuser']}</a> |\n";
-                               echo "<a class=\"navlink\" href=\"{$PHP_SELF}?action=grantgroup&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "\">{$lang['strgrantgroup']}</a>\n";
+                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=alter&{$misc->href}&type={$_REQUEST['type']}&object=",
+                                       urlencode($_REQUEST['object']), "\">{$lang['stralterprivs']}</a></p>\n";
                                break;
                        case 'function':
-                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=grantuser&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "&function=", urlencode($_REQUEST['function']), "\">{$lang['strgrantuser']}</a> |\n";
-                               echo "<a class=\"navlink\" href=\"{$PHP_SELF}?action=grantgroup&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "&function=", urlencode($_REQUEST['function']), "\">{$lang['strgrantgroup']}</a>\n";
+                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=alter&{$misc->href}&type={$_REQUEST['type']}&object=",
+                                       urlencode($_REQUEST['object']), "&function=", urlencode($_REQUEST['function']), "\">{$lang['stralterprivs']}</a> |\n";
                                echo "| <a class=\"navlink\" href=\"functions.php?{$misc->href}\">{$lang['strshowallfunctions']}</a></p>\n";
                                break;
                        case 'schema':
-                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=grantuser&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "\">{$lang['strgrantuser']}</a> |\n";
-                               echo "<a class=\"navlink\" href=\"{$PHP_SELF}?action=grantgroup&{$misc->href}&type={$_REQUEST['type']}&object=",
-                                       urlencode($_REQUEST['object']), "\">{$lang['strgrantgroup']}</a>\n";
+                               echo "<p><a class=\"navlink\" href=\"{$PHP_SELF}?action=alter&{$misc->href}&type={$_REQUEST['type']}&object=",
+                                       urlencode($_REQUEST['object']), "\">{$lang['stralterprivs']}</a> |\n";
                                echo "| <a class=\"navlink\" href=\"database.php?database=", urlencode($_REQUEST['database']),
                                        "\">{$lang['strshowallschemas']}</a></p>\n";
                                break;
        $misc->printBody();
 
        switch ($action) {
-               case 'savegrantgroup':
-                       if (isset($_REQUEST['cancel'])) doDefault();
-                       else doGrantGroup(false);
-                       break;
-               case 'grantgroup':
-                       doGrantGroup(true);
-                       break;
-               case 'savegrantuser':
+               case 'save':
                        if (isset($_REQUEST['cancel'])) doDefault();
-                       else doGrantUser(false);
+                       else doAlter(false);
                        break;
-               case 'grantuser':
-                       doGrantUser(true);
+               case 'alter':
+                       doAlter(true);
                        break;
                default:
                        doDefault();