CREATE OR REPLACE TRIGGER TRG_LOGON_RESTRICT
AFTER LOGON
ON DATABASE
DISABLE
DECLARE
SQL_STATEMENT VARCHAR2 (500);
V_EXIST NUMBER (19);
TYPE LOGON_RECORD_TYPE --定义记录类型
IS
RECORD (
ID TAB_AUDIT_LOGON_RECORD.id%TYPE,
LOGON_TIME TAB_AUDIT_LOGON_RECORD.logon_time%TYPE,
SESSION_ID TAB_AUDIT_LOGON_RECORD.session_id%TYPE,
OS_USER TAB_AUDIT_LOGON_RECORD.os_user%TYPE,
IP_ADDRESS TAB_AUDIT_LOGON_RECORD.ip_address%TYPE,
TERMINAL TAB_AUDIT_LOGON_RECORD.terminal%TYPE,
HOST_NAME TAB_AUDIT_LOGON_RECORD.host_name%TYPE,
USER_NAME TAB_AUDIT_LOGON_RECORD.user_name%TYPE
);
LOGON_RECORD LOGON_RECORD_TYPE;
BEGIN
SQL_STATEMENT := '0';
V_EXIST := 0;
SELECT SEQ_LOGON.NEXTVAL id,
SYSDATE,
SYS_CONTEXT ('USERENV', 'SESSIONID'),
SYS_CONTEXT ('USERENV', 'OS_USER'),
SYS_CONTEXT ('USERENV', 'IP_ADDRESS'),
SYS_CONTEXT ('USERENV', 'TERMINAL'),
SYS_CONTEXT ('USERENV', 'HOST'),
ORA_LOGIN_USER
INTO LOGON_RECORD
FROM DUAL;
INSERT INTO TAB_AUDIT_LOGON_RECORD
VALUES LOGON_RECORD;
SELECT COUNT ( * )
INTO V_EXIST
FROM TAB_AUTHORIZATION_LOGON_USER
WHERE ip_address = LOGON_RECORD.ip_address
AND terminal = LOGON_RECORD.terminal
AND valid = 1;
IF V_EXIST IS NULL
THEN
V_EXIST := 0;
END IF;
IF V_EXIST > 0
THEN
DBMS_OUTPUT.put_line ('Authorized user, please be careful operation!');
ELSE
SP_KILL_SESSION (LOGON_RECORD.HOST_NAME);
raise_application_error (-20001,
'Restricted LOGON! Please contact DBA');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
DBMS_OUTPUT.put_line ('Login session does not exist');
WHEN OTHERS
THEN
RAISE_APPLICATION_ERROR (-20002,
'Restricted LOGON! Please contact DBA!');
END;
/
CREATE OR REPLACE PROCEDURE SYS.SP_KILL_SESSION (P_HOST_NAME IN VARCHAR)
AS
SQL_STATEMENT VARCHAR2 (500);
BEGIN
SQL_STATEMENT := '0';
--定义游标