ORA-28113:策略谓词有错误

时间:2015-11-16 17:16:14

标签: database oracle oracle11g vpd

我需要Oracle的VPD功能方面的一些帮助。我以前从未使用它,但在网上做了一些关于它的研究,但是我遇到了一个问题。

以下是我采取的步骤:

QuanTriDL:

create table NhanVien2

table NhanVien2

QuanTriVPD:

CREATE OR REPLACE CONTEXT ThongTinTaiKhoan USING TTTK_PKG;

CREATE OR REPLACE PACKAGE TTTK_PKG IS
PROCEDURE GetTTTK;
END;
/

CREATE OR REPLACE PACKAGE BODY TTTK_PKG IS
PROCEDURE GetTTTK AS
TaiKhoan varchar(30);
tenPhong varchar(30);
tenChucVu varchar(30);
tenMaNV varchar(10);
BEGIN  
    TaiKhoan := LOWER(SYS_CONTEXT('USERENV','SESSION_USER'));

    DBMS_SESSION.set_context('ThongTinTaiKhoan','GetTaiKhoan',TaiKhoan); 
    if (TaiKhoan = 'nv001') then
        DBMS_SESSION.set_context('ThongTinTaiKhoan','GetChucVu','Giam doc');        
    else    
    if (TaiKhoan = 'nv002') then
        DBMS_SESSION.set_context('ThongTinTaiKhoan','GetChucVu','Truong phong');
        DBMS_SESSION.set_context('ThongTinTaiKhoan','GetPhong','Kinh doanh');       
    else    
    if (TaiKhoan = 'nv006') then
        DBMS_SESSION.set_context('ThongTinTaiKhoan','GetChucVu','Truong phong');
        DBMS_SESSION.set_context('ThongTinTaiKhoan','GetPhong','Ky thuat');                  
    else
        DBMS_SESSION.set_context('ThongTinTaiKhoan','GetChucVu','Nhan vien');
    end if;
    end if;
    end if;
    EXCEPTION
    WHEN NO_DATA_FOUND THEN NULL;
    END GetTTTK;
END;
/

CREATE OR REPLACE TRIGGER RangBuocTTTK AFTER LOGON ON DATABASE  
    BEGIN QuanTriVPD.TTTK_PKG.GetTTTK; 
        EXCEPTION WHEN NO_DATA_FOUND 
        THEN NULL; 
    END;
/

然后:

CREATE OR REPLACE FUNCTION Select_Nhanvien(
schema_p   IN VARCHAR2,
table_p    IN VARCHAR2)
RETURN VARCHAR2
AS
getChucVu varchar(50);
trave varchar2(1000);
BEGIN
    SELECT SYS_CONTEXT('ThongTinTaiKhoan','GetChucVu') into getChucVu FROM DUAL;
    trave := '1=2';
    if (getChucVu = 'Giam doc')  then
        trave := NULL;
    else
    if (getChucVu = 'Truong phong') then
        trave :='Phong=(SELECT SYS_CONTEXT(''ThongTinTaiKhoan'',''GetPhong'') FROM DUAL)';
    else
        trave :='TenTaiKhoan=(SELECT SYS_CONTEXT(''ThongTinTaiKhoan'',''GetTaiKhoan'') FROM DUAL)';
    end if;
    end if;
    RETURN trave;
END;
/

BEGIN
    DBMS_RLS.ADD_POLICY (
    object_schema    => 'QuanTriDL',            
    object_name      => 'NhanVien2',            
    policy_name      => 'VPD_Select_Nhanvien',      
    function_schema  => 'QuanTriVPD',           
    policy_function  => 'Select_Nhanvien',      
    statement_types  => 'SELECT');
END;
/

当连接为nv001,nv002,nv006时,没关系。但是连接另一个用户:

  

ORA-28113:策略谓词有错误

为什么会导致错误?

1 个答案:

答案 0 :(得分:1)

(一年前的问题但是因为我偶然发现了它,我会继续为其他人解答...)

ORA-28113只是意味着当你的策略函数返回一个where子句时,生成的SQL有一些错误。您可以通过查看跟踪文件来获取详细信息。另外,试试:

select Select_Nhanvien('myschema','mytable') from dual;

然后将结果附加到WHERE子句,如下所示:

SELECT * FROM MYTABLE WHERE <results from above>;

然后你应该看到根本原因。我猜测其他用户&#39;以上的情况。没有构建where子句所需的sys_context变量,也没有登录触发器的访问权限。

作为旁注,当你的策略函数引用它自己的表时,你可以遇到的另一个问题是循环引用 - 理想情况下我希望策略函数在策略函数中绕过它自己,这样你就可以不存在,但是它似乎不会那样工作。