그래서 여러 테이블이 있고 각기 다른 사용자가 테이블의 일부에 액세스 할 수 있도록 다른 역할을 만들었습니다.오라클의 절차 - 모든 행을 반환
모든 것을 보려고하는 조정자와 함께 SELECT * FROM yaser.enrol;
을 시도 할 때마다 은 employee_no
을 쿼리하여 직원 역할을 확인하는 라인을 가리 킵니다.
Theres 총 4 가지 유형의 사용자 : 학생, 교사, 강사, 코디네이터.
편집 ** 모든 코드가 추가되었습니다. 다른 모든 테이블이 기본적으로 동일
CREATE TABLE employee
(
emp_no varchar(10),
name varchar(100)
);
-VARCHARS
어떤 도움 : - 모든 직원
CREATE TABLE course
(
course_code varchar(10),
course_title varchar(50),
course_coord_emp_no varchar(10),
primary key (course_code)
);
을 그리고 :
-- Create policy function to be called when ‘ENROL’ table is accessed under user Yaser.
create or replace function f_policy_enrol (schema in varchar2, tab in varchar2)
return varchar2
as
v_emp_no varchar2(10);
v_student_no varchar2(10);
v_tutor_emp_no varchar2(10);
v_lecturer_emp_no varchar2(10);
v_coord_emp_no varchar2(10);
is_tutor number:=0;
is_lecturer number:=0;
is_coordinator number:=0;
is_student number:=0;
is_employee number:=0;
v_program_code varchar2(10);
v_user varchar2(100);
out_string varchar2(400) default '1=2 ';
-- The return value will be out_string. '1=2' means 'Nothing to access'.
begin
-- get session user
v_user := lower(sys_context('userenv','session_user'));
-- Is the user a student?
begin
SELECT student_no INTO v_student_no FROM student WHERE lower(student_no) = v_user;
is_student:=1;
exception
when no_data_found then
v_student_no := 0;
end;
-- Is the user an employee?
begin
SELECT emp_no INTO v_emp_no FROM employee WHERE lower(emp_no) = v_user;
is_employee:=1;
exception
when no_data_found then
v_emp_no := 0;
end;
-- Query the employee number to determine role.
-- If Tutor.
SELECT MAX(tutor_emp_no) INTO v_tutor_emp_no FROM tutorial WHERE lower(tutor_emp_no) = v_user;
-- If Lecturer.
SELECT MAX(course_coord_emp_no) INTO v_lecturer_emp_no FROM course WHERE lower(course_coord_emp_no) = v_user;
-- If Coordinator.
SELECT MAX(prog_coord_emp_no) into v_coord_emp_no FROM program WHERE lower(prog_coord_emp_no) = v_user;
-- Get role of the employee if the user is an employee.
if v_emp_no != 0 and v_tutor_emp_no is NOT NULL then
-- Employee is a Tutor.
is_tutor := 1;
elsif v_emp_no != 0 and v_lecturer_emp_no is NOT NULL then
-- Employee is Lecturer.
is_lecturer := 1;
elsif v_emp_no != 0 and v_coord_emp_no is NOT NULL then
-- Employee is Coordinator.
is_coordinator := 1;
end if;
-- Create the string to be used as the WHERE clause.
if is_student = 1 then
-- Students are allowed to see their orders only.
out_string := out_string||'or student_no = '''||v_student_no||''' ';
end if;
if is_tutor = 1 then
-- Tutors are allowed to see enrolments of students that they tutor.
---- out_string := out_string||'or student_no in (select student_no from tutorial where tutor_emp_no = '||v_tutor_emp_no||') ';
---- NOT WORKING.
out_string := out_string||'or student_no in (select student_no from tutorial where lower(tutor_emp_no) = v_tutor_emp_no) ';
end if;
if is_coordinator = 1 then
-- The coordinator is allowed to see all records in ENROL (WHERE 1=1 or anything) means all rows.
out_string := out_string||'or 1=1 ';
end if;
return out_string;
end;
/
이는 내가 참조하고있어 테이블은 좋은 것입니다! :) Yaser
전체 절차 및 입력 및 오류를 표시하십시오. – kevinsky
'오류입니다'는 올바른 오류 메시지가 아닙니다.코드를 보여주십시오. 아마도 당신은'out_string'을 동적 SQL로 실행하고, 아마도 참조 커서를 여는 것일까? 작은 비트를 바탕으로 내 생각 엔 'or'또는 '1 = 1''앞에 공간이 필요하다는 것을 알았습니다. 또는 괄호 문제가 있음을 나타냅니다. 그러나 괜찮은 대답을 줄 수있는 방법은 여기에 있습니다. –
@AlexPoole, 죄송합니다. 처음 게시물을 편집하십시오! –