-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path016_update_permissions_v1-10.sql
1436 lines (1293 loc) · 49.9 KB
/
016_update_permissions_v1-10.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 check_version('1.9', _component := 'keen_auth_permissions', _throw_err := true);
select *
from start_version_update('1.10',
'Update of user_info table to have user_preferences instead of having them in user_data. Also few minor fixes of unrelated stuff',
_component := 'keen_auth_permissions');
/***
* ███████╗██╗██╗ ██╗███████╗███████╗
* ██╔════╝██║╚██╗██╔╝██╔════╝██╔════╝
* █████╗ ██║ ╚███╔╝ █████╗ ███████╗
* ██╔══╝ ██║ ██╔██╗ ██╔══╝ ╚════██║
* ██║ ██║██╔╝ ██╗███████╗███████║
* ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝
*
*/
drop function if exists public.search_journal_msgs(integer, text, timestamp with time zone, timestamp with time zone,
integer, integer,
text, bigint, text, jsonb, integer, integer, integer);
drop function if exists public.search_journal_msgs(bigint, text, timestamp with time zone, timestamp with time zone,
integer, integer,
text, bigint, text, jsonb, integer, integer, integer);
create or replace function public.search_journal_msgs(_user_id bigint, _search_text text,
_from timestamp with time zone DEFAULT NULL::timestamp with time zone,
_to timestamp with time zone DEFAULT NULL::timestamp with time zone,
_target_user_id integer DEFAULT null,
_event_id integer DEFAULT NULL::integer,
_data_group text DEFAULT NULL::text,
_data_object_id bigint DEFAULT NULL::bigint,
_data_object_code text DEFAULT NULL::text,
_payload_criteria jsonb DEFAULT NULL::jsonb,
_page integer DEFAULT 1, _page_size integer DEFAULT 10,
_tenant_id integer DEFAULT 1)
returns TABLE
(
__created timestamp with time zone,
__created_by text,
__journal_id bigint,
__event_id integer,
__data_group text,
__data_object_id bigint,
__data_object_code text,
__user_id bigint,
__msg text,
__total_items bigint
)
stable
language plpgsql
as
$$
declare
__can_read_global_journal bool;
begin
__can_read_global_journal = auth.has_permission(_user_id, 'journal.read_global_journal', _throw_err := false);
if (_tenant_id = 1) then
if not __can_read_global_journal then
perform auth.throw_no_permission(_user_id, 'journal.read_global_journal');
end if;
else
perform
auth.has_permission(_user_id, 'journal.read_journal', _tenant_id);
end if;
_search_text := helpers.normalize_text(_search_text);
return query
with filtered_rows
as (select journal_id
, count(1) over () as total_items
from journal
where (helpers.is_empty_string(_search_text) or
journal.nrm_search_data like '%' || _search_text || '%')
and ((_tenant_id = 1 and __can_read_global_journal) or tenant_id = _tenant_id)
and (_target_user_id is null or user_id = _target_user_id)
and (_event_id is null or event_id = _event_id)
and (_data_group is null or data_group = _data_group)
and (_data_object_id is null or data_object_id = _data_object_id)
and (_data_object_code is null or data_object_code = _data_object_code)
and (_payload_criteria is null or data_payload @> _payload_criteria)
and created between coalesce(_from, now() - interval '100 years') and coalesce(_to, now() + interval '100 years')
order by created desc
offset ((_page - 1) * _page_size) limit _page_size)
select created
, created_by
, fr.journal_id
, event_id
, data_group
, data_object_id
, data_object_code
, user_id
, message
, fr.total_items
from filtered_rows fr
inner join journal j on fr.journal_id = j.journal_id
order by created desc;
end;
$$;
/***
* ████████╗ █████╗ ██████╗ ██╗ ███████╗███████╗
* ╚══██╔══╝██╔══██╗██╔══██╗██║ ██╔════╝██╔════╝
* ██║ ███████║██████╔╝██║ █████╗ ███████╗
* ██║ ██╔══██║██╔══██╗██║ ██╔══╝ ╚════██║
* ██║ ██║ ██║██████╔╝███████╗███████╗███████║
* ╚═╝ ╚═╝ ╚═╝╚═════╝ ╚══════╝╚══════╝╚══════╝
*
*/
alter table auth.user_info
add column user_preferences jsonb default '{}' not null;
alter table auth.perm_set
drop constraint perm_set_code_key;
alter table auth.perm_set
add unique (code, tenant_id);
alter table auth.tenant
add column is_default bool default false not null;
create table auth.user_tenant_preference
(
tenant_user_preference_id bigint generated always as identity not null primary key,
user_id bigint not null references auth.user_info (user_id) on delete cascade,
tenant_id bigint not null references auth.tenant (tenant_id) on delete cascade,
user_preferences jsonb default '{}' not null
) inherits (_template_timestamps);
create unique index uq_user_tenant_preference on auth.user_tenant_preference (user_id, tenant_id);
create index ix_user_tenant_preference on auth.user_tenant_preference (user_id, tenant_id);
/***
* ████████╗███████╗███╗ ██╗ █████╗ ███╗ ██╗████████╗███████╗
* ╚══██╔══╝██╔════╝████╗ ██║██╔══██╗████╗ ██║╚══██╔══╝██╔════╝
* ██║ █████╗ ██╔██╗ ██║███████║██╔██╗ ██║ ██║ ███████╗
* ██║ ██╔══╝ ██║╚██╗██║██╔══██║██║╚██╗██║ ██║ ╚════██║
* ██║ ███████╗██║ ╚████║██║ ██║██║ ╚████║ ██║ ███████║
* ╚═╝ ╚══════╝╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚══════╝
*
*/
drop function auth.create_tenant(
_created_by text
, _user_id bigint
, _title text
, _code text
, _is_removable bool
, _is_assignable bool
, _tenant_owner_id bigint);
create or replace function auth.create_tenant(
_created_by text
, _user_id bigint
, _title text
, _code text default null
, _is_removable bool default true
, _is_assignable bool default true
, _tenant_owner_id bigint default null)
returns table
(
__tenant_id integer,
__uuid uuid,
__title text,
__code text,
__is_removable boolean,
__is_assignable boolean,
__access_type_code text,
__is_default boolean
)
language plpgsql
rows 1
as
$$
declare
__last_item auth.tenant;
__tenant_owner_group_id int;
__tenant_member_group_id int;
begin
perform
auth.has_permission(_user_id, 'tenants.create_tenant');
insert into auth.tenant (created_by, modified_by, title, code, is_removable, is_assignable)
values (_created_by, _created_by, _title, coalesce(_code, helpers.get_code(_title)), _is_removable, _is_assignable)
returning * into __last_item;
perform
add_journal_msg_jsonb(_created_by, _user_id
, format('Tenant: (code: %s, title: %s) created by: %s'
, __last_item.code, __last_item.title, _created_by)
, 'tenant'
, __last_item.tenant_id
, _data_object_code := __last_item.code
, _payload := jsonb_build_object(
'title', _title
, 'is_assignable', _is_assignable
, 'is_removable', _is_removable
)
, _event_id := 50001
, _tenant_id := 1);
select __user_group_id
from unsecure.create_user_group(_created_by, _user_id, 'Tenant Admins'
, true, true, false, true, _tenant_id := __last_item.tenant_id)
into __tenant_owner_group_id;
perform unsecure.assign_permission(_created_by, _user_id
, __tenant_owner_group_id, null, 'tenant_admin', _tenant_id := __last_item.tenant_id);
select __user_group_id
from unsecure.create_user_group(_created_by, _user_id, 'Tenant Members'
, true, true, false, true, _tenant_id := __last_item.tenant_id)
into __tenant_member_group_id;
perform unsecure.assign_permission(_created_by, _user_id
, __tenant_member_group_id, null, 'tenant_member', _tenant_id := __last_item.tenant_id);
if
(_tenant_owner_id is not null)
then
perform auth.create_owner(_created_by, _user_id, _tenant_owner_id, null, _tenant_id := __last_item.tenant_id);
end if;
return query
select tenant_id
, uuid
, title
, code
, is_removable
, is_assignable
, access_type_code
, is_default
from auth.tenant
where tenant_id = __last_item.tenant_id;
end;
$$;
create or replace function auth.update_tenant(
_created_by text
, _user_id bigint
, _title text
, _code text default null
, _is_removable bool default true
, _is_assignable bool default true
, _tenant_owner_id bigint default null)
returns table
(
__tenant_id integer,
__uuid uuid,
__title text,
__code text,
__is_removable boolean,
__is_assignable boolean,
__access_type_code text,
__is_default boolean
)
language plpgsql
rows 1
as
$$
declare
__last_item auth.tenant;
__tenant_owner_group_id int;
__tenant_member_group_id int;
begin
perform
auth.has_permission(_user_id, 'tenants.create_tenant');
insert into auth.tenant (created_by, modified_by, title, code, is_removable, is_assignable)
values (_created_by, _created_by, _title, coalesce(_code, helpers.get_code(_title)), _is_removable, _is_assignable)
returning * into __last_item;
perform
add_journal_msg_jsonb(_created_by, _user_id
, format('Tenant: (code: %s, title: %s) created by: %s'
, __last_item.code, __last_item.title, _created_by)
, 'tenant'
, __last_item.tenant_id
, _data_object_code := __last_item.code
, _payload := jsonb_build_object(
'title', _title
, 'is_assignable', _is_assignable
, 'is_removable', _is_removable
)
, _event_id := 50001
, _tenant_id := 1);
select __user_group_id
from unsecure.create_user_group(_created_by, _user_id, 'Tenant Admins'
, true, true, false, true, _tenant_id := __last_item.tenant_id)
into __tenant_owner_group_id;
perform unsecure.assign_permission(_created_by, _user_id
, __tenant_owner_group_id, null, 'tenant_admin', _tenant_id := __last_item.tenant_id);
select __user_group_id
from unsecure.create_user_group(_created_by, _user_id, 'Tenant Members'
, true, true, false, true, _tenant_id := __last_item.tenant_id)
into __tenant_member_group_id;
perform unsecure.assign_permission(_created_by, _user_id
, __tenant_member_group_id, null, 'tenant_member', _tenant_id := __last_item.tenant_id);
if
(_tenant_owner_id is not null)
then
perform auth.create_owner(_created_by, _user_id, _tenant_owner_id, null, _tenant_id := __last_item.tenant_id);
end if;
return query
select created
, created_by
, modified
, modified_by
, tenant_id
, uuid
, title
, code
, is_removable
, is_assignable
, access_type_code
, is_default
from auth.tenant
where tenant_id = __last_item.tenant_id;
end;
$$;
create function unsecure.delete_tenant(
_deleted_by text
, _user_id bigint
, _tenant_id int)
returns table
(
__tenant_id integer,
__uuid uuid,
__code text
)
language plpgsql
as
$$
declare
__last_item auth.tenant;
begin
delete
from auth.tenant
where tenant_id = _tenant_id
returning * into __last_item;
perform
add_journal_msg_jsonb(_deleted_by, _user_id
, format('Tenant: (code: %s, title: %s) deleted by: %s'
, __last_item.code, __last_item.title, _deleted_by)
, 'tenant'
, __last_item.tenant_id
, _data_object_code := __last_item.code
, _event_id := 50003
, _tenant_id := 1);
return query
select __last_item.tenant_id
, __last_item.uuid
, __last_item.code;
end;
$$;
create or replace function auth.delete_tenant(
_deleted_by text
, _user_id bigint
, _tenant_uuid uuid)
returns table
(
__tenant_id integer,
__uuid uuid,
__code text
)
language plpgsql
rows 1
as
$$
begin
perform
auth.has_permission(_user_id, 'tenants.delete_tenant');
return query
select *
from auth.tenant t
, lateral unsecure.delete_tenant(_deleted_by, _user_id, t.tenant_id)
where t.uuid = _tenant_uuid;
end;
$$;
create or replace function auth.delete_tenant_by_uuid(
_deleted_by text
, _user_id bigint
, _tenant_uuid uuid)
returns table
(
__tenant_id integer,
__uuid uuid,
__code text
)
language plpgsql
rows 1
as
$$
begin
perform
auth.has_permission(_user_id, 'tenants.delete_tenant');
return query
select *
from auth.tenant t
, lateral unsecure.delete_tenant(_deleted_by, _user_id, t.tenant_id)
where t.uuid = _tenant_uuid;
end;
$$;
drop function auth.get_user_available_tenants(
bigint
, bigint
, text);
-- missing schema for `permission` table
create or replace function auth.get_user_available_tenants(
_user_id bigint
, _target_user_id bigint)
returns table
(
__tenant_id integer,
__tenant_uuid text,
__tenant_code text,
__tenant_title text,
__tenant_is_default bool
)
language plpgsql
as
$$
declare
__necessary_permission_code text := 'users.get_available_tenants';
begin
if _user_id <> _target_user_id and not auth.has_permission(_user_id, __necessary_permission_code)
then
perform auth.throw_no_permission(_user_id, __necessary_permission_code);
end if;
return query
with member_of_tenants as (
select tenant_id
, group_id
from auth.user_group_member ugm
inner join auth.user_group ug on ug.user_group_id = ugm.group_id
where ugm.user_id = _target_user_id)
select distinct mt.tenant_id
, t.uuid::text
, t.code
, t.title
, t.is_default
from member_of_tenants mt
inner join auth.tenant t on mt.tenant_id = t.tenant_id
order by t.title;
end;
$$;
create unique index uq_tenant_uuid on auth.tenant (uuid);
truncate table auth.user_permission_cache;
alter table auth.user_permission_cache
add column tenant_uuid uuid references auth.tenant (uuid) not null;
create unique index uq_user_permission_cache on auth.user_permission_cache (user_id, tenant_id);
create index ix_user_permission_cache on auth.user_permission_cache (user_id, tenant_id);
/***
* ███████╗██╗ ██╗███╗ ██╗ ██████╗████████╗██╗ ██████╗ ███╗ ██╗███████╗
* ██╔════╝██║ ██║████╗ ██║██╔════╝╚══██╔══╝██║██╔═══██╗████╗ ██║██╔════╝
* █████╗ ██║ ██║██╔██╗ ██║██║ ██║ ██║██║ ██║██╔██╗ ██║███████╗
* ██╔══╝ ██║ ██║██║╚██╗██║██║ ██║ ██║██║ ██║██║╚██╗██║╚════██║
* ██║ ╚██████╔╝██║ ╚████║╚██████╗ ██║ ██║╚██████╔╝██║ ╚████║███████║
* ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚══════╝
*
*/
-- create or replace function auth.user_has_permission_for_user_in_any_tenant(
-- _user_id bigint
-- , _target_user_id bigint
-- , _perm_code text
-- , _throw_err boolean default true)
-- returns boolean
-- stable
-- language plpgsql
-- as
-- $$
-- begin
-- if _user_id <> _target_user_id
-- and true not in (
-- select has_permision
-- from auth.user_info ui
-- inner join auth.tenant_user tu using (user_id)
-- , lateral auth.has_permission(_user_id, _perm_code, _tenant_id := tu.tenant_id,
-- _throw_err := false) has_permision)
-- then
-- if _throw_err
-- then
-- perform auth.throw_no_permission(_user_id, _perm_code);
-- else
-- return false;
-- end if;
-- else
-- return true;
-- end if;
-- end
-- $$;
drop function unsecure.recalculate_user_groups(
_created_by text
, _target_user_id bigint
, _provider_code text);
create or replace function unsecure.recalculate_user_groups(
_created_by text
, _target_user_id bigint
, _provider_code text)
returns TABLE
(
__tenant_id integer,
__user_group_id integer,
__user_group_code text
)
language plpgsql
as
$$
declare
__not_really_used int;
__provider_groups text[];
__provider_roles text[];
begin
select provider_groups
, provider_roles
from auth.user_identity
where provider_code = _provider_code
and user_id = _target_user_id
into __provider_groups, __provider_roles;
-- cleanup membership of groups user is no longer part of
with affected_deleted_group_tenants as (
delete
from auth.user_group_member
where user_id = _target_user_id
and mapping_id is not null
and group_id not in (
select distinct ugm.group_id
from unnest(__provider_groups) g
inner join auth.user_group_mapping ugm
on ugm.provider_code = _provider_code and ugm.mapped_object_id = lower(g)
inner join auth.user_group u
on u.user_group_id = ugm.group_id
union
select distinct ugm.group_id
from unnest(__provider_roles) r
inner join auth.user_group_mapping ugm
on ugm.provider_code = _provider_code and ugm.mapped_role = lower(r)
inner join auth.user_group u
on u.user_group_id = ugm.group_id)
returning group_id)
, affected_group_tenants as (
insert
into auth.user_group_member (created_by, user_id, group_id, mapping_id, member_type_code)
select distinct _created_by
, _target_user_id
, ugm.group_id
, ugm.ug_mapping_id
, 'adhoc'
from unnest(__provider_groups) g
inner join auth.user_group_mapping ugm
on ugm.provider_code = _provider_code and ugm.mapped_object_id = lower(g)
where ugm.group_id not in (
select group_id
from auth.user_group_member
where user_id = _target_user_id)
returning group_id)
, affected_role_tenants as (
insert
into auth.user_group_member (created_by, user_id, group_id, mapping_id, member_type_code)
select distinct _created_by
, _target_user_id
, ugm.group_id
, ugm.ug_mapping_id
, 'adhoc'
from unnest(__provider_roles) r
inner join auth.user_group_mapping ugm
on ugm.provider_code = _provider_code and ugm.mapped_role = lower(r)
where ugm.group_id not in (
select group_id
from auth.user_group_member
where user_id = _target_user_id)
returning group_id)
, all_group_ids as (
select group_id
from affected_deleted_group_tenants
union
select group_id
from affected_group_tenants
union
select group_id
from affected_role_tenants)
, all_tenants as (
select tenant_id
from all_group_ids ids
inner join auth.user_group ug
on ids.group_id = ug.user_group_id
group by tenant_id)
-- variable not really used, it's there just to avoid 'query has no destination for result data'
select at.tenant_id
from all_tenants at
, lateral unsecure.clear_permission_cache(_created_by, _target_user_id, at.tenant_id) r
into __not_really_used;
return query
select distinct ug.tenant_id
, ug.user_group_id
, ug.code
from auth.user_group_member ugm
inner join auth.user_group ug on ug.user_group_id = ugm.group_id
where ugm.user_id = _target_user_id;
end;
$$;
-- from now on, user always have to have permissions for given tenant, and no permission sharing is possible
create or replace function auth.has_permissions(
_target_user_id bigint
, _perm_codes text[]
, _tenant_id integer default 1
, _throw_err boolean default true) returns boolean
stable
language plpgsql
as
$$
declare
__perms text[];
__expiration_date timestamptz;
__last_used_provider_code text;
begin
if (_target_user_id = 1)
then
return true;
end if;
-- NOT REALLY SAFE FOR SOME INTERNAL/SYSTEM PERMISSIONS
if (auth.is_owner(_target_user_id, null, _tenant_id))
then
return true;
end if;
select permissions
, expiration_date
from auth.user_permission_cache upc
where upc.tenant_id = _tenant_id -- this was originally, either _tenant_id or 1, but from now on it's just _tenant_id
and user_id = _target_user_id
into __perms, __expiration_date;
if __expiration_date is null or __expiration_date <= now()
then
if not exists(
select
from auth.user_info ui
where ui.user_id = _target_user_id)
then
perform error.raise_52103(_target_user_id);
end if;
select last_used_provider_code
from auth.user_info
where user_id = _target_user_id
into __last_used_provider_code;
perform unsecure.recalculate_user_groups('permission_check'
, _target_user_id
, __last_used_provider_code
);
select __permissions
from unsecure.recalculate_user_permissions('permission_check', _target_user_id, _tenant_id)
into __perms;
end if;
if exists(
select
from unnest(__perms) p
inner join unnest(_perm_codes) rp on p = rp)
then
return true;
end if;
if (_throw_err)
then
perform add_journal_msg('system', _target_user_id
, format('User: (id: %s) has no permission: %s'
, _target_user_id, array_to_string(_perm_codes, '; '))
, 'perm', _target_user_id
, _event_id := 50003
, _tenant_id := _tenant_id);
perform
auth.throw_no_permission(_target_user_id, _perm_codes, _tenant_id);
end if;
return false;
end ;
$$;
-- recalculate permissions now returns permissions
-- - for specific tenant if _tenant_id is not null
-- - all tenants if it is null
drop function unsecure.recalculate_user_permissions(
_created_by text
, _target_user_id bigint
, _tenant_id int);
create or replace function unsecure.recalculate_user_permissions(
_created_by text
, _target_user_id bigint
, _tenant_id int default null)
returns table
(
__tenant_id integer,
__tenant_uuid uuid,
__groups text[],
__permissions text[]
)
language plpgsql
as
$$
declare
__perm_cache_timeout_in_s bigint;
__expiration_date timestamptz;
begin
if _tenant_id is not null and exists(
select
from auth.user_permission_cache
where tenant_id = _tenant_id
and user_id = _target_user_id
and expiration_date > now())
then
return query
select _tenant_id
, upc.tenant_uuid
, upc.groups
, upc.permissions
from auth.user_permission_cache upc
where upc.tenant_id = _tenant_id
and upc.user_id = _target_user_id;
else
select number_value
from const.sys_param sp
where sp.group_code = 'auth'
and sp.code = 'perm_cache_timeout_in_s'
into __perm_cache_timeout_in_s;
if
(__perm_cache_timeout_in_s is null)
then
__perm_cache_timeout_in_s := 300;
end if;
create temporary table __temp_users_groups_permissions
(
tenant_id integer,
tenant_uuid uuid,
group_codes text[],
permission_codes text[]
) on commit drop;
with ugs as (
select ugm.tenant_id
, t.uuid as tenant_uuid
, user_group_id
, group_code
from auth.user_group_members ugm
inner join auth.tenant t on t.tenant_id = ugm.tenant_id
where ugm.user_id = _target_user_id)
, group_assignments as (
select distinct pa.tenant_id
, ug.tenant_uuid
, ep.permission_code as full_code
from ugs ug
inner join auth.permission_assignment pa
on ug.user_group_id = pa.group_id
inner join auth.effective_permissions ep on pa.perm_set_id = ep.perm_set_id
where ep.perm_set_is_assignable = true
and ep.permission_is_assignable = true
union
select distinct pa.tenant_id
, ug.tenant_uuid
, sp.full_code
from ugs ug
inner join auth.permission_assignment pa
on ug.user_group_id = pa.group_id
inner join auth.permission p on pa.permission_id = p.permission_id
inner join auth.permission sp
on sp.node_path <@ p.node_path and sp.is_assignable = true)
, user_assignments as (
select distinct pa.tenant_id
, t.uuid as tenant_uuid
, ep.permission_code as full_code
from auth.permission_assignment pa
inner join auth.tenant t on pa.tenant_id = t.tenant_id
inner join auth.effective_permissions ep
on pa.perm_set_id = ep.perm_set_id
where pa.user_id = _target_user_id
and ep.perm_set_is_assignable = true
and ep.permission_is_assignable = true
union
select distinct pa.tenant_id
, t.uuid as tenant_uuid
, sp.full_code
from auth.permission_assignment pa
inner join auth.tenant t on pa.tenant_id = t.tenant_id
inner join auth.permission p
on pa.permission_id = p.permission_id
inner join auth.permission sp
on sp.node_path <@ p.node_path and sp.is_assignable = true
where pa.user_id = _target_user_id)
, user_permissions as (
select distinct ga.tenant_id
, ga.tenant_uuid
, ga.full_code
from group_assignments ga
union
select ua.tenant_id
, ua.tenant_uuid
, ua.full_code
from user_assignments ua
order by full_code)
insert
into __temp_users_groups_permissions(tenant_id, tenant_uuid, group_codes, permission_codes)
select data.tenant_id
, data.tenant_uuid
, coalesce(array_agg(distinct data.groups) filter ( where data.groups is not null ), array []::text[])
, coalesce(array_agg(distinct data.perms) filter ( where data.perms is not null ), array []::text[])
from (
select ug.tenant_id
, ug.tenant_uuid
, ug.group_code as groups
, null as perms
from ugs ug
union
select up.tenant_id
, up.tenant_uuid
, null
, up.full_code::text
from user_permissions up) data
group by data.tenant_id, data.tenant_uuid;
__expiration_date := now() + interval '1 second' * __perm_cache_timeout_in_s;
insert into auth.user_permission_cache (created_by, user_id, tenant_id, tenant_uuid, groups, permissions,
expiration_date)
select _created_by
, _target_user_id
, tugp.tenant_id
, tugp.tenant_uuid
, tugp.group_codes
, tugp.permission_codes
, __expiration_date
from __temp_users_groups_permissions tugp
on conflict (user_id, tenant_id )
do update
set modified = now()
, modified_by = _created_by
, groups = excluded.groups
, permissions = excluded.permissions
, expiration_date = __expiration_date;
return query
select ugp.tenant_id
, ugp.tenant_uuid
, ugp.group_codes
, ugp.permission_codes
from __temp_users_groups_permissions ugp
where _tenant_id is null
or ugp.tenant_id = _tenant_id
order by ugp.tenant_id;
end if;
end;
$$;
drop function auth.ensure_groups_and_permissions(
_created_by text
, _user_id bigint
, _target_user_id bigint
, _provider_code text
, _provider_groups text[]
, _provider_roles text[]
, _tenant_id integer);
create or replace function auth.ensure_groups_and_permissions(
_created_by text
, _user_id bigint
, _target_user_id bigint
, _provider_code text
, _provider_groups text[] default null::text[]
, _provider_roles text[] default null::text[])
returns TABLE
(
__tenant_id integer,
__tenant_uuid uuid,
__groups text[],
__permissions text[]
)
rows 1
language plpgsql
as
$$
begin
perform
auth.has_permission(_user_id, 'authentication.ensure_permissions');
update auth.user_identity
set modified_by = _created_by
, modified = now()
, provider_groups = _provider_groups
, provider_roles = _provider_roles
where provider_code = _provider_code
and user_id = _target_user_id;
create temporary table __temp_users_groups on commit drop as
select ug.__tenant_id as tenant_id
, ug.__user_group_id as user_group_id
, ug.__user_group_code as user_group_code
from unsecure.recalculate_user_groups(_created_by
, _target_user_id
, _provider_code
) ug;
-- return query
-- select null::integer,
-- null::uuid,
-- array []::text[],
-- array []::text[];
return query
select up.__tenant_id
, up.__tenant_uuid
, up.__groups
, up.__permissions
from unsecure.recalculate_user_permissions(_created_by
, _target_user_id, null) up;
end;
$$;
-- drop function auth.get_users_groups_and_permissions(
-- _requested_by text
-- , _user_id bigint
-- , _target_user_id bigint);
create function auth.get_users_groups_and_permissions(
_requested_by text
, _user_id bigint
, _target_user_id bigint)
returns TABLE
(
__tenant_id integer,
__tenant_uuid uuid,
__groups text[],
__permissions text[]
)
rows 1
language plpgsql
as
$$
begin
perform
auth.has_permission(_user_id, 'authentication.get_users_groups_and_permissions');