Fix custom pre-request handler

Uses Ruslan's technique
This commit is contained in:
Joe Nelson
2016-09-27 23:17:08 -07:00
parent 06363ccc77
commit 5a166e8e80
7 changed files with 63 additions and 17 deletions
+21 -4
View File
@@ -237,15 +237,32 @@ SELECT jwt.sign(
) r;
$$;
create function block_bad_role() returns void
language plpgsql as $$
CREATE OR REPLACE FUNCTION switch_role() RETURNS void
LANGUAGE plpgsql
AS $$
declare
user_id text;
begin
if current_role = 'bad_role' then
raise invalid_password using message = 'role is not allowed';
user_id = current_setting('postgrest.claims.id')::text;
if user_id = '1'::text then
execute 'set local role postgrest_test_author';
elseif user_id = '2'::text then
execute 'set local role postgrest_test_default_role';
elseif user_id = '3'::text then
RAISE EXCEPTION 'Disabled ID --> %', user_id USING HINT = 'Please contact administrator';
/* else */
/* execute 'set local role postgrest_test_anonymous'; */
end if;
end
$$;
CREATE FUNCTION get_current_user() RETURNS text
LANGUAGE sql
AS $$
SELECT current_user::text;
$$;
--
-- Name: reveal_big_jwt(); Type: FUNCTION; Schema: test; Owner: -
--