pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
CREATE USER in side a TRIGGER FUNCTION
2+ messages / 2 participants
[nested] [flat]

* CREATE USER in side a TRIGGER FUNCTION
@ 2005-04-15 03:30 shaun <savages@mozapps.org>
  2005-04-19 03:57 ` Re: CREATE USER in side a TRIGGER FUNCTION Michael Fuhr <mike@fuhr.org>
  0 siblings, 1 reply; 2+ messages in thread

From: shaun @ 2005-04-15 03:30 UTC (permalink / raw)
  To: pgsql-sql

Hi
I want to put login and user managment into the database for security 
reasons. I have  a employee table. When I add a person to the employee 
table I want to create them in the database and when I remove a person I 
want to drop the person from the database also.  How do you do it and 
what is wrong the the following code?

shaun

here is the add person trigger.
CREATE FUNCTION insuser () RETURNS TRIGGER AS
'BEGIN
        IF NEW.role = ''clerk'' THEN
                EXECUTE CREATE USER NEW.login IN GROUP gp_clerk;
        ELSIF NEW.role = ''medic'' THEN
                EXECUTE CREATE USER NEW.login IN GROUP gp_medic;
        ELSIF NEW.role = ''super'' THEN
                CREATE USER NEW.login IN GROUP gp_super;        
         ELSIF NEW.role = ''admin'' THEN
                CREATE USER NEW.login CREATEUSER IN GROUP gp_admin;        
        ELSIF NEW.role = ''maint'' THEN
                CREATE USER NEW.login CREATEUSER IN GROUP gp_maint;
        END IF;
        RETURN NEW;
END;'
LANGUAGE plpgsql;




^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: CREATE USER in side a TRIGGER FUNCTION
  2005-04-15 03:30 CREATE USER in side a TRIGGER FUNCTION shaun <savages@mozapps.org>
@ 2005-04-19 03:57 ` Michael Fuhr <mike@fuhr.org>
  0 siblings, 0 replies; 2+ messages in thread

From: Michael Fuhr @ 2005-04-19 03:57 UTC (permalink / raw)
  To: shaun <savages@mozapps.org>; +Cc: pgsql-sql

On Thu, Apr 14, 2005 at 08:30:02PM -0700, shaun wrote:
>
> I want to put login and user managment into the database for security 
> reasons. I have  a employee table. When I add a person to the employee 
> table I want to create them in the database and when I remove a person I 
> want to drop the person from the database also.  How do you do it and 
> what is wrong the the following code?
> 
> CREATE FUNCTION insuser () RETURNS TRIGGER AS
> 'BEGIN
>        IF NEW.role = ''clerk'' THEN
>                EXECUTE CREATE USER NEW.login IN GROUP gp_clerk;

EXECUTE needs a string.  Try something like this:

  EXECUTE ''CREATE USER '' || quote_ident(NEW.login) || '' IN GROUP gp_clerk'';

quote_ident() is a safety measure to prevent SQL injection.  For
more information about using EXECUTE, see "Executing Dynamic Commands"
in the PL/pgSQL documentation (if you're using a version of PostgreSQL
earlier than 8.0 then see the documentation for that version):

http://www.postgresql.org/docs/8.0/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-...

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2005-04-19 03:57 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-04-15 03:30 CREATE USER in side a TRIGGER FUNCTION shaun <savages@mozapps.org>
2005-04-19 03:57 ` Michael Fuhr <mike@fuhr.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox