77 * Portions Copyright (c) 1994, Regents of the University of California
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.126 2002/08/17 12:15:48 momjian Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/commands/trigger.c,v 1.127 2002/08/18 11:20:05 petere Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -86,6 +86,11 @@ CreateTrigger(CreateTrigStmt *stmt, bool forConstraint)
8686
8787rel = heap_openrv (stmt -> relation ,AccessExclusiveLock );
8888
89+ if (stmt -> constrrel != NULL )
90+ constrrelid = RangeVarGetRelid (stmt -> constrrel , false);
91+ else
92+ constrrelid = InvalidOid ;
93+
8994if (rel -> rd_rel -> relkind != RELKIND_RELATION )
9095elog (ERROR ,"CreateTrigger: relation \"%s\" is not a table" ,
9196stmt -> relation -> relname );
@@ -94,10 +99,29 @@ CreateTrigger(CreateTrigStmt *stmt, bool forConstraint)
9499elog (ERROR ,"CreateTrigger: can't create trigger for system relation %s" ,
95100stmt -> relation -> relname );
96101
97- aclresult = pg_class_aclcheck (RelationGetRelid (rel ),GetUserId (),
98- stmt -> isconstraint ?ACL_REFERENCES :ACL_TRIGGER );
99- if (aclresult != ACLCHECK_OK )
100- aclcheck_error (aclresult ,RelationGetRelationName (rel ));
102+ /* permission checks */
103+
104+ if (stmt -> isconstraint )
105+ {
106+ /* foreign key constraint trigger */
107+
108+ aclresult = pg_class_aclcheck (RelationGetRelid (rel ),GetUserId (),ACL_REFERENCES );
109+ if (aclresult != ACLCHECK_OK )
110+ aclcheck_error (aclresult ,RelationGetRelationName (rel ));
111+ if (constrrelid != InvalidOid )
112+ {
113+ aclresult = pg_class_aclcheck (constrrelid ,GetUserId (),ACL_REFERENCES );
114+ if (aclresult != ACLCHECK_OK )
115+ aclcheck_error (aclresult ,get_rel_name (constrrelid ));
116+ }
117+ }
118+ else
119+ {
120+ /* real trigger */
121+ aclresult = pg_class_aclcheck (RelationGetRelid (rel ),GetUserId (),ACL_TRIGGER );
122+ if (aclresult != ACLCHECK_OK )
123+ aclcheck_error (aclresult ,RelationGetRelationName (rel ));
124+ }
101125
102126/*
103127 * Generate the trigger's OID now, so that we can use it in the name
@@ -124,11 +148,6 @@ CreateTrigger(CreateTrigStmt *stmt, bool forConstraint)
124148constrname = "" ;
125149}
126150
127- if (stmt -> constrrel != NULL )
128- constrrelid = RangeVarGetRelid (stmt -> constrrel , false);
129- else
130- constrrelid = InvalidOid ;
131-
132151TRIGGER_CLEAR_TYPE (tgtype );
133152if (stmt -> before )
134153TRIGGER_SETT_BEFORE (tgtype );