how to auto GRANT rights on a new table?

From: Maciej Sieczka <tutey(at)o2(dot)pl>
To: pgsql-novice(at)postgresql(dot)org
Subject: how to auto GRANT rights on a new table?
Date: 2008-02-22 21:46:18
Message-ID: 47BF42AA.8010607@o2.pl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hello,

Greetings Everyone! This is my first post to your list.

I have a novice problem as follows:

There are 2 role groups in my cluster: "editors" and "viewers". In each
group there are several users.

In the DB, members of "editors" are allowed to create new tables in one
schema. My problem is that only the very user who created the table is
allowed to edit it.

I need to modify this PostgreSQL's default behaviour, so that the ACL on
a new table in this schema is set to arwdx for the group "editors", and
to r for "viewers", without having to manually GRANT rights each time a
new table is created. I don't know how.

From reading so far I suppose I should create a function which calls
an appropriate GRANT, and trigger it when a new record is added to
"pg_tables". Is that a way to go? Is there an easier one?

If the function+trgger approach is optimal, can Anybody show me how they
should look and how to deploy them? I have a muddy idea they would
something like this pseudo-code:

CREATE TRIGGER trig_acl
AFTER INSERT
ON pg_catalog.pg_tables WHERE schemaname='dss_postgis' FOR EACH ROW
EXECUTE PROCEDURE fnct_acl();

CREATE FUNCTION fnct_acl () RETURNS opaque AS '
DECLARE
table_name TEXT #how do I fetch the table name???
BEGIN
GRANT ALL ON myschema.table_name TO editors;
END;
' LANGUAGE 'plpgsql';

What should I modify to make it real code?

An extra, but desired functionality, would be if I could also
prevent other "editors" from modifying the table *if* it is being
currently being edited by some user. Is that feasible at all?

Thank you for any pointers!

Regards,
Maciek

P.S.
I searched the archives and only found this [1] thread related, but it
does not provide an obvious (for me) solution.

[1]http://www.nabble.com/grant-select-on-all-tables-to1029307.html#a1029307

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Maciej Sieczka 2008-02-22 21:48:19 Re: how to auto GRANT rights on a new table?
Previous Message Terry Lee Tucker 2008-02-22 14:17:47 Re: Trigger question