Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 7F9C6B5DBCA for ; Mon, 24 Oct 2011 23:55:07 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 08893-02 for ; Tue, 25 Oct 2011 02:55:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mail-yw0-f46.google.com (mail-yw0-f46.google.com [209.85.213.46]) by mail.postgresql.org (Postfix) with ESMTP id 6B20DB5D80A for ; Mon, 24 Oct 2011 23:55:01 -0300 (ADT) Received: by ywm3 with SMTP id 3so47302ywm.19 for ; Mon, 24 Oct 2011 19:55:02 -0700 (PDT) Received: by 10.236.73.228 with SMTP id v64mr37652760yhd.103.1319511302377; Mon, 24 Oct 2011 19:55:02 -0700 (PDT) Received: from ?IPv6:2001:44b8:6109:9f00:2a0:c9ff:fe6e:ed8f? (2001-44b8-6109-9f00-02a0-c9ff-fe6e-ed8f.static.ipv6.internode.on.net. [2001:44b8:6109:9f00:2a0:c9ff:fe6e:ed8f]) by mx.google.com with ESMTPS id 15sm68875351anz.16.2011.10.24.19.54.59 (version=SSLv3 cipher=OTHER); Mon, 24 Oct 2011 19:55:01 -0700 (PDT) Message-ID: <4EA62500.9070000@ringerc.id.au> Date: Tue, 25 Oct 2011 10:54:56 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110307 Thunderbird/3.1.9 MIME-Version: 1.0 To: =?ISO-8859-1?Q?Brice_Andr=E9?= CC: pgsql-sql@postgresql.org Subject: Re: Handling mutliple clients access with views References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201110/66 X-Sequence-Number: 35950 On 25/10/11 03:23, Brice André wrote: > Hello everyone, > > I am developping a web service where some tables are handling the data > of different clients. Depending on configured rights, one client can > have no access, or read access, or read and write access to other > clients data. > > In order to handle that, I am using views and, to ensure that a client > cannot access data outside the view, all clients info is stored in a > table where its postgresql user name is also stored. So, in order to > limit access of view, I am using the postgresql special function > "current_user()" and I am retrieving the id of my client like this. That sounds ... roundabout. Why not use roles and role inheritance? You can use SET ROLE to temporarily change roles, log in as different roles, have one role be a member of other roles, have role access permissions on tables/views at the column or table level, etc. > - My method requests that each user has its own postgresql user. But, > in this case, my web server needs to establish a postgresql connection > for each user, which will maybe cause problems. Connect as a single user, then SET ROLE to the user you want in order to control access. Instead of using current_user() and programmatic security checking, use GRANT and REVOKE for declarative access checking where possible. > So, I was guessing if I was not completely wrong by doing like that. > Maybe is there a simpler way of doing what I try to do ? Or maybe am I a > little bit too paranoïde, and maybe should I handle all my clients with > a single postgresql user, handling all safety aspect in my php script ? Nope, I heartily approve of doing security in-database, especially if you can do it declaratively. -- Craig Ringer