Skip to content

Commit

Permalink
avniproject/avni-webapp#1304 - function to grant permission only on t…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
petmongrels committed Aug 8, 2024
1 parent 9917dd4 commit e9f4e21
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
21 changes: 21 additions & 0 deletions avni-server-api/src/main/resources/db/migration/R__Functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,27 @@ BEGIN
END;
$$;

CREATE OR REPLACE FUNCTION grant_all_on_table(rolename text, tablename text)
RETURNS text AS
$body$
BEGIN
EXECUTE (
SELECT 'GRANT ALL ON TABLE '
|| tablename
|| ' TO ' || quote_ident(rolename)
);

EXECUTE (
SELECT 'GRANT SELECT ON '
|| tablename
|| ' TO ' || quote_ident(rolename)
);

EXECUTE 'GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO ' || quote_ident(rolename) || '';
RETURN 'ALL PERMISSIONS GRANTED TO ' || quote_ident(rolename);
END;
$body$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION grant_all_on_all(rolename text)
RETURNS text AS
$body$
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CREATE OR REPLACE FUNCTION grant_all_on_table(rolename text, tablename text)
RETURNS text AS
$body$
BEGIN
EXECUTE (
SELECT 'GRANT ALL ON TABLE '
|| tablename
|| ' TO ' || quote_ident(rolename)
);

EXECUTE (
SELECT 'GRANT SELECT ON '
|| tablename
|| ' TO ' || quote_ident(rolename)
);

EXECUTE 'GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO ' || quote_ident(rolename) || '';
RETURN 'ALL PERMISSIONS GRANTED TO ' || quote_ident(rolename);
END;
$body$ LANGUAGE plpgsql;

SELECT grant_all_on_table(a.rolname, 'organisation_category') FROM pg_roles a WHERE pg_has_role('openchs', a.oid, 'member');
SELECT grant_all_on_table(a.rolname, 'organisation_status') FROM pg_roles a WHERE pg_has_role('openchs', a.oid, 'member');

0 comments on commit e9f4e21

Please sign in to comment.