Oracle存储过程中authid current_user和authid definer

栏目: 数据库 · Oracle · 发布时间: 7年前

内容简介:Oracle存储过程中authid current_user和authid definer

以下举例说明, authid current_user authid definer 的一些差别。

DB Version :11.2.0.4

举例 1

用户 system:

用户 system 新建一个存储过程 p_test ,这里 authid current_user

create or replace procedure p_test

authid current_user

as

v_count number;

begin

select count(*) into v_count from dba_objects;

dbms_output.put_line(v_count);

end;

用户 system 授权 scott 有执行这个存储过程 system.p_test 的权限。

grant execute on system.p_test to scott;

用户 scott:

用户 scott 执行存储过程 system.p_test

begin

system.p_test;

end;

报错,表或视图不存在,因为没权限 select 视图 dba_objects

ORA-00942: table or view does not exist

ORA-06512: at "SYSTEM.P_TEST", line 6

ORA-06512: at line 2

用户 system:

用户 system 新建一个存储过程 p_test ,这里不指定 authid ,即默认的 authid definer

create or replace procedure p_test

as

v_count number;

begin

select count(*) into v_count from dba_objects;

dbms_output.put_line(v_count);

end;

用户 scott:

用户 scott 执行存储过程 system.p_test

begin

system.p_test;

end;

执行成功。 authid definer 是使用定义者的权限去运行的, system select dba_objects 的权限,所执行成功。

举例 2

用户 system:

用户 system 新建一个存储过程 p_test2 ,这里不指定 authid ,即默认的 authid definer

create or replace procedure p_test2

as

begin

execute immediate 'create table scott.tb_01 as select * from dual';

end;

用户 system 执行存储过程 system.p_test

begin

system.p_test2;

end;

报错,无权限。虽然 system 有执行 create table scott.tb_01 的权限,但这个权限是角色 DBA 中的权限,在 authid definer 的存储过程是 disabled [All roles are disabled in any named PL/SQL block (stored procedure, function, or trigger) that executes with definer's rights.]

ORA-01031: insufficient privileges

ORA-06512: at "SYSTEM.P_TEST2", line 5

ORA-06512: at line 2

用户 system 新建一个存储过程 p_test2 ,这里 authid current_user

create or replace procedure p_test2

authid current_user

as

begin

execute immediate 'create table scott.tb_01 as select * from dual';

end;

用户 system 执行存储过程 system.p_test

begin

system.p_test2;

end;

执行成功。 authid current_user 时,使用存储过程的会话的角色 DBA 中的权限是 enabled ,所以执行成功。这一点可以通过在存储过程中查看 session_roles 视图来验证。

执行成功。

举例 3

用户 system:

用户 system 授权 DBA 角色给 scott 。这里注意, scoot 只有授权之后的新会话才具有 DBA 角色。

grant dba to scott;

用户 scott:

用户 scott 新建一个存储过程 p_test

create or replace procedure p_test

as

v_count number;

begin

select count(*) into v_count from dba_objects;

dbms_output.put_line(v_count);

end;

编译时报错,表或视图不存在。因为 scott 虽然具有 DBA 的角色,但在编译时,角色是 disabled 。这里不论 authid current_user 还是 authid definer ,因为 authid 属性是作用于运行时,而不是编译时。

Compilation errors for PROCEDURE SCOTT.P_TEST

Error: PL/SQL: ORA-00942: table or view does not exist

Line: 6

Text: select count(*) into v_count from dba_objects;

用户 system:

-- 用户 system 授权 dba_objects 视图的 select 权限给 scott

grant select on dba_objects to scott;

用户 scott:

用户 scott 新建一个存储过程 p_test

create or replace procedure p_test

as

v_count number;

begin

select count(*) into v_count from dba_objects;

dbms_output.put_line(v_count);

end;

编译成功。因为这时 scott 具有对视图 dba_objects select 权限,而且这个权限不是来自于角色。

总结:

1.authid current_user 使用的是调用者的权限去运行, authid definer 使用定义者的权限去运行。

2. 角色在 authid definer 的存储过程中是 disabled

3. 存储过程编译时角色也是 disabled ,并且不验证动态 SQL 的权限。

4. 以上也适用于函数、触发器,即命名的 PL/SQL 块。

更多Oracle相关信息见 Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

本文永久更新链接地址 http://www.linuxidc.com/Linux/2017-05/144173.htm


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Linux/UNIX系统编程手册

Linux/UNIX系统编程手册

Michael Kerrisk / 孙剑 许从年 董健、孙余强 郭光伟 陈舸 / 人民邮电出版社 / 2014-1 / 158

《linux/unix系统编程手册(上、下册)》是介绍linux与unix编程接口的权威著作。linux编程资深专家michael kerrisk在书中详细描述了linux/unix系统编程所涉及的系统调用和库函数,并辅之以全面而清晰的代码示例。《linux/unix系统编程手册(上、下册)》涵盖了逾500个系统调用及库函数,并给出逾200个程序示例,另含88张表格和115幅示意图。 《li......一起来看看 《Linux/UNIX系统编程手册》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换