This repository was archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPREREQUISITES.sql
1522 lines (1294 loc) · 39.5 KB
/
PREREQUISITES.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
SET
search_path TO public;
/********************************************************************************************
Author: frigvid
Create Date: 2024-04-02
Description: Copy everything in this file, and paste it into Supabase's SQL Editor, and run
it. Once this is done, everything should be ready for the project to run.
*********************************************************************************************
SUMMARY OF CHANGES
Date (yyyy-mm-dd) Author Comments
------------------- ------------------- ---------------------------------------------------
2024-04-12 frigvid Moved all SQL code over to an SQL file instead of a
markdown file. I don't think I messed anything up,
but this should make it a little less painful for
users to install. Hopefully didn't mess anything up
either.
2024-04-13 frigvid Added RLS and POLICIES for public.* tables.
2024-04-13T20-50 frigvid Added history-gamedata trigger.
2024-04-13T21-10 frigvid Add Supabase REALTIME support to select tables.
2024-04-14 frigvid Added function to promote user to administrator.
2024-04-15 frigvid Added friend_request_get_one and friend_get_one,
and modified friend_get_all_friends to make it
function better with the realtime implementation.
2024-04-20 frigvid Modified public.docs POLICIES to feature match
public.news' POLICIES. Also added trigger for
updating public.news modified_at time.
2024-04-21 frigvid Modified public.faq POLICIES to feature match
public.news' and public.docs' POLICIES. Also added
triggers for updating modified_at time.
2024-04-26 frigvid Make sure delete_opening lets administrators delete
"default" openings.
********************************************************************************************/
/******************************************
* *
* TABLES *
* *
******************************************/
/* =============================================
* Author: frigvid
* Create date: 2024-04-02
* Description: User profile table.
* ============================================= */
CREATE TABLE IF NOT EXISTS
profiles (
id UUID PRIMARY KEY,
updated_at timestamptz NULL,
display_name TEXT NULL,
elo_rank INT NULL,
avatar_url TEXT NULL,
about_me TEXT NULL,
nationality TEXT NULL DEFAULT 'none',
visibility BOOLEAN DEFAULT FALSE,
visibility_friends BOOLEAN DEFAULT TRUE,
FOREIGN KEY (id) REFERENCES auth.users (id)
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-11
* Description: Table containing user's friends.
* ============================================= */
CREATE TABLE IF NOT EXISTS
friends (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
friends_since timestamptz NULL DEFAULT now(),
user1 UUID NOT NULL,
user2 UUID NOT NULL,
FOREIGN KEY (user1) REFERENCES auth.users (id),
FOREIGN KEY (user2) REFERENCES auth.users (id),
/* Ensure any permutation of 2 users are caught. */
UNIQUE (user1, user2),
UNIQUE (user2, user1)
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-11
* Description: Table containing friend requests.
* ============================================= */
CREATE TABLE IF NOT EXISTS
friend_requests (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at timestamptz NULL DEFAULT now(),
by_user UUID NOT NULL,
to_user UUID NOT NULL,
accepted BOOLEAN NULL DEFAULT NULL,
FOREIGN KEY (by_user) REFERENCES auth.users (id),
/* Ensure any permutation of 2 users are caught.
* There shouldn't ever be more than 1 friend
* request between two distinct users.
*/
UNIQUE (by_user, to_user),
UNIQUE (to_user, by_user)
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-05
* Description: Contains individual chess games,
* 1 per row, by user.
* ============================================= */
CREATE TABLE IF NOT EXISTS
history (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
datetime timestamptz NOT NULL DEFAULT (timezone('utc', now())),
player UUID NOT NULL,
/* https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation. */
fen jsonb NOT NULL,
/* 0 = loss, 1 = win, 2 = draw. */
score smallint NOT NULL,
FOREIGN KEY (player) REFERENCES auth.users (id)
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-02
* Description: Contains game data totals.
* ============================================= */
CREATE TABLE IF NOT EXISTS
gamedata (
id UUID PRIMARY KEY,
wins BIGINT NOT NULL DEFAULT 0,
losses BIGINT NOT NULL DEFAULT 0,
draws BIGINT NOT NULL DEFAULT 0,
FOREIGN KEY (id) REFERENCES auth.users (id)
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-05
* Description: WIP
* ============================================= */
CREATE TABLE IF NOT EXISTS
openings (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_by UUID NULL,
title TEXT NOT NULL,
description TEXT NOT NULL,
pgn JSONB NOT NULL,
timestamp TIMESTAMPTZ NOT NULL DEFAULT (timezone('utc', now())),
FOREIGN KEY (created_by) REFERENCES auth.users (id)
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-05
* Description: Contains groups of openings as
* an opening repetoire to train
* against.
* ============================================= */
CREATE TABLE IF NOT EXISTS
repertoire (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
timestamp TIMESTAMPTZ NOT NULL DEFAULT (timezone('utc', now())),
/* Named this way to avoid naming collision with USER() in SELECTs. */
usr UUID,
title TEXT DEFAULT (timezone('utc', now())),
description TEXT,
/* Array with opening IDs. */
openings JSONB,
FOREIGN KEY (usr) REFERENCES auth.users (id)
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-02
* Description: Contains admin-created news.
* ============================================= */
CREATE TABLE IF NOT EXISTS
news (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ NOT NULL DEFAULT (timezone('utc', now())),
/* Changed using a TRIGGER. */
modified_at TIMESTAMPTZ NOT NULL DEFAULT (timezone('utc', now())),
created_by UUID NOT NULL,
title TEXT NOT NULL,
summary TEXT NULL,
content TEXT NULL,
/* Used to check if "news" are still drafts, or if they've been published.
* A superuser is necessary to see them in the UI if FALSE. */
is_published BOOLEAN NOT NULL DEFAULT TRUE,
FOREIGN KEY (created_by) REFERENCES auth.users (id)
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Contains admin-created docs.
* ============================================= */
CREATE TABLE IF NOT EXISTS
docs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ NOT NULL DEFAULT (timezone('utc', now())),
/* Changed using a TRIGGER. */
modified_at TIMESTAMPTZ NOT NULL DEFAULT (timezone('utc', now())),
created_by UUID NOT NULL,
title TEXT NOT NULL,
summary TEXT NULL,
content TEXT NULL,
/* Used to check if "docs" are still drafts, or if they've been published.
* A superuser is necessary to see them in the UI if FALSE. */
is_published BOOLEAN NOT NULL DEFAULT TRUE,
FOREIGN KEY (created_by) REFERENCES auth.users (id)
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Contains admin-created FAQs.
* ============================================= */
CREATE TABLE IF NOT EXISTS
faq (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMPTZ NOT NULL DEFAULT (timezone('utc', now())),
/* Changed using a TRIGGER. */
modified_at TIMESTAMPTZ NOT NULL DEFAULT (timezone('utc', now())),
created_by UUID NOT NULL,
title TEXT NOT NULL,
summary TEXT NULL,
content TEXT NULL,
/* Used to check if "faq" are still drafts, or if they've been published.
* A superuser is necessary to see them in the UI if FALSE. */
is_published BOOLEAN NOT NULL DEFAULT TRUE,
FOREIGN KEY (created_by) REFERENCES auth.users (id)
);
/*********************************************
* *
* REALTIME SUPPORT *
* *
********************************************/
ALTER PUBLICATION supabase_realtime ADD TABLE public.openings;
ALTER PUBLICATION supabase_realtime ADD TABLE public.repertoire;
ALTER PUBLICATION supabase_realtime ADD TABLE public.gamedata;
ALTER PUBLICATION supabase_realtime ADD TABLE public.profiles;
ALTER PUBLICATION supabase_realtime ADD TABLE public.friends;
ALTER PUBLICATION supabase_realtime ADD TABLE public.friend_requests;
ALTER PUBLICATION supabase_realtime ADD TABLE public.news;
ALTER PUBLICATION supabase_realtime ADD TABLE public.faq;
ALTER PUBLICATION supabase_realtime ADD TABLE public.docs;
/*********************************************
* *
* ROW LEVEL SECURITY & POLICIES *
* (Insert order sensitive) *
********************************************/
/* NEWS POLICIES */
ALTER TABLE news ENABLE ROW LEVEL SECURITY;
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read access to non-user
* created rows.
* ============================================= */
CREATE POLICY news_r_to_published
ON public.news
AS PERMISSIVE
FOR SELECT
TO authenticated, anon
USING (
is_published = TRUE
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-19
* Description: Grants read access to unpublished
* news for administrators.
* ============================================= */
CREATE POLICY news_r_to_unpublished_as_admin
ON public.news
AS PERMISSIVE
FOR SELECT
TO authenticated
USING (
admin_is_admin() = TRUE AND
is_published = FALSE
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read-write access to admins.
* ============================================= */
CREATE POLICY news_rw_as_admin
ON public.news
AS PERMISSIVE
FOR ALL
TO authenticated
USING (
admin_is_admin() = TRUE
);
/* DOCS POLICIES */
ALTER TABLE docs ENABLE ROW LEVEL SECURITY;
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read access to non-user
* created rows.
* ============================================= */
CREATE POLICY docs_r_to_published
ON public.docs
AS PERMISSIVE
FOR SELECT
TO authenticated, anon
USING (
is_published = TRUE
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-19
* Description: Grants read access to unpublished
* docs for administrators.
* ============================================= */
CREATE POLICY docs_r_to_unpublished_as_admin
ON public.docs
AS PERMISSIVE
FOR SELECT
TO authenticated
USING (
admin_is_admin() = TRUE AND
is_published = FALSE
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read-write access to admins.
* ============================================= */
CREATE POLICY docs_rw_as_admin
ON public.docs
AS PERMISSIVE
FOR ALL
TO authenticated
USING (
admin_is_admin() = TRUE
);
/* FAQ POLICIES */
ALTER TABLE faq ENABLE ROW LEVEL SECURITY;
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read access to non-user
* created rows.
* ============================================= */
CREATE POLICY faq_r_to_published
ON public.faq
AS PERMISSIVE
FOR SELECT
TO authenticated, anon
USING (
is_published = TRUE
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-19
* Description: Grants read access to unpublished
* FAQs for administrators.
* ============================================= */
CREATE POLICY faq_r_to_unpublished_as_admin
ON public.faq
AS PERMISSIVE
FOR SELECT
TO authenticated
USING (
admin_is_admin() = TRUE AND
is_published = FALSE
);
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read-write access to admins.
* ============================================= */
CREATE POLICY faq_rw_as_admin
ON public.faq
AS PERMISSIVE
FOR ALL
TO authenticated
USING (
admin_is_admin() = TRUE
);
/* PROFILE POLICIES */
ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read-write access to user
* created rows.
* ============================================= */
CREATE POLICY profiles_rw_to_own_rows
ON profiles
TO authenticated
USING (id = auth.uid())
WITH CHECK (id = auth.uid());
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read access to non-user
* created rows.
* ============================================= */
CREATE POLICY profiles_r_to_not_own_rows
ON profiles
TO anon, authenticated
USING (id != auth.uid());
/* FRIEND POLICIES */
ALTER TABLE friends ENABLE ROW LEVEL SECURITY;
/* ===================================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read-write access to user
* rows where the user is the sender
* or recipient.
*
* The table has dual-unique constraints,
* it's just a bit of paranoia checking.
* =================================================== */
CREATE POLICY friends_rw_to_own_or_shared_rows
ON friends
TO authenticated
USING (user1 = auth.uid() OR user2 = auth.uid())
WITH CHECK (user1 = auth.uid() OR user2 = auth.uid());
/* ===================================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read access to non-user rows if
* the user has a public profile OR has
* friends list visibility on.
* =================================================== */
CREATE POLICY friends_r_to_others_public_rows
ON friends
TO anon, authenticated
USING (
NOT EXISTS (
SELECT 1 FROM profiles
WHERE id IN (friends.user1, friends.user2)
AND (visibility = TRUE OR visibility_friends = FALSE)
)
);
/* FRIEND REQUESTS POLICIES */
ALTER TABLE friend_requests ENABLE ROW LEVEL SECURITY;
/* ===================================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read-write access to user
* rows where the user is the sender
* or recipient.
*
* The table has dual-unique constraints,
* it's just a bit of paranoia checking.
* =================================================== */
CREATE POLICY friend_requests_rw_to_own_or_shared_rows
ON friend_requests
TO authenticated
USING (by_user = auth.uid() OR to_user = auth.uid())
WITH CHECK (by_user = auth.uid() OR to_user = auth.uid());
/* OPENING POLICIES */
ALTER TABLE openings ENABLE ROW LEVEL SECURITY;
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read-write access to user
* created rows.
* ============================================= */
CREATE POLICY openings_rw_to_own_rows
ON openings
TO authenticated
USING (created_by = auth.uid())
WITH CHECK (created_by = auth.uid());
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read access to non-user
* created rows. These are considered
* as "default" openings.
* ============================================= */
CREATE POLICY openings_r_to_default
ON openings
TO anon, authenticated
USING (created_by IS NULL);
/* REPERTOIRE POLICIES */
ALTER TABLE repertoire ENABLE ROW LEVEL SECURITY;
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read-write access to user
* created rows.
* ============================================= */
CREATE POLICY repertoire_rw_to_own_rows
ON repertoire
USING (usr = auth.uid())
WITH CHECK (usr = auth.uid());
/* GAMEDATA POLICIES */
ALTER TABLE gamedata ENABLE ROW LEVEL SECURITY;
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read-write access to user
* created rows.
* ============================================= */
CREATE POLICY gamedata_rw_to_own_rows
ON gamedata
TO authenticated
USING (id = auth.uid())
WITH CHECK (id = auth.uid());
/* ===================================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read access to non-user rows if
* the user has a public profile OR has
* friends list visibility on.
* =================================================== */
CREATE POLICY gamedata_r_to_others_public_rows
ON gamedata
TO anon, authenticated
USING (
NOT EXISTS (
SELECT 1 FROM profiles
WHERE id = gamedata.id
AND (visibility = TRUE OR visibility_friends = FALSE)
)
);
/* HISTORY POLICIES */
ALTER TABLE history ENABLE ROW LEVEL SECURITY;
/* =============================================
* Author: frigvid
* Create date: 2024-04-13
* Description: Grant read-write access to user
* created rows.
* ============================================= */
CREATE POLICY history_rw_to_own_rows
ON history
TO authenticated
USING (player = auth.uid())
WITH CHECK (player = auth.uid());
/*********************************************
* *
* FUNCTIONS AND TRIGGERS *
* *
********************************************/
/* =============================================
* Author: frigvid
* Create date: 2024-04-11
* Description: Check if user is an admin.
* ============================================= */
CREATE OR REPLACE FUNCTION public.admin_is_admin()
RETURNS boolean
LANGUAGE plpgsql SECURITY DEFINER
AS $$
DECLARE
usr_is_admin boolean;
BEGIN
usr_is_admin := false;
SELECT is_super_admin
INTO usr_is_admin
FROM auth.users
WHERE id = auth.uid();
RETURN usr_is_admin;
END;
$$;
/* =============================================
* Author: frigvid
* Create date: 2024-04-14
* Description: Administrator check for if user
* is an administrator. Does a bit
* of extra paranoid checking, just
* to be sure.
* ============================================= */
CREATE OR REPLACE FUNCTION public.admin_check_if_admin(user_to_check UUID)
RETURNS boolean
LANGUAGE plpgsql SECURITY DEFINER
AS $$
DECLARE
user_is_admin boolean;
BEGIN
/* Check if there is a currently authenticated user. */
IF auth.uid() IS NULL THEN
RETURN NULL;
END IF;
/* Check if the currently authenticated user is an administrator. */
IF (SELECT public.admin_is_admin()) IS FALSE THEN
RAISE EXCEPTION 'Authenticated user is not an administrator';
END IF;
IF NOT EXISTS(
SELECT 1
FROM auth.users
WHERE id = user_to_check
) THEN
RAISE EXCEPTION 'User to check does not appear to exist';
END IF;
IF (
SELECT is_super_admin
FROM auth.users
WHERE id = user_to_check
) IS TRUE THEN
/* User is an administrator. */
RETURN TRUE;
ELSE
/* User is not an administrator. */
RETURN FALSE;
END IF;
END;
$$;
/* =============================================
* Author: frigvid
* Create date: 2024-04-14
* Description: Promote a user to administrator
* status.
* ============================================= */
CREATE OR REPLACE FUNCTION admin_promote_to_admin(user_to_promote UUID)
RETURNS VOID
LANGUAGE plpgsql SECURITY DEFINER
AS $$
BEGIN
/* Check if there is a currently authenticated user. */
IF auth.uid() IS NULL THEN
RAISE EXCEPTION 'No authenticated user';
END IF;
/* Check if the currently authenticated user is an administrator. */
IF (SELECT public.admin_is_admin()) IS FALSE THEN
RAISE EXCEPTION 'Authenticated user is not an administrator';
END IF;
/* Check if the user to promote exists and is not already an administrator. */
IF (SELECT public.admin_check_if_admin(user_to_promote)) IS TRUE THEN
RAISE NOTICE 'User to promote is already an administrator';
ELSE
/* Promote the user to administrator. */
UPDATE auth.users SET is_super_admin = TRUE WHERE id = user_to_promote;
END IF;
END;
$$;
/* =============================================
* Author: frigvid
* Create date: 2024-04-14
* Description: Demote an administrator to a
* regular user.
* ============================================= */
CREATE OR REPLACE FUNCTION admin_demote_to_user(admin_to_demote UUID)
RETURNS VOID
LANGUAGE plpgsql SECURITY DEFINER
AS $$
BEGIN
/* Check if there is a currently authenticated user. */
IF auth.uid() IS NULL THEN
RAISE EXCEPTION 'No authenticated user';
END IF;
/* Check if the currently authenticated user is an administrator. */
IF (SELECT public.admin_is_admin()) IS FALSE THEN
RAISE EXCEPTION 'Authenticated user is not an administrator';
END IF;
/* Check if the administrator to demote exists and if they're already an administraotr. */
IF (SELECT public.admin_check_if_admin(admin_to_demote)) IS TRUE THEN
/* Demote the administrator to a regular user. */
UPDATE auth.users
SET is_super_admin = FALSE
WHERE id = admin_to_demote;
ELSE
RAISE NOTICE 'User to promote is already an administrator';
END IF;
END;
$$;
/* =============================================
* Author: frigvid
* Create date: 2024-04-14
* Description: Let admins delete a user manually.
* ============================================= */
CREATE OR REPLACE FUNCTION admin_delete_user(user_to_delete UUID)
RETURNS VOID
LANGUAGE plpgsql SECURITY DEFINER
AS $$
BEGIN
/* Check if there is a currently authenticated user. */
IF auth.uid() IS NULL THEN
RAISE EXCEPTION 'No authenticated user';
END IF;
/* Check if the currently authenticated user is an administrator. */
IF (SELECT public.admin_is_admin()) IS FALSE THEN
RAISE EXCEPTION 'Authenticated user is not an administrator';
END IF;
/* This is just taken from public.user_delete.
* Would be nice to not need to duplicate this,
* but given its use-case, I think its okay.
*/
/* Public. */
DELETE FROM public.profiles WHERE id = user_to_delete;
DELETE FROM public.friend_requests WHERE (by_user = user_to_delete OR to_user = user_to_delete);
DELETE FROM public.friends WHERE (user1 = user_to_delete OR user2 = user_to_delete);
DELETE FROM public.history WHERE player = user_to_delete;
DELETE FROM public.gamedata WHERE id = user_to_delete;
DELETE FROM public.repertoire WHERE usr = user_to_delete;
DELETE FROM public.openings WHERE created_by = user_to_delete;
/* Storage. */
/*
DELETE FROM storage.buckets WHERE id = user_to_delete;
DELETE FROM storage.migrations WHERE id = user_to_delete;
DELETE FROM storage.objects WHERE id = user_to_delete;
*/
/* Auth. */
DELETE FROM auth.users WHERE id = user_to_delete;
DELETE FROM auth.identities WHERE id = user_to_delete;
END;
$$;
/* =============================================
* Author: frigvid
* Create date: 2024-04-10
* Description: Delete's a given user's data, and
* their account when done.
* ============================================= */
CREATE OR REPLACE FUNCTION public.user_get_all_users()
RETURNS TABLE(
id UUID,
display_name TEXT,
avatar_url TEXT
)
LANGUAGE plpgsql
AS $$
BEGIN
/*
IF auth.uid() IS NULL THEN
RETURN;
END IF;
*/
RETURN QUERY
SELECT
p.id,
p.display_name,
p.avatar_url
FROM
public.profiles AS p;
END;
$$;
/* =============================================
* Author: frigvid
* Create date: 2024-04-04
* Description: Delete's a given user's data, and
* their account when done.
* ============================================= */
CREATE OR REPLACE FUNCTION public.user_delete()
RETURNS void
LANGUAGE SQL SECURITY DEFINER
AS $$
-- Public.
DELETE FROM public.profiles WHERE id = auth.uid();
DELETE FROM public.friend_requests WHERE (by_user = auth.uid() OR to_user = auth.uid());
DELETE FROM public.friends WHERE (user1 = auth.uid() OR user2 = auth.uid());
DELETE FROM public.history WHERE player = auth.uid();
DELETE FROM public.gamedata WHERE id = auth.uid();
DELETE FROM public.repertoire WHERE usr = auth.uid();
DELETE FROM public.openings WHERE created_by = auth.uid();
-- Storage.
/*
DELETE FROM storage.buckets WHERE id = auth.uid();
DELETE FROM storage.migrations WHERE id = auth.uid();
DELETE FROM storage.objects WHERE id = auth.uid();
*/
-- Auth.
DELETE FROM auth.users WHERE id = auth.uid();
DELETE FROM auth.identities WHERE id = auth.uid();
$$;
/* =============================================
* Author: frigvid
* Create date: 2024-04-04
* Description: Initializes a user's required data.
* ============================================= */
CREATE OR REPLACE FUNCTION user_init()
RETURNS TRIGGER
LANGUAGE plpgsql SECURITY DEFINER
AS $$
BEGIN
INSERT INTO public.gamedata (id, wins, losses, draws)
VALUES (NEW.id, 0, 0, 0);
INSERT INTO public.profiles (id, updated_at, display_name)
VALUES (NEW.id, NOW(), NULL);
RETURN NEW;
END;
$$;
/* =============================================
* Author: frigvid
* Create date: 2024-04-09
* Description: Create an opening.
* ============================================= */
CREATE OR REPLACE FUNCTION public.opening_create(
opn_title text,
opn_moves jsonb
)
RETURNS void
LANGUAGE SQL
AS $$
INSERT INTO public.openings (created_by, title, pgn)
VALUES (auth.uid(), opn_title, opn_moves);
$$;
/* =============================================
* Author: frigvid
* Create date: 2024-04-09
* Description: Deletes an opening.
* ============================================= */
CREATE OR REPLACE FUNCTION public.opening_delete(
opn_id UUID DEFAULT NULL
)
RETURNS void
LANGUAGE SQL
AS $$
DELETE FROM public.openings
WHERE id = opn_id AND (created_by = auth.uid() OR (created_by IS NULL AND admin_is_admin()));
$$;
/* =============================================
* Author: frigvid
* Create date: 2024-04-09
* Description: Gets an opening by ID.
* ============================================= */
CREATE OR REPLACE FUNCTION public.opening_get(
opn_id UUID
)
RETURNS SETOF openings
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT * FROM public.openings
WHERE id = opn_id;
END;
$$;
/* =============================================
* Author: frigvid
* Create date: 2024-04-09
* Description: Updates a user's profile data.
* ============================================= */
CREATE OR REPLACE FUNCTION public.profile_modify(
usr_avatar_url text,
usr_display_name text,
usr_about_me text,
usr_nationality text,
usr_visibility boolean
)
RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE public.profiles
SET
display_name = usr_display_name,
avatar_url = usr_avatar_url,
about_me = usr_about_me,
nationality = usr_nationality,
visibility = usr_visibility
WHERE id = auth.uid();