PMA\libraries\Util::currentUserHasPrivilege PHP Method

currentUserHasPrivilege() public static method

Example: currentUserHasPrivilege('CREATE ROUTINE', 'mydb'); Checks if the currently logged in user has the global 'CREATE ROUTINE' privilege or, if not, checks if the user has this privilege on database 'mydb'.
public static currentUserHasPrivilege ( string $priv, mixed $db = null, mixed $tbl = null ) : boolean
$priv string The privilege to check
$db mixed null, to only check global privileges string, db name where to also check for privileges
$tbl mixed null, to only check global/db privileges string, table name where to also check for privileges
return boolean
    public static function currentUserHasPrivilege($priv, $db = null, $tbl = null)
    {
        // Get the username for the current user in the format
        // required to use in the information schema database.
        list($user, $host) = $GLOBALS['dbi']->getCurrentUserAndHost();
        if ($user === '') {
            // MySQL is started with --skip-grant-tables
            return true;
        }
        $username = "''";
        $username .= str_replace("'", "''", $user);
        $username .= "''@''";
        $username .= str_replace("'", "''", $host);
        $username .= "''";
        // Prepare the query
        $query = "SELECT `PRIVILEGE_TYPE` FROM `INFORMATION_SCHEMA`.`%s` " . "WHERE GRANTEE='%s' AND PRIVILEGE_TYPE='%s'";
        // Check global privileges first.
        $user_privileges = $GLOBALS['dbi']->fetchValue(sprintf($query, 'USER_PRIVILEGES', $username, $priv));
        if ($user_privileges) {
            return true;
        }
        // If a database name was provided and user does not have the
        // required global privilege, try database-wise permissions.
        if ($db !== null) {
            $query .= " AND '%s' LIKE `TABLE_SCHEMA`";
            $schema_privileges = $GLOBALS['dbi']->fetchValue(sprintf($query, 'SCHEMA_PRIVILEGES', $username, $priv, $GLOBALS['dbi']->escapeString($db)));
            if ($schema_privileges) {
                return true;
            }
        } else {
            // There was no database name provided and the user
            // does not have the correct global privilege.
            return false;
        }
        // If a table name was also provided and we still didn't
        // find any valid privileges, try table-wise privileges.
        if ($tbl !== null) {
            // need to escape wildcards in db and table names, see bug #3518484
            $tbl = str_replace(array('%', '_'), array('\\%', '\\_'), $tbl);
            $query .= " AND TABLE_NAME='%s'";
            $table_privileges = $GLOBALS['dbi']->fetchValue(sprintf($query, 'TABLE_PRIVILEGES', $username, $priv, $GLOBALS['dbi']->escapeString($db), $GLOBALS['dbi']->escapeString($tbl)));
            if ($table_privileges) {
                return true;
            }
        }
        // If we reached this point, the user does not
        // have even valid table-wise privileges.
        return false;
    }

Usage Example

Esempio n. 1
0
 /**
  * Returns the db tabs as an array
  *
  * @return array Data for generating db tabs
  */
 private function _getDbTabs()
 {
     $db_is_system_schema = $GLOBALS['dbi']->isSystemSchema($this->_db);
     $num_tables = count($GLOBALS['dbi']->getTables($this->_db));
     $is_superuser = $GLOBALS['dbi']->isSuperuser();
     $isCreateOrGrantUser = $GLOBALS['dbi']->isUserType('grant') || $GLOBALS['dbi']->isUserType('create');
     /**
      * Gets the relation settings
      */
     $cfgRelation = PMA_getRelationsParam();
     $tabs = array();
     $tabs['structure']['link'] = 'db_structure.php';
     $tabs['structure']['text'] = __('Structure');
     $tabs['structure']['icon'] = 'b_props.png';
     $tabs['sql']['link'] = 'db_sql.php';
     $tabs['sql']['text'] = __('SQL');
     $tabs['sql']['icon'] = 'b_sql.png';
     $tabs['search']['text'] = __('Search');
     $tabs['search']['icon'] = 'b_search.png';
     $tabs['search']['link'] = 'db_search.php';
     if ($num_tables == 0) {
         $tabs['search']['warning'] = __('Database seems to be empty!');
     }
     $tabs['qbe']['text'] = __('Query');
     $tabs['qbe']['icon'] = 's_db.png';
     $tabs['qbe']['link'] = 'db_qbe.php';
     if ($num_tables == 0) {
         $tabs['qbe']['warning'] = __('Database seems to be empty!');
     }
     $tabs['export']['text'] = __('Export');
     $tabs['export']['icon'] = 'b_export.png';
     $tabs['export']['link'] = 'db_export.php';
     if ($num_tables == 0) {
         $tabs['export']['warning'] = __('Database seems to be empty!');
     }
     if (!$db_is_system_schema) {
         $tabs['import']['link'] = 'db_import.php';
         $tabs['import']['text'] = __('Import');
         $tabs['import']['icon'] = 'b_import.png';
         $tabs['operation']['link'] = 'db_operations.php';
         $tabs['operation']['text'] = __('Operations');
         $tabs['operation']['icon'] = 'b_tblops.png';
         if ($is_superuser || $isCreateOrGrantUser) {
             $tabs['privileges']['link'] = 'server_privileges.php';
             $tabs['privileges']['args']['checkprivsdb'] = $this->_db;
             // stay on database view
             $tabs['privileges']['args']['viewing_mode'] = 'db';
             $tabs['privileges']['text'] = __('Privileges');
             $tabs['privileges']['icon'] = 's_rights.png';
         }
         $tabs['routines']['link'] = 'db_routines.php';
         $tabs['routines']['text'] = __('Routines');
         $tabs['routines']['icon'] = 'b_routines.png';
         if (Util::currentUserHasPrivilege('EVENT', $this->_db)) {
             $tabs['events']['link'] = 'db_events.php';
             $tabs['events']['text'] = __('Events');
             $tabs['events']['icon'] = 'b_events.png';
         }
         if (Util::currentUserHasPrivilege('TRIGGER', $this->_db)) {
             $tabs['triggers']['link'] = 'db_triggers.php';
             $tabs['triggers']['text'] = __('Triggers');
             $tabs['triggers']['icon'] = 'b_triggers.png';
         }
     }
     if (Tracker::isActive() && !$db_is_system_schema) {
         $tabs['tracking']['text'] = __('Tracking');
         $tabs['tracking']['icon'] = 'eye.png';
         $tabs['tracking']['link'] = 'db_tracking.php';
     }
     if (!$db_is_system_schema) {
         $tabs['designer']['text'] = __('Designer');
         $tabs['designer']['icon'] = 'b_relations.png';
         $tabs['designer']['link'] = 'db_designer.php';
         $tabs['designer']['id'] = 'designer_tab';
     }
     if (!$db_is_system_schema && $cfgRelation['centralcolumnswork']) {
         $tabs['central_columns']['text'] = __('Central columns');
         $tabs['central_columns']['icon'] = 'centralColumns.png';
         $tabs['central_columns']['link'] = 'db_central_columns.php';
     }
     return $tabs;
 }
Util