From 944b6b0526032ad8c1b4a2612d6723bec75e0e4c Mon Sep 17 00:00:00 2001
From: Freya Murphy <freya@freyacat.org>
Date: Fri, 29 Mar 2024 22:29:56 -0400
Subject: start database (user and post), and initial barebones home page

---
 db/rest/user/api_user_delete.sql | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 db/rest/user/api_user_delete.sql

(limited to 'db/rest/user/api_user_delete.sql')

diff --git a/db/rest/user/api_user_delete.sql b/db/rest/user/api_user_delete.sql
new file mode 100644
index 0000000..8d7d52f
--- /dev/null
+++ b/db/rest/user/api_user_delete.sql
@@ -0,0 +1,30 @@
+CREATE FUNCTION _api.user_delete()
+RETURNS TRIGGER
+LANGUAGE plpgsql VOLATILE
+AS $BODY$
+DECLARE
+	_user_id INTEGER;
+BEGIN
+	_user_id = _api.get_user_id();
+
+	IF OLD.id <> _user_id THEN
+		PERFORM _api.raise_deny();
+	END IF;
+
+	DELETE FROM admin.user
+	WHERE id = _user_id;
+END
+$BODY$;
+
+GRANT EXECUTE ON FUNCTION _api.user_delete()
+	TO rest_user;
+GRANT DELETE ON TABLE api.user
+	TO rest_user;
+GRANT DELETE ON TABLE admin.user
+	TO rest_user;
+
+CREATE TRIGGER api_user_delete_trgr
+	INSTEAD OF DELETE
+			ON api.user
+			FOR EACH ROW
+				EXECUTE PROCEDURE _api.user_delete();
-- 
cgit v1.2.3-freya

