Re: storing access rights in a postgres database

From: "Dirk Jagdmann" <jagdmann(at)gmail(dot)com>
To: "tv(at)fuzzy(dot)cz" <tv(at)fuzzy(dot)cz>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: storing access rights in a postgres database
Date: 2006-10-10 21:57:26
Message-ID: 5d0f60990610101457w36b0a3f5r9e4110c7194fd14c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello tv,

I think in your case the PostgreSQL array column type really fits
well. I would just add an array of type integer (or whatever your
primary key in your role table is) to your company, project, module,
... tables. Then you can easy check if a role has access to the
project row by checking if the roles primary key is contained in the
role array. And you can easily select over the entire project table
and matching any values in the role array.

Here are some (untested) SQL statements to clarify my suggestion:

create table role (
id serial primary key,
name text );
create table project (
id serial primary key,
name text,
roles int[] );
create table company (
id serial primary key,
name text,
roles int[] );
insert into role values(1,'you');
insert into role values(2,'me');
insert into project values(1,'a',{1,2});
insert into project values(2,'b',{2});
-- check if I can access a project
select id from project where name='a' and 2=ANY(roles); -- 2 is 'my' role id
-- get all companies I have access to
select id,name from company where 2=ANY(roles);

I think the foreign key constraints can not be enforced with the
standard foreign key triggers/functions so you would have to write
your own plpgsql triggers if this is a mandatory requirement.

--
---> Dirk Jagdmann
----> http://cubic.org/~doj
-----> http://llg.cubic.org

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jim C. Nasby 2006-10-11 02:11:51 Re: Temp tables and functions
Previous Message Dirk Jagdmann 2006-10-10 21:15:16 Re: optimal insert