1 CREATE OR REPLACE FUNCTION _usrprivTrigger() RETURNS TRIGGER AS $$
2 -- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
3 -- See www.xtuple.com/CPAL for the full text of the software license.
8 -- This looks like a candidate for a foreign key but isn't.
9 -- fkeys don't work if the foreign key value resides in a child of the
10 -- table and not the table itself.
11 IF (NOT EXISTS(SELECT usrpriv_id
13 WHERE ((usrpriv_priv_id=priv_id) AND (priv_name ='MaintainUsers')
14 AND (usrpriv_username=geteffectivextuser())))) THEN
15 RAISE EXCEPTION 'User have no privileges to modify user privileges.';
18 IF ((TG_OP = 'UPDATE' OR TG_OP = 'INSERT') AND
19 (NOT EXISTS(SELECT priv_id
21 WHERE (priv_id=NEW.usrpriv_priv_id)))) THEN
22 RAISE EXCEPTION 'Privilege id % does not exist or is part of a disabled package.',
26 ELSIF (TG_OP = 'DELETE') THEN
32 $$ LANGUAGE 'plpgsql';
34 SELECT dropIfExists('trigger', 'usrprivTrigger');
35 CREATE TRIGGER usrprivTrigger BEFORE INSERT OR UPDATE ON usrpriv FOR EACH ROW EXECUTE PROCEDURE _usrprivTrigger();