关于oracle存储过程Definer(定义者权限 )与Invoker(调用者权限)的实验

试验:
--sys用户
drop user test1 ;
drop user test2 ;

create user test1 identified by test;
grant connect to test1;
grant create table to test1;


create user test2 identified by test;
grant connect to test2;
grant create table to test2;


conn test1/test
create table departments(deptno number, dname varchar2(10),mgr number,loc number);

create or replace procedure create_dept(v_deptno NUMBER,
      v_name VARCHAR2,
      v_mgr NUMBER,
      v_loc NUMBER
) is
begin
  INSERT INTO departments
VALUES (v_deptno,v_name,v_mgr,v_loc);

end create_dept;

以上test1用户无法创建
--sys执行如下操作
grant create any procedure to test1; 
以上存储过程test1用户可以创建,但无法执行如下:

 
declare 
begin
  create_dept(1,'a',1,1);
end;


--sys执行如下操作
alter user test1 quota 10M on USERS; 
则上面的可以执行

--test2用户执行
declare 
begin
    test1.create_dept(2,'a',2,2);
end;

无法执行以上


grant execute any procedure to test2;

则test2用户可以执行,数据插入到test1.departments中。


事实上,存储过程包括2种,DR(Definer's Rights 定义者权限 ) Procedure和IR(Invoker's Rights 调用者权限) Procedure。
前文默认为定义者权限存储过程。
create or replace procedure create_dept(v_deptno NUMBER,
      v_name VARCHAR2,
      v_mgr NUMBER,
      v_loc NUMBER
) is
……
完全等价于
create or replace procedure create_dept(v_deptno NUMBER,
      v_name VARCHAR2,
      v_mgr NUMBER,
      v_loc NUMBER
)  AUTHID DEFINER is


如果通过test1用户将存储过程修改为:
create or replace procedure create_dept(v_deptno NUMBER,
      v_name VARCHAR2,
      v_mgr NUMBER,
      v_loc NUMBER
) AUTHID CURRENT_USER is
begin
  INSERT INTO departments
VALUES (v_deptno,v_name,v_mgr,v_loc);

end create_dept;

则此时,存储过程变成了调用者模式后,test1用户可以执行此存储过程,而test2无法执行(错误提醒:表和视图不存在)。

--sys用户
alter user test2 quota 10M on USERS; 

--test2用户
create table departments(deptno number, dname varchar2(10),mgr number,loc number);

此后test2用户再执行
declare 
begin
    test1.create_dept(2,'a',2,2);
end;

结果提醒可以执行成功,数据插入到test2.departments中。

再次通过test1对存储过程进行修改为定义者权限:

create or replace procedure create_dept(v_deptno NUMBER,
      v_name VARCHAR2,
      v_mgr NUMBER,
      v_loc NUMBER
) AUTHID DEFINER is
begin
  INSERT INTO departments
VALUES (v_deptno,v_name,v_mgr,v_loc);

end create_dept;

再次通过test2执行如下:
declare 
begin
    test1.create_dept(3,'c',3,3);
end;

数据插入到test1.departments中。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值