Guest User

Untitled

a guest
May 2nd, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 2.43 KB | None | 0 0
  1. -- drop cascade type section_list_type;
  2.  
  3. /**
  4.  * Section structure
  5.  */
  6. CREATE TYPE section_list_type AS (
  7.     id INTEGER,
  8.     parent_id INTEGER,
  9.     name character varying(1024),
  10.     owner_id INTEGER,
  11.     GROUP_ID INTEGER,
  12.     created TIMESTAMP without TIME ZONE,
  13.     changed TIMESTAMP without TIME ZONE,
  14.     group_permisssion SMALLINT,
  15.     other_permissions SMALLINT,
  16.     is_section BOOLEAN
  17. );
  18.  
  19. /**
  20.  * Select all sections in selected section
  21.  */
  22. CREATE OR REPLACE FUNCTION select_sections_in_section(
  23.     IN INTEGER
  24. ) returns setof section_list_type AS $$
  25.     SELECT id, parent_id, GROUP_ID, name, owner_id, created, changed, group_permissions, other_permissions, TRUE FROM sections WHERE parent_id = $1;
  26. $$ language SQL;
  27.  
  28. /**
  29.  * Select all positions int selected section
  30.  */
  31. CREATE OR REPLACE FUNCTION select_positions_in_section(
  32.     IN INTEGER
  33. ) returns setof section_list_type AS $$
  34.     SELECT id, parent_id, GROUP_ID, name, owner_id, created, changed, group_permissions, other_permissions, TRUE FROM positions WHERE parent_id = $1;
  35. $$ language SQL;
  36.  
  37. /**
  38.  * Select all allowed positions & sections with permissions checking
  39.  */
  40. CREATE OR REPLACE FUNCTION select_section_content(
  41.     IN INTEGER, -- section id
  42.     IN INTEGER, -- current user id
  43.     IN INTEGER  -- current group id
  44. ) returns setof section_list_type AS $$
  45. DECLARE
  46.     rec section_list_type%ROWTYPE;
  47.     current_section RECORD;
  48. BEGIN
  49.     -- check current section permissions
  50.     SELECT INTO current_section id, parent_id, GROUP_ID, name, owner_id, created, changed, group_permissions, other_permissions FROM sections WHERE id = $1;
  51.     IF ((current_section.owner_id == $2) OR (current_section.GROUP_ID == $3 AND current_section.group_permissions > 0) OR (current_section.other_permissions > 0)) THEN
  52.         -- sections
  53.         FOR rec IN SELECT * FROM select_sections_in_section($1) LOOP
  54.             RETURN next rec;
  55.         END LOOP;
  56.        
  57.         -- positions
  58.         FOR rec IN SELECT * FROM select_positions_in_section($1) LOOP
  59.             IF ((rec.owner_id == $2) OR (rec.GROUP_ID == $3 AND rec.group_permissions > 0) OR (rec.other_permissions > 0)) THEN -- check permissions
  60.                 RETURN next rec;
  61.             END IF;
  62.         END LOOP;
  63.     END IF;
  64.     RETURN;
  65. END;
  66. $$ language plpgsql;
  67.  
  68.  
  69. SELECT * FROM select_section_content(0);
  70. -- select id, parent_id, name, owner_id, created, changed, group_permissions, other_permissions, true from sections;
Advertisement
Add Comment
Please, Sign In to add comment