Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect and use one column
    primarykey
    data
    text
    <p>Please consider the following (working) stored procedure. This function receives an integer as its first parameter, indicating which <code>privilegeid</code> has to be checked against the current user. Privileges are stored in the table <code>privileges</code> and consist of an id and a name (varchar).</p> <p>Each privilege belongs to one or more <code>roles</code> stored in <code>users_roles</code>. Each user is assigned to one or more roles. This function retrieves all roles assigned to <code>current_user</code> and checks them, like said, against the given priviligeid.</p> <pre><code>CREATE OR REPLACE FUNCTION "public"."has_privilege" (in int4) RETURNS bool AS $BODY$ DECLARE asked_privilegeid ALIAS FOR $1; userid int; role_row users_roles%rowtype; privilege_row privileges%rowtype; BEGIN EXECUTE 'SELECT userid FROM users WHERE username=$1' INTO userid USING current_user; FOR role_row IN SELECT * FROM users_roles WHERE userid=userid LOOP IF role_row.roleid = 1 THEN return TRUE; END IF; FOR privilege_row IN SELECT * FROM privileges WHERE roleid=role_row.roleid LOOP IF privilege_row.privilegeid = asked_privilegeid THEN return TRUE; END IF; END LOOP; END LOOP; return FALSE; END $BODY$ LANGUAGE 'plpgsql' </code></pre> <p>However, this code isn't a efficient as it could be considering it retrieves <em>all</em> rowvalues for <code>users_roles</code> and <code>privileges</code>. I tried to write the procedure as following, but it doesn't seem to work:</p> <pre><code>CREATE OR REPLACE FUNCTION "public"."has_privilege" (in int4) RETURNS bool AS $BODY$ DECLARE asked_privilegeid ALIAS FOR $1; privilegeid int; userid int; roleid int; //role_row users_roles%rowtype; //privilege_row privileges%rowtype; BEGIN EXECUTE 'SELECT userid FROM users WHERE username=$1' INTO userid USING current_user; FOR roleid IN SELECT roleid FROM users_roles WHERE userid=userid LOOP IF roleid = 1 THEN return TRUE; END IF; FOR privilegeid IN SELECT privilegeid FROM privileges WHERE roleid=roleid LOOP IF privilegeid = asked_privilegeid THEN return TRUE; END IF; END LOOP; END LOOP; return FALSE; END $BODY$ LANGUAGE 'plpgsql' </code></pre> <p>What am I doing wrong? Thanks in advance!</p> <p><strong>Edit</strong>: The indentions didn't came through as expected. Here are pastebin links: <a href="http://pastebin.com/w18WaCW0" rel="nofollow">http://pastebin.com/w18WaCW0</a> <a href="http://pastebin.com/W8ewXxEe" rel="nofollow">http://pastebin.com/W8ewXxEe</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload