授予使用函数的视图的选择权

发布于 2024-11-18 08:32:47 字数 793 浏览 3 评论 0原文

我正在使用 postgresql 8.4,当视图使用任何数据库函数时,从不同用户拥有的数据库中授予视图的选择权限时遇到一些问题。

作为新用户,当我尝试运行时,例如 select * from users_pwd; 其中 users_pwd 定义为:

create view users_pwd as
select *, get_pwd(id)
from users;

和 get_pwd 为:

CREATE OR REPLACE FUNCTION get_pwd(p_id integer)
RETURNS text AS
$BODY$
declare u record;
BEGIN
select into u * from users where id = p_id;
return u.password;
END;
$BODY$
LANGUAGE plpgsql;

我收到以下错误:

ERROR:  permission denied for relation users
CONTEXT:  SQL statement "select * from users where id =  $1 "
PL/pgSQL function "get_pwd" line 3 at SQL statement

让用户查询的唯一方法view 的目的是显式授予对表用户的选择,这是我不想做的。

如果视图不使用任何函数,而只是新用户没有显式访问权限的其他表,则它可以正常工作。

I'm using postgresql 8.4 and am having a bit of a problem granting select privileges on a view from a database onwed by a different user when the view uses any of the databases functions.

As the new user, when I try to run, for example select * from users_pwd; where users_pwd is defined as:

create view users_pwd as
select *, get_pwd(id)
from users;

and get_pwd as:

CREATE OR REPLACE FUNCTION get_pwd(p_id integer)
RETURNS text AS
$BODY$
declare u record;
BEGIN
select into u * from users where id = p_id;
return u.password;
END;
$BODY$
LANGUAGE plpgsql;

I get the following error:

ERROR:  permission denied for relation users
CONTEXT:  SQL statement "select * from users where id =  $1 "
PL/pgSQL function "get_pwd" line 3 at SQL statement

The only way to have the user query the view is to explicitly grant select on the table users which I don't want to do.

If a view doesn't use any function, but rather just other tables which the new user doesn't have explicit access to it works perfectly fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清秋悲枫 2024-11-25 08:32:47

您可以创建具有可以从表用户中选择的所有者的函数。这样的函数应该使用 SECURITY DEFINER 子句创建,因此它将以所有者权限执行。

您可以在这里找到更多信息: http://www.postgresql.org/ docs/9.0/interactive/sql-createfunction.html

您还可以授予函数的 EXECUTE 权限。请参阅文档中的 GRANT

You could create the function with owner who can select from the table users. Such a function should be created with SECURITY DEFINER clause, so it will be executed with the owner rights.

More information you can find here: http://www.postgresql.org/docs/9.0/interactive/sql-createfunction.html

You can also GRANT EXECUTE privileges on functions. See GRANT in the docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文