summaryrefslogtreecommitdiff
path: root/db/rest/comment/api_comment_delete.sql
diff options
context:
space:
mode:
Diffstat (limited to 'db/rest/comment/api_comment_delete.sql')
-rw-r--r--db/rest/comment/api_comment_delete.sql31
1 files changed, 31 insertions, 0 deletions
diff --git a/db/rest/comment/api_comment_delete.sql b/db/rest/comment/api_comment_delete.sql
new file mode 100644
index 0000000..d7db8a4
--- /dev/null
+++ b/db/rest/comment/api_comment_delete.sql
@@ -0,0 +1,31 @@
+CREATE FUNCTION _api.comment_delete()
+RETURNS TRIGGER
+LANGUAGE plpgsql VOLATILE
+AS $BODY$
+DECLARE
+ _user_id INTEGER;
+BEGIN
+ _user_id = _api.get_user_id();
+
+ IF OLD.user_id <> _user_id THEN
+ PERFORM _api.raise_deny();
+ END IF;
+
+ DELETE FROM admin.comment
+ WHERE user_id = _user_id
+ AND id = OLD.id;
+END
+$BODY$;
+
+GRANT EXECUTE ON FUNCTION _api.comment_delete()
+ TO rest_user;
+GRANT DELETE ON TABLE api.comment
+ TO rest_user;
+GRANT DELETE ON TABLE admin.comment
+ TO rest_user;
+
+CREATE TRIGGER api_comment_delete_trgr
+ INSTEAD OF DELETE
+ ON api.comment
+ FOR EACH ROW
+ EXECUTE PROCEDURE _api.comment_delete();