首页 > 解决方案 > 更改数据库 Informix 上的用户权限

问题描述

在 Informix 中,如何更改数据库、从咨询到选择、删除、更新、插入的所有表的用户权限。

我试着用这样的东西

dbaccess DATABASE exec for i in (select table_name from user_tables) loop execute immediate 'grant select,insert,update,delete on '||i.table_name||' to USER'; end loop

但它没有用。

标签: informix

解决方案


DB-Access 不是我最喜欢的工具,但它可以在 shell 脚本的一些支持下完成这项工作。您最终会调用它两次,一次是生成表名列表,一次是处理权限。

鉴于文件so-6952-3871.sql包含:

unload to '/dev/stdout'
    select tabname
       from "informix".systables
      where tabid >= 100
        and tabtype = 'T';

shell 脚本so-6952-3871.sh完成这项工作,撤销现有权限并授予新权限:

#!/bin/sh
#
# @(#)$Id$
#
# Grant select, insert, delete, update permission on all user tables in a database to a named user.

: "${DBACCESS:=dbaccess}"

if [ $# = 0 ]
then
    echo "$0: you must specify the database and at least one user" >&2
    echo "Usage: $0 database user [...]" >&2
fi
dbase="$1"
shift
if [ $# = 0 ]
then
    echo "$0: must specify at least one user" >&2
    echo "Usage: $0 database user [...]" >&2
    exit 1
fi

$DBACCESS $dbase so-6952-3871.sql |
sed -n '/^\([a-zA-Z0-9_]*\)|$/ s//\1/p' |
while read tabname
do
    #echo "Table: $tabname" >&2
    for user in "$@"
    do
        echo "revoke all on $tabname from $user;"
        echo "grant select, insert, delete, update on $tabname to $user;"
    done
done |
$DBACCESS $dbase -

我选择使用"informix".systables系统目录表,因为我的数据库中没有调用表user_tables。您可以根据需要细化选择标准(例如,省略时间序列表)。

如果您有足够的权限来授予和撤销数据库中的权限(例如,作为 DBA 或作为用户informix),这应该可以正常工作。

我没有使用 DB-Access,而是使用我的 SQLCMD(可从IIUG Software Archive获得),我编写它是为了在 shell 脚本上下文中表现一致,而 DB-Access 则不然。它可以追溯到 1986 年(之前有dbaccess; 在那些日子里,你使用的是 - DB-Access 是在一个晚上isql被雕刻出来的)。isql它与 Microsoft 的同名 johnny-come-lately 程序没有任何关系——除了名称和具有相同的通用目的(操作 SQL 数据库)。使用 SQLCMD,线路无需sed在标准输出上忽略来自 DB-Access 的噪声输出。


推荐阅读