-
Notifications
You must be signed in to change notification settings - Fork 0
/
004_create_permissions.sql
6769 lines (5959 loc) · 206 KB
/
004_create_permissions.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
/*
GROUP HEADERS GENERATED BY: https://patorjk.com/software/taag/#p=display&h=0&v=1&c=c&f=ANSI%20Shadow&t=STAGE%20FUNCS
SUB GROUP HEADERS GENERATED BY: https://patorjk.com/software/taag/#p=display&h=1&v=1&c=c&f=Banner3&t=permissions
*/
select *
from start_version_update('1', 'Initial version', _component := 'keen_auth_permissions');
/***
* ███████╗██╗ ██╗████████╗███████╗███╗ ██╗███████╗██╗ ██████╗ ███╗ ██╗███████╗
* ██╔════╝╚██╗██╔╝╚══██╔══╝██╔════╝████╗ ██║██╔════╝██║██╔═══██╗████╗ ██║██╔════╝
* █████╗ ╚███╔╝ ██║ █████╗ ██╔██╗ ██║███████╗██║██║ ██║██╔██╗ ██║███████╗
* ██╔══╝ ██╔██╗ ██║ ██╔══╝ ██║╚██╗██║╚════██║██║██║ ██║██║╚██╗██║╚════██║
* ███████╗██╔╝ ██╗ ██║ ███████╗██║ ╚████║███████║██║╚██████╔╝██║ ╚████║███████║
* ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚══════╝
*
*/
-- Ensure necessary extensions
create extension if not exists "uuid-ossp" schema ext;
create extension if not exists ltree schema ext;
create extension if not exists unaccent schema ext;
/***
* ███████╗██████╗ ██████╗ ██████╗ ██████╗ ███████╗
* ██╔════╝██╔══██╗██╔══██╗██╔═══██╗██╔══██╗██╔════╝
* █████╗ ██████╔╝██████╔╝██║ ██║██████╔╝███████╗
* ██╔══╝ ██╔══██╗██╔══██╗██║ ██║██╔══██╗╚════██║
* ███████╗██║ ██║██║ ██║╚██████╔╝██║ ██║███████║
* ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝
*
*/
-- Cannot ensure user for email provider
create function error.raise_52101(_username text) returns void
language plpgsql as
$$
begin
raise
exception 'User (username: %) cannot be ensured for email provider, use registration for that', _username
using errcode = 52101;
end;
$$;
-- User cannot register user because the identity is already in use
create function error.raise_52102(_normalized_email text) returns void
language plpgsql as
$$
begin
raise
exception 'User identity (uid: %) is already in use', _normalized_email
using errcode = 52102;
end;
$$;
-- User does not exist
create function error.raise_52103(_user_id bigint, _email text default null) returns void
language plpgsql as
$$
begin
raise
exception 'User (user id: %, email: %) does not exist'
, _user_id, _email
using errcode = 52103;
end;
$$;
-- User is a system user
create function error.raise_52104(_user_id bigint) returns void
language plpgsql as
$$
begin
raise
exception 'User (user id: %) is a system user'
, _user_id
using errcode = 52104;
end;
$$;
-- User is in inactive state
create function error.raise_52105(_user_id bigint) returns void
language plpgsql as
$$
begin
raise
exception 'User (id: %) is not in active state', _user_id
using errcode = 52105;
end;
$$;
-- User is locked
create function error.raise_52106(_email text) returns void
language plpgsql as
$$
begin
raise
exception 'User (email: %) is locked out', _email
using errcode = 52106;
end;
$$;
-- Provider is not active
create function error.raise_52107(_provider_code text) returns void
language plpgsql as
$$
begin
raise
exception 'Provider (provider code: %) is not in active state', _provider_code
using errcode = 52107;
end;
$$;
-- User has no access to tenant
create function error.raise_52108(_tenant_id text, _username text) returns void
language plpgsql as
$$
begin
raise
exception 'User(username: %) has no access to tenant(id: %)', _username, _tenant_id
using errcode = 52108;
end;
$$;
-- User has no correct permission in tenant
create function error.raise_52109(_user_id bigint, _perm_codes text[], _tenant_id int default 1) returns void
language plpgsql as
$$
begin
raise
exception 'User(id: %) has no permission (codes: %) in tenant(id: %)', _user_id, array_to_string(_perm_codes, '; '), _tenant_id
using errcode = 52109;
end;
$$;
-- User provider identity is not active
create function error.raise_52110(_user_id bigint, _provider_code text) returns void
language plpgsql as
$$
begin
raise
exception 'User (id: %) identity for provider (code: %) is not in active state', _user_id, _provider_code
using errcode = 52110;
end;
$$;
-- User provider identity does not exist
create function error.raise_52111(_user_id bigint, _provider_code text) returns void
language plpgsql as
$$
begin
raise
exception 'User (id: %) identity for provider (code: %) does not exist', _user_id, _provider_code
using errcode = 52111;
end;
$$;
-- User provider identity does not exist
create function error.raise_52112(_user_id bigint) returns void
language plpgsql as
$$
begin
raise
exception 'User (id: %) is not supposed to log in', _user_id
using errcode = 52112;
end;
$$;
-- User group not found
create function error.raise_52171(_user_group_id int) returns void
language plpgsql as
$$
begin
raise
exception 'User group (group id: %) does not exist'
, _user_group_id
using errcode = 52171;
end;
$$;
-- User cannot be added to group because the group is not active
create function error.raise_52172(_user_group_id int) returns void
language plpgsql as
$$
begin
raise
exception 'User group (group id: %) is not active'
, _user_group_id
using errcode = 52172;
end;
$$;
-- User cannot be added to group because it's either not assignable or a
create function error.raise_52173(_user_group_id int) returns void
language plpgsql as
$$
begin
raise
exception 'User group (group id: %) is either is not assignable or is external'
, _user_group_id
using errcode = 52173;
end;
$$;
-- Either mapped object id or role must not be empty
create function error.raise_52174() returns void
language plpgsql as
$$
begin
raise
exception 'Either mapped object id or mapped role must not be empty'
using errcode = 52174;
end;
$$;
-- Permission set is not assignable
create function error.raise_52175(_perm_set_code text) returns void
language plpgsql as
$$
begin
raise
exception 'Permission set (code: %) is not assignable'
, _perm_set_code
using errcode = 52175;
end;
$$;
-- Permission is not assignable
create function error.raise_52176(_perm_set_code text) returns void
language plpgsql as
$$
begin
raise
exception 'Permission (code: %) is not assignable'
, _perm_set_code
using errcode = 52176;
end;
$$;
-- Permission set is not defined in tenant
create function error.raise_52177(_perm_set_id int, _tenant_id int default 1) returns void
language plpgsql as
$$
begin
raise
exception 'Permission set (id: %) is not defined in tenant (id: %)', _perm_set_id, _tenant_id
using errcode = 52177;
end;
$$;
-- Permission is not assignable
create function error.raise_52178() returns void
language plpgsql as
$$
begin
raise
exception 'Some permissions are not assignable'
using errcode = 52178;
end;
$$;
-- Permission is not assignable
create function error.raise_52179(_parent_full_code text) returns void
language plpgsql as
$$
begin
raise
exception 'Parent permission (code: %) does not exist',
_parent_full_code
using errcode = 52179;
end;
$$;
-- User group cannot be deleted because it's a system group
create function error.raise_52271(_user_group_id int) returns void
language plpgsql as
$$
begin
raise
exception 'User: group (group id: %) is a system group'
, _user_group_id
using errcode = 52271;
end;
$$;
-- Either user group id or target user id has to be not null
create function error.raise_52272() returns void
language plpgsql as
$$
begin
raise
exception 'Either user group is or target user id must not be null'
using errcode = 52272;
end;
$$;
-- Either permission set code or permission code has to be not null
create function error.raise_52273() returns void
language plpgsql as
$$
begin
raise
exception 'Either permission set code or permission code must not be null'
using errcode = 52273;
end;
$$;
-- Either permission id or code has to be not null
create function error.raise_52274() returns void
language plpgsql as
$$
begin
raise
exception 'Either permission id or code has to be not null'
using errcode = 52274;
end;
$$;
-- Permission does not exist
create function error.raise_52275(_permission_full_code text) returns void
language plpgsql as
$$
begin
raise
exception 'Permission (full code: %s) does not exist'
, _permission_full_code
using errcode = 52275;
end;
$$;
-- The same token is already used
create function error.raise_52276() returns void
language plpgsql as
$$
begin
raise
exception 'The same token is already used'
using errcode = 52276;
end;
$$;
-- Token does not exist
create function error.raise_52277() returns void
language plpgsql as
$$
begin
raise
exception 'Token does not exist'
using errcode = 52277;
end;
$$;
-- Token is not valid or has expired
create function error.raise_52278(_token_uid text) returns void
language plpgsql as
$$
begin
raise
exception 'Token (uid: %) is not valid or has expired', _token_uid
using errcode = 52278;
end;
$$;
-- Token was created for different user
create function error.raise_52279(_token_uid text) returns void
language plpgsql as
$$
begin
raise
exception 'Token (uid: %) was created for different user', _token_uid
using errcode = 52279;
end;
$$;
-- Permission does not exists
create function error.raise_52180(_permission_code text) returns void
language plpgsql as
$$
begin
raise
exception 'Permission (code: %) does not exists', _permission_code
using errcode = 52180;
end;
$$;
-- Permission is not assignable
create function error.raise_52181(_permission_code text) returns void
language plpgsql as
$$
begin
raise
exception 'Permission (code: %) is not assignable', _permission_code
using errcode = 52181;
end;
$$;
-- Permission does not exist
create function error.raise_52282(_perm_set_code text) returns void
language plpgsql as
$$
begin
raise
exception 'Permission set (code: %s) does not exist'
, _perm_set_code
using errcode = 52282;
end;
$$;
-- Permission does not exist
create function error.raise_52283(_perm_set_code text) returns void
language plpgsql as
$$
begin
raise
exception 'Permission set (code: %s) is not assignable', _perm_set_code
using errcode = 52283;
end;
$$;
-- API key/secret combination is not valid or API user has not been found
create function error.raise_52301(_api_key text) returns void
language plpgsql as
$$
begin
raise
exception 'API key/secret (key: %) combination is not valid or API user has not been found', _api_key
using errcode = 52301;
end;
$$;
-- User group has defined owners but the user is not one of them
-- User is not tenant or user group owner
create function error.raise_52401(_user_id bigint, _user_group_id int, _tenant_id int default 1) returns void
language plpgsql as
$$
begin
raise
exception 'User (uid: %) is not tenant (id: %) or user group (id: %) owner', _user_id, _tenant_id, _user_group_id
using errcode = 52281;
end;
$$;
/***
* ██╗--██╗███████╗██╗-----██████╗-███████╗██████╗-███████╗
* ██║--██║██╔════╝██║-----██╔══██╗██╔════╝██╔══██╗██╔════╝
* ███████║█████╗--██║-----██████╔╝█████╗--██████╔╝███████╗
* ██╔══██║██╔══╝--██║-----██╔═══╝-██╔══╝--██╔══██╗╚════██║
* ██║--██║███████╗███████╗██║-----███████╗██║--██║███████║
* ╚═╝--╚═╝╚══════╝╚══════╝╚═╝-----╚══════╝╚═╝--╚═╝╚══════╝
* --------------------------------------------------------
*/
create function auth.get_user_random_code()
returns text
language sql
volatile
parallel safe
cost 1
as
$$
select helpers.random_string(8);
$$;
/***
* ████████╗███████╗███╗---███╗██████╗-██╗------█████╗-████████╗███████╗███████╗
* ╚══██╔══╝██╔════╝████╗-████║██╔══██╗██║-----██╔══██╗╚══██╔══╝██╔════╝██╔════╝
* ---██║---█████╗--██╔████╔██║██████╔╝██║-----███████║---██║---█████╗--███████╗
* ---██║---██╔══╝--██║╚██╔╝██║██╔═══╝-██║-----██╔══██║---██║---██╔══╝--╚════██║
* ---██║---███████╗██║-╚═╝-██║██║-----███████╗██║--██║---██║---███████╗███████║
* ---╚═╝---╚══════╝╚═╝-----╚═╝╚═╝-----╚══════╝╚═╝--╚═╝---╚═╝---╚══════╝╚══════╝
* -----------------------------------------------------------------------------
*/
create table public._template_created
(
created timestamp with time zone default now() not null,
created_by text check (length(created_by) <= 250) default 'unknown' :: text not null
);
create table public._template_timestamps
(
modified timestamp with time zone default now() not null,
modified_by text check (length(modified_by) <= 250) default 'unknown' :: text not null
) inherits (_template_created);
/***
* ████████╗██████╗-██╗-██████╗--██████╗-███████╗██████╗-███████╗
* ╚══██╔══╝██╔══██╗██║██╔════╝-██╔════╝-██╔════╝██╔══██╗██╔════╝
* ---██║---██████╔╝██║██║--███╗██║--███╗█████╗--██████╔╝███████╗
* ---██║---██╔══██╗██║██║---██║██║---██║██╔══╝--██╔══██╗╚════██║
* ---██║---██║--██║██║╚██████╔╝╚██████╔╝███████╗██║--██║███████║
* ---╚═╝---╚═╝--╚═╝╚═╝-╚═════╝--╚═════╝-╚══════╝╚═╝--╚═╝╚══════╝
* --------------------------------------------------------------
*/
create or replace function helpers.trg_generate_code_from_title()
returns trigger
language plpgsql as
$$
begin
new.code
:= helpers.get_code(new.title, '_');
return new;
end
$$;
/***
* ███████╗██╗ ██╗███████╗████████╗███████╗███╗ ███╗ ████████╗ █████╗ ██████╗ ██╗ ███████╗███████╗
* ██╔════╝╚██╗ ██╔╝██╔════╝╚══██╔══╝██╔════╝████╗ ████║ ╚══██╔══╝██╔══██╗██╔══██╗██║ ██╔════╝██╔════╝
* ███████╗ ╚████╔╝ ███████╗ ██║ █████╗ ██╔████╔██║ ██║ ███████║██████╔╝██║ █████╗ ███████╗
* ╚════██║ ╚██╔╝ ╚════██║ ██║ ██╔══╝ ██║╚██╔╝██║ ██║ ██╔══██║██╔══██╗██║ ██╔══╝ ╚════██║
* ███████║ ██║ ███████║ ██║ ███████╗██║ ╚═╝ ██║ ██║ ██║ ██║██████╔╝███████╗███████╗███████║
* ╚══════╝ ╚═╝ ╚══════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═════╝ ╚══════╝╚══════╝╚══════╝
*
*/
create table const.sys_param
(
sys_param_id int generated always as identity not null primary key,
group_code text not null,
code text not null,
text_value text,
number_value bigint,
bool_value bool
) inherits (_template_timestamps);
create unique index uq_sys_params on const.sys_param (group_code, code);
create table const.tenant_access_type
(
code text not null primary key
);
create table const.token_type
(
code text not null primary key,
default_expiration_in_seconds int
);
create table const.token_channel
(
code text not null primary key
);
create table const.token_state
(
code text not null primary key
);
create table const.user_type
(
code text not null primary key
);
create table const.user_event_type
(
code text not null primary key
);
create table auth.provider
(
provider_id int generated always as identity not null primary key,
code text not null unique,
name text not null,
is_active bool not null default true
) inherits (_template_timestamps);
create table auth.tenant
(
tenant_id int generated always as identity not null primary key,
uuid uuid not null default ext.uuid_generate_v4(), -- if you need this kind of identifier, it's ready for you
title text not null,
code text not null,
is_removable bool not null default true,
is_assignable bool not null default true,
access_type_code text not null references const.tenant_access_type (code) default 'authenticated' -- members_only, authenticated
) inherits (_template_timestamps);
create table auth.user_info
(
user_id bigint generated by default as identity not null primary key,
user_type_code text not null references const.user_type (code) default 'normal', -- system, normal, api
code text not null default auth.get_user_random_code() unique, -- if you need this kind of identifier, it's ready for you
uuid uuid not null default ext.uuid_generate_v4() unique, -- if you need this kind of identifier, it's ready for you
can_login bool not null default true,
username text not null check (length(username) <= 255 ),
email text check (length(email) <= 255 ),
display_name text not null check (length(display_name) <= 255 ),
is_system bool not null default false,
is_active bool not null default true,
is_locked bool not null default false,
last_used_provider_code text references auth.provider (code) on update set null,
ua_username text not null generated always as ( lower(username) ) stored,
original_username text not null
) inherits (_template_timestamps);
create unique index uq_auth_user_info on auth.user_info (username);
create table auth.tenant_user
(
tenant_user_id bigint generated always as identity not null primary key,
tenant_id int not null references auth.tenant (tenant_id) on delete cascade,
user_id int not null references auth.user_info (user_id) on delete cascade
) inherits (_template_created);
create table auth.user_permission_cache
(
upc_id bigint generated always as identity not null primary key,
user_id bigint not null references auth.user_info (user_id),
tenant_id int not null references auth.tenant (tenant_id),
groups text[] not null default '{}',
permissions text[] not null default '{}',
expiration_date timestamptz not null
) inherits (_template_timestamps);
-- can be replaced with your version of user data
create table auth.user_data
(
user_data_id bigint generated always as identity not null primary key,
user_id bigint not null unique references auth.user_info (user_id) on delete cascade,
first_name text,
middle_name text,
last_name text
) inherits (_template_timestamps);
create table auth.user_identity
(
user_identity_id bigint generated always as identity not null primary key,
provider_code text not null references auth.provider (code) on update cascade on delete cascade,
uid text,
user_id bigint references auth.user_info (user_id) on delete cascade,
provider_groups text[],
provider_roles text[],
user_data jsonb,
password_hash text,
password_salt text,
is_active bool not null default true
) inherits (_template_timestamps);
create unique index uq_user_identity on auth.user_identity (provider_code, coalesce(uid, '-1'));
create table auth.permission
(
permission_id int generated always as identity not null primary key,
is_assignable bool not null default true,
title text not null,
code text,
full_code ext.ltree,
node_path ext.ltree,
has_children bool not null default false,
full_title text
) inherits (_template_timestamps);
create unique index uq_permission_full_code on auth.permission (full_code);
create index ix_permission_node_path on auth.permission using gist (node_path);
create table auth.perm_set
(
perm_set_id int generated always as identity not null primary key,
tenant_id int references auth.tenant (tenant_id) on delete cascade,
title text not null,
code text not null unique, -- set with trigger
is_system bool not null default false,
is_assignable bool not null default true
) inherits (_template_timestamps);
create trigger c_perm_set_code
before insert
on auth.perm_set
for each row
execute procedure helpers.trg_generate_code_from_title();
create table auth.perm_set_perm
(
psp_id int generated always as identity not null primary key,
perm_set_id int not null references auth.perm_set (perm_set_id) on delete cascade,
permission_id int not null references auth.permission (permission_id) on delete cascade
) inherits (_template_created);
create table auth.user_group
(
user_group_id int generated always as identity not null primary key,
tenant_id int references auth.tenant (tenant_id),
title text not null,
code text not null, -- set with trigger
is_system bool not null default false,
is_external bool not null default false,
is_assignable bool not null default true,
is_active bool not null default true,
is_default bool not null default false check ( is_external = false or (is_external and not is_default) ),
can_members_manage_others bool not null default false,
can_members_see_others bool not null default true
) inherits (_template_timestamps);
create trigger c_user_group_code
before insert
on auth.user_group
for each row
execute procedure helpers.trg_generate_code_from_title();
create table auth.user_group_mapping
(
ug_mapping_id int generated always as identity not null primary key,
group_id int not null references auth.user_group (user_group_id),
provider_code text not null references auth.provider (code) on update cascade on delete cascade,
mapped_object_id text,
mapped_object_name text,
mapped_role text
) inherits (_template_created);
create unique index uq_user_group_mapping on auth.user_group_mapping (group_id, provider_code,
coalesce(mapped_object_id, ''),
coalesce(mapped_role, ''));
create table auth.user_group_member
(
member_id bigint generated always as identity not null primary key,
group_id int not null references auth.user_group (user_group_id) on delete cascade,
user_id bigint not null references auth.user_info (user_id) on delete cascade,
mapping_id int references auth.user_group_mapping (ug_mapping_id) on delete cascade,
manual_assignment bool not null default false
) inherits (_template_created);
create unique index uq_user_group_member on auth.user_group_member (group_id, user_id, coalesce(mapping_id, 0));
create table auth.owner
(
owner_id bigint generated always as identity not null primary key,
tenant_id int not null references auth.tenant (tenant_id) on delete cascade,
user_group_id int references auth.user_group (user_group_id) on delete cascade,
user_id bigint not null references auth.user_info (user_id) on delete cascade
) inherits (_template_created);
create unique index ix_owner on auth.owner using btree (user_id, tenant_id, user_group_id);
create table auth.permission_assignment
(
assignment_id bigint not null primary key generated always as identity,
tenant_id int references auth.tenant (tenant_id) on delete cascade,
group_id int references auth.user_group (user_group_id),
user_id bigint references auth.user_info (user_id),
perm_set_id int references auth.perm_set (perm_set_id),
permission_id int references auth.permission (permission_id),
constraint pa_either_object check (group_id is not null or user_id is not null),
constraint pa_either_perm check (perm_set_id is not null or permission_id is not null)
) inherits (_template_created);
create unique index uq_permission_assignment on auth.permission_assignment (group_id, coalesce(user_id, 0),
coalesce(perm_set_id, 0),
coalesce(permission_id, 0));
create table auth.user_event
(
user_event_id bigint generated always as identity not null primary key,
event_type_code text not null references const.user_event_type (code),
requester_user_id bigint references auth.user_info (user_id) on delete set null,
requester_username text,
target_user_id bigint references auth.user_info (user_id) on delete set null,
target_user_oid text,
target_username text,
ip_address text,
user_agent text,
origin text,
event_data jsonb
) inherits (_template_created);
create index ix_user_event_data on auth.user_event using gin (event_data jsonb_path_ops);
create table auth.token
(
token_id bigint generated always as identity not null primary key,
user_id bigint references auth.user_info (user_id),
user_oid text,
uid text not null default helpers.random_string(12) unique, -- token uid
user_event_id int references auth.user_event (user_event_id) on delete cascade, -- related authentication event
token_state_code text not null default 'valid' references const.token_state (code),
token_type_code text not null references const.token_type (code),
token_channel_code text not null references const.token_channel (code),
token text not null,
expires_at timestamptz not null,
used_at timestamptz,
ip_address text,
user_agent text,
origin text,
token_data jsonb
) inherits (_template_timestamps);
create index ix_token_token on auth.token using hash (token);
create index ix_token_expires_at on auth.token using btree (expires_at);
create index ix_token_token_data on auth.token using gin (token_data jsonb_path_ops);
create table journal
(
journal_id bigint not null generated always as identity,
tenant_id int references auth.tenant (tenant_id),
data_group text,
data_object_id bigint,
data_object_code text,
event_id int,
user_id bigint references auth.user_info (user_id) on delete set null,
message text not null,
data_payload jsonb
) inherits (_template_created);
create index ix_journal on journal (tenant_id, data_group, coalesce(data_object_id, 0),
coalesce(data_object_code, '-'));
create table auth.api_key
(
api_key_id int not null primary key generated always as identity,
tenant_id int not null references auth.tenant (tenant_id) on delete cascade default 1,
title text not null,
description text,
api_key text not null unique,
secret_hash bytea not null,
expire_at timestamptz,
notification_email text
) inherits (_template_timestamps);
/***
* ██╗ ██╗██╗███████╗██╗ ██╗███████╗
* ██║ ██║██║██╔════╝██║ ██║██╔════╝
* ██║ ██║██║█████╗ ██║ █╗ ██║███████╗
* ╚██╗ ██╔╝██║██╔══╝ ██║███╗██║╚════██║
* ╚████╔╝ ██║███████╗╚███╔███╔╝███████║
* ╚═══╝ ╚═╝╚══════╝ ╚══╝╚══╝ ╚══════╝
*
*/
create view auth.user_group_members as
(
select ug.tenant_id
, ugm.member_id
, case when t.code is null then 'system' else t.code end as tenant_code
, ui.user_id
, ui.display_name as user_display_name
, ui.uuid as user_uuid
, ui.code as user_code
, ug.user_group_id
, ug.is_external
, ug.is_active
, ug.is_assignable
, ug.title as group_title
, ug.code as group_code
, case when ugm.mapping_id is not null then 'mapped_member' else 'direct_member' end as member_type
, ugm.manual_assignment
, u.mapped_object_name
, u.mapped_role
from auth.user_group ug
left join auth.user_group_member ugm on ugm.group_id = ug.user_group_id
left join auth.user_info ui
on ui.user_id = ugm.user_id
inner join auth.tenant t on ug.tenant_id = t.tenant_id
left join auth.user_group_mapping u on ugm.mapping_id = u.ug_mapping_id
);
create view auth.active_user_groups as
(
select ug.user_group_id
, ug.is_system
, ug.is_external
, ug.is_assignable
, ug.is_active
, ug.is_default
, ug.title as group_title
, ug.code as group_code
, ug.tenant_id
, t.code as tenant_code
, t.title as tenant_title
from auth.user_group ug
left join auth.tenant t on ug.tenant_id = t.tenant_id
where ug.is_active
);
create view auth.effective_permissions as
(
select distinct ps.perm_set_id
, ps.code as perm_set_code
, ps.title as perm_set_title
, ps.is_assignable as perm_set_is_assignable
, sp.permission_id
, sp.title as permission_title
, sp.full_code as permission_code
, sp.is_assignable as permission_is_assignable
from auth.perm_set ps
inner join auth.perm_set_perm psp on ps.perm_set_id = psp.perm_set_id
inner join auth.permission p on psp.permission_id = p.permission_id
inner join auth.permission sp on sp.node_path <@ p.node_path);
/***
* -----██╗-██████╗-██╗---██╗██████╗-███╗---██╗-█████╗-██╗----------██████╗-██████╗-███████╗██████╗--█████╗-████████╗██╗-██████╗-███╗---██╗███████╗
* -----██║██╔═══██╗██║---██║██╔══██╗████╗--██║██╔══██╗██║---------██╔═══██╗██╔══██╗██╔════╝██╔══██╗██╔══██╗╚══██╔══╝██║██╔═══██╗████╗--██║██╔════╝
* -----██║██║---██║██║---██║██████╔╝██╔██╗-██║███████║██║---------██║---██║██████╔╝█████╗--██████╔╝███████║---██║---██║██║---██║██╔██╗-██║███████╗
* ██---██║██║---██║██║---██║██╔══██╗██║╚██╗██║██╔══██║██║---------██║---██║██╔═══╝-██╔══╝--██╔══██╗██╔══██║---██║---██║██║---██║██║╚██╗██║╚════██║
* ╚█████╔╝╚██████╔╝╚██████╔╝██║--██║██║-╚████║██║--██║███████╗----╚██████╔╝██║-----███████╗██║--██║██║--██║---██║---██║╚██████╔╝██║-╚████║███████║
* -╚════╝--╚═════╝--╚═════╝-╚═╝--╚═╝╚═╝--╚═══╝╚═╝--╚═╝╚══════╝-----╚═════╝-╚═╝-----╚══════╝╚═╝--╚═╝╚═╝--╚═╝---╚═╝---╚═╝-╚═════╝-╚═╝--╚═══╝╚══════╝
* ------------------------------------------------------------------------------------------------------------------------------------------------
*/
create or replace function add_journal_msg_jsonb(_created_by text
, _user_id bigint, _msg text
, _data_group text default 'system'
, _data_object_id bigint default null
, _payload jsonb default null
, _event_id int default null
, _data_object_code text default null
, _tenant_id int default 1)
returns setof journal
language plpgsql
rows 1
as
$$
begin
return query insert into journal (created_by, tenant_id, data_group, data_object_id, data_object_code, event_id,
user_id, message, data_payload)
values ( _created_by, _tenant_id, _data_group, _data_object_id, _data_object_code, _event_id, _user_id, _msg
, case when _payload is not null then _payload end)
returning *;
end;
$$;
create or replace function add_journal_msg(_created_by text
, _user_id bigint, _msg text
, _data_group text default 'system'
, _data_object_id bigint default null
, _payload text[] default null
, _event_id int default null
, _data_object_code text default null