This repository was archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathrhn_channel.pkb
More file actions
1269 lines (1121 loc) · 46.3 KB
/
rhn_channel.pkb
File metadata and controls
1269 lines (1121 loc) · 46.3 KB
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
-- oracle equivalent source sha1 5bb6820eb0e542bdd8cf74d4555a5edc44aeac28
--
-- Copyright (c) 2008--2014 Red Hat, Inc.
--
-- This software is licensed to you under the GNU General Public License,
-- version 2 (GPLv2). There is NO WARRANTY for this software, express or
-- implied, including the implied warranties of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
-- along with this software; if not, see
-- http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
--
-- Red Hat trademarks are not licensed under GPLv2. No permission is
-- granted to use or replicate Red Hat trademarks that are incorporated
-- in this software or its documentation.
--
--
--
--
-- create schema rhn_channel;
--update pg_setting
update pg_settings set setting = 'rhn_channel,' || setting where name = 'search_path';
create or replace function obtain_read_lock(channel_family_id_in in numeric, org_id_in in numeric)
returns void as $$
declare
read_lock timestamptz;
begin
select created into read_lock
from rhnPrivateChannelFamily
where channel_family_id = channel_family_id_in and org_id = org_id_in
for update;
end$$ language plpgsql;
-- this "emulates" cursor server_base_subscriptions defined in oracle/rhn_channel.pks
create or replace function server_base_subscriptions(server_id_in NUMERIC)
returns boolean as $$
begin
return exists(SELECT C.id FROM rhnChannel C, rhnServerChannel SC
WHERE C.id = SC.channel_id
AND SC.server_id = server_id_in
AND C.parent_channel IS NULL);
end$$ language plpgsql;
-- this "emulates" cursor server_base_subscriptions defined in oracle/rhn_channel.pks
create or replace function check_server_subscription(server_id_in NUMERIC, channel_id_in NUMERIC)
returns boolean as $$
begin
return exists(SELECT channel_id FROM rhnServerChannel WHERE server_id = server_id_in AND channel_id = channel_id_in);
end$$ language plpgsql;
CREATE OR REPLACE FUNCTION subscribe_server(server_id_in IN NUMERIC, channel_id_in NUMERIC, immediate_in NUMERIC default 1, user_id_in in numeric default null, recalcfamily_in NUMERIC default 1) returns void
AS $$
declare
channel_parent_val rhnChannel.parent_channel%TYPE;
parent_subscribed BOOLEAN;
server_has_base_chan BOOLEAN;
server_already_in_chan BOOLEAN;
channel_family_id_val NUMERIC;
server_org_id_val NUMERIC;
available_subscriptions NUMERIC;
available_fve_subs numeric;
consenting_user NUMERIC;
allowed numeric;
is_fve_char char(1) := 'N';
BEGIN
if user_id_in is not null then
allowed := rhn_channel.user_role_check(channel_id_in, user_id_in, 'subscribe');
else
allowed := 1;
end if;
if allowed = 0 then
perform rhn_exception.raise_exception('no_subscribe_permissions');
end if;
SELECT parent_channel INTO channel_parent_val FROM rhnChannel WHERE id = channel_id_in;
IF channel_parent_val IS NOT NULL
THEN
-- child channel; if attempting to cross-subscribe a child to the wrong base, silently ignore
parent_subscribed := rhn_channel.check_server_subscription(server_id_in, channel_parent_val);
IF NOT parent_subscribed
THEN
RETURN;
END IF;
ELSE
-- base channel
server_has_base_chan := rhn_channel.server_base_subscriptions(server_id_in);
IF server_has_base_chan
THEN
perform rhn_exception.raise_exception('channel_server_one_base');
END IF;
END IF;
server_already_in_chan := rhn_channel.check_server_subscription(server_id_in, channel_id_in);
IF server_already_in_chan
THEN
RETURN;
END IF;
channel_family_id_val := rhn_channel.family_for_channel(channel_id_in);
IF channel_family_id_val IS NULL
THEN
perform rhn_exception.raise_exception('channel_subscribe_no_family');
END IF;
--
-- Use the org_id of the server only if the org_id of the channel = NULL.
-- This is required for subscribing to shared channels.
--
SELECT COALESCE(org_id, (SELECT org_id FROM rhnServer WHERE id = server_id_in))
INTO server_org_id_val
FROM rhnChannel
WHERE id = channel_id_in;
begin
perform rhn_channel.obtain_read_lock(channel_family_id_val, server_org_id_val);
exception
when no_data_found then
perform rhn_exception.raise_exception('channel_family_no_subscriptions');
end;
available_subscriptions := rhn_channel.available_family_subscriptions(channel_family_id_val, server_org_id_val);
available_fve_subs := rhn_channel.available_fve_family_subs(channel_family_id_val, server_org_id_val);
IF available_subscriptions IS NULL OR
available_subscriptions > 0 or
rhn_channel.can_server_consume_virt_channl(server_id_in, channel_family_id_val) = 1 OR
(available_fve_subs > 0 AND rhn_channel.can_server_consume_fve(server_id_in) = 1)
THEN
if rhn_channel.can_server_consume_virt_channl(server_id_in, channel_family_id_val) = 0 AND available_fve_subs > 0 AND rhn_channel.can_server_consume_fve(server_id_in) = 1 THEN
is_fve_char := 'Y';
end if;
insert into rhnServerHistory (id,server_id,summary,details) (
select nextval('rhn_event_id_seq'),
server_id_in,
'subscribed to channel ' || SUBSTR(c.label, 0, 106),
c.label
from rhnChannel c
where c.id = channel_id_in
);
INSERT INTO rhnServerChannel (server_id, channel_id, is_fve) VALUES (server_id_in, channel_id_in, is_fve_char);
IF recalcfamily_in > 0
THEN
perform rhn_channel.update_family_counts(channel_family_id_val, server_org_id_val);
END IF;
perform queue_server(server_id_in, immediate_in);
update rhnServer
set channels_changed = current_timestamp
where id = server_id_in;
ELSE
perform rhn_exception.raise_exception('channel_family_no_subscriptions');
END IF;
END$$ language plpgsql;
create or replace function can_convert_to_fve(server_id_in IN NUMERIC, channel_family_id_val IN NUMERIC)
RETURNS NUMERIC
as $$
declare
fve_convertible_entries cursor for
select 1 from
rhnServerFveCapable cap
where cap.server_id = server_id_in
AND cap.channel_family_id = channel_family_id_val;
BEGIN
FOR entry IN fve_convertible_entries LOOP
return 1;
END LOOP;
RETURN 0;
END$$ language plpgsql;
-- Converts server channel_family to use a flex entitlement
create or replace function convert_to_fve(server_id_in IN NUMERIC, channel_family_id_val IN NUMERIC)
returns void
as $$
declare
available_fve_subs NUMERIC;
server_org_id_val NUMERIC;
BEGIN
--
-- Use the org_id of the server only if the org_id of the channel = NULL.
-- This is required for subscribing to shared channels.
--
SELECT org_id
INTO server_org_id_val
FROM rhnServer
WHERE id = server_id_in;
perform rhn_channel.obtain_read_lock(channel_family_id_val, server_org_id_val);
if not found then
perform rhn_exception.raise_exception('channel_family_no_subscriptions');
end if;
IF (rhn_channel.can_convert_to_fve(server_id_in, channel_family_id_val ) = 0)
THEN
perform rhn_exception.raise_exception('server_cannot_convert_to_flex');
END IF;
available_fve_subs := rhn_channel.available_fve_family_subs(channel_family_id_val, server_org_id_val);
IF (available_fve_subs > 0)
THEN
insert into rhnServerHistory (id,server_id,summary,details) (
select nextval('rhn_event_id_seq'),
server_id_in,
'converted to flex entitlement' || SUBSTR(cf.label, 0, 99),
cf.label
from rhnChannelFamily cf
where cf.id = channel_family_id_val
);
UPDATE rhnServerChannel sc set is_fve = 'Y'
where sc.server_id = server_id_in and
sc.channel_id in
(select cfm.channel_id from rhnChannelFamilyMembers cfm
where cfm.CHANNEL_FAMILY_ID = channel_family_id_val);
perform rhn_channel.update_family_counts(channel_family_id_val, server_org_id_val);
ELSE
perform rhn_exception.raise_exception('not_enough_flex_entitlements');
END IF;
END$$ language plpgsql;
create or replace function can_server_consume_virt_channl(
server_id_in in numeric,
family_id_in in numeric )
returns numeric
as $$
begin
if exists(
select 1
from
rhnChannelFamilyVirtSubLevel cfvsl,
rhnSGTypeVirtSubLevel sgtvsl,
rhnVirtualInstance vi
where
vi.virtual_system_id = server_id_in
and sgtvsl.virt_sub_level_id = cfvsl.virt_sub_level_id
and cfvsl.channel_family_id = family_id_in
and exists (
select 1
from rhnServerEntitlementView sev
where vi.host_system_id = sev.server_id
and sev.server_group_type_id = sgtvsl.server_group_type_id ))
then
return 1;
else
return 0;
end if;
end$$ language plpgsql;
create or replace function can_server_consume_fve(server_id_in in numeric) returns numeric
as $$
declare
vi_entries cursor for
SELECT 1
FROM rhnVirtualInstance vi
WHERE vi.virtual_system_id = server_id_in;
vi_count numeric;
begin
FOR vi_entry IN VI_ENTRIES LOOP
return 1;
END LOOP;
RETURN 0;
end$$ language plpgsql;
create or replace function guess_server_base(
server_id_in in numeric
) RETURNS numeric as $$
declare
server_cursor cursor for
select s.server_arch_id, s.release, s.org_id
from rhnServer s
where s.id = server_id_in;
-- Cursor that fetches all the possible base channels for a
-- (server_arch_id, release, org_id) combination
base_channel_cursor cursor(
release_in varchar,
server_arch_id_in numeric,
org_id_in numeric
) for
select distinct c.*
from rhnOrgDistChannelMap odcm,
rhnServerChannelArchCompat scac,
rhnChannel c
where c.parent_channel is null
and c.id = odcm.channel_id
and c.channel_arch_id = odcm.channel_arch_id
and odcm.release = release_in
and odcm.for_org_id = org_id_in
and scac.server_arch_id = server_arch_id_in
and scac.channel_arch_id = c.channel_arch_id;
begin
for s in server_cursor loop
for channel in base_channel_cursor(s.release,
s.server_arch_id, s.org_id)
loop
return channel.id;
end loop;
end loop;
-- Server not found, or no base channel applies to it
return null;
end$$ language plpgsql;
-- Private function
create or replace function normalize_server_arch(server_arch_in in varchar)
returns varchar
as $$
declare
suffix VARCHAR(128) := '-redhat-linux';
begin
if server_arch_in is NULL then
return NULL;
end if;
if position('-' IN server_arch_in) > 0
then
-- Suffix already present
return server_arch_in;
end if;
return server_arch_in || suffix;
end$$ language plpgsql;
--
-- Raises:
-- server_arch_not_found
-- no_subscribe_permissions
create or replace function base_channel_for_release_arch(
release_in in varchar,
server_arch_in in varchar,
org_id_in in numeric default -1,
user_id_in in numeric default null
) returns numeric as $$
declare
server_arch varchar(256) := rhn_channel.normalize_server_arch(server_arch_in);
server_arch_id numeric;
begin
-- Look up the server arch
select id
into server_arch_id
from rhnServerArch
where label = server_arch;
if not found then
perform rhn_exception.raise_exception('server_arch_not_found');
end if;
return rhn_channel.base_channel_rel_archid(release_in, server_arch_id,
org_id_in, user_id_in);
end$$ language plpgsql;
create or replace function base_channel_rel_archid(
release_in in varchar,
server_arch_id_in in numeric,
org_id_in in numeric default -1,
user_id_in in numeric default null
) returns numeric as $$
declare
denied_channel_id numeric := null;
valid_org_id numeric := org_id_in;
valid_user_id numeric := user_id_in;
channel_subscribable numeric;
-- Cursor that fetches all the possible base channels for a
-- (server_arch_id, release, org_id) combination
base_channel_cursor cursor(
release_in varchar,
server_arch_id_in numeric,
org_id_in numeric
) for
select distinct c.*
from rhnOrgDistChannelMap odcm,
rhnServerChannelArchCompat scac,
rhnChannel c
where c.parent_channel is null
and c.id = odcm.channel_id
and c.channel_arch_id = odcm.channel_arch_id
and odcm.release = release_in
and odcm.for_org_id = org_id_in
and scac.server_arch_id = server_arch_id_in
and scac.channel_arch_id = c.channel_arch_id;
begin
if org_id_in = -1 and user_id_in is not null then
-- Get the org id from the user id
select org_id
into valid_org_id
from web_contact
where id = user_id_in;
if not found then
-- User doesn't exist
-- XXX Only list public stuff for now
valid_user_id := null;
valid_org_id := -1;
end if;
end if;
for c in base_channel_cursor(release_in, server_arch_id_in, valid_org_id)
loop
-- This row is a possible match
if valid_user_id is null then
-- User ID not specified, so no user to channel permissions to
-- check
return c.id;
end if;
-- Check user to channel permissions
select rhn_channel.loose_user_role_check(c.id, user_id_in, 'subscribe')
into channel_subscribable;
if channel_subscribable = 1 then
return c.id;
end if;
-- Base channel exists, but is not subscribable; keep trying
denied_channel_id := c.id;
end loop;
if denied_channel_id is not null then
perform rhn_exception.raise_exception('no_subscribe_permissions');
end if;
-- No base channel applies
return NULL;
end$$ language plpgsql;
CREATE OR REPLACE FUNCTION clear_subscriptions(server_id_in IN NUMERIC, deleting_server IN NUMERIC default 0,
update_family_countsYN IN NUMERIC default 1 ) returns void
AS $$
declare
server_channels cursor(server_id_in numeric) for
select s.org_id, sc.channel_id, cfm.channel_family_id
from rhnServer s,
rhnServerChannel sc,
rhnChannelFamilyMembers cfm
where s.id = server_id_in
and s.id = sc.server_id
and sc.channel_id = cfm.channel_id
order by cfm.channel_family_id;
last_channel_family_id rhnChannelFamilyMembers.channel_family_id%type := -1;
last_channel_org_id rhnServer.org_id%type := -1;
BEGIN
for channel in server_channels(server_id_in)
loop
perform rhn_channel.unsubscribe_server(server_id_in, channel.channel_id, 1, 1, deleting_server, 0);
if update_family_countsYN > 0
and channel.channel_family_id != last_channel_family_id then
-- update family counts only once
-- after all channels with same family has been fetched
if last_channel_family_id != -1 then
perform rhn_channel.update_family_counts(channel.channel_family_id, channel.org_id);
end if;
last_channel_family_id := channel.channel_family_id;
last_channel_org_id := channel.org_id;
end if;
end loop;
if update_family_countsYN > 0 and last_channel_family_id != -1 then
-- update the last family fetched
perform rhn_channel.update_family_counts(last_channel_family_id, last_channel_org_id);
end if;
END$$ language plpgsql;
CREATE OR REPLACE FUNCTION unsubscribe_server(server_id_in IN NUMERIC, channel_id_in NUMERIC, immediate_in NUMERIC default 1, unsubscribe_children_in numeric default 0,
deleting_server in numeric default 0,
update_family_countsYN in numeric default 1) returns void
AS $$
declare
channel_family_id_val NUMERIC;
server_org_id_val NUMERIC;
available_subscriptions NUMERIC;
server_already_in_chan BOOLEAN;
channel_family_is_proxy cursor(channel_family_id_in numeric) for
select 1
from rhnChannelFamily
where id = channel_family_id_in
and label = 'rhn-proxy';
channel_family_is_satellite cursor(channel_family_id_in numeric) for
select 1
from rhnChannelFamily
where id = channel_family_id_in
and label = 'rhn-satellite';
child record;
BEGIN
-- In PostgreSQL recursion with opened cursors is not allowed so we use
-- FOR IN SELECT form which is ok.
FOR child IN select c.id
from rhnChannel c,
rhnServerChannel sc
where 1=1
and c.parent_channel = channel_id_in
and c.id = sc.channel_id
and sc.server_id = server_id_in
LOOP
if unsubscribe_children_in = 1 then
perform rhn_channel.unsubscribe_server(server_id_in,
child.id,
immediate_in,
unsubscribe_children_in,
deleting_server,
update_family_countsYN);
else
perform rhn_exception.raise_exception('channel_unsubscribe_child_exists');
end if;
END LOOP;
server_already_in_chan := rhn_channel.check_server_subscription(server_id_in, channel_id_in);
IF NOT server_already_in_chan
THEN
RETURN;
END IF;
if deleting_server = 0 then
insert into rhnServerHistory (id,server_id,summary,details) (
select nextval('rhn_event_id_seq'),
server_id_in,
'unsubscribed from channel ' || SUBSTR(c.label, 0, 106),
c.label
from rhnChannel c
where c.id = channel_id_in
);
end if;
DELETE FROM rhnServerChannel WHERE server_id = server_id_in AND channel_id = channel_id_in;
if deleting_server = 0 then
perform queue_server(server_id_in, immediate_in);
update rhnServer
set channels_changed = current_timestamp
where id = server_id_in;
end if;
channel_family_id_val := rhn_channel.family_for_channel(channel_id_in);
IF channel_family_id_val IS NULL
THEN
perform rhn_exception.raise_exception('channel_unsubscribe_no_family');
END IF;
for ignore in channel_family_is_satellite(channel_family_id_val) loop
delete from rhnSatelliteInfo where server_id = server_id_in;
end loop;
for ignore in channel_family_is_proxy(channel_family_id_val) loop
delete from rhnProxyInfo where server_id = server_id_in;
end loop;
SELECT org_id INTO server_org_id_val
FROM rhnServer
WHERE id = server_id_in;
if update_family_countsYN = 1 then
perform rhn_channel.update_family_counts(channel_family_id_val, server_org_id_val);
end if;
END$$ language plpgsql;
CREATE OR REPLACE FUNCTION family_for_channel(channel_id_in IN NUMERIC)
RETURNS NUMERIC
AS $$
declare
channel_family_id_val NUMERIC;
BEGIN
SELECT channel_family_id INTO channel_family_id_val
FROM rhnChannelFamilyMembers
WHERE channel_id = channel_id_in;
IF NOT FOUND THEN
RETURN NULL;
END IF;
RETURN channel_family_id_val;
END$$ language plpgsql;
CREATE OR REPLACE FUNCTION available_family_subscriptions(channel_family_id_in IN NUMERIC, org_id_in IN NUMERIC)
RETURNS NUMERIC
AS $$
declare
cfp record;
current_members_val NUMERIC;
max_members_val NUMERIC;
found NUMERIC;
BEGIN
for cfp in SELECT * FROM rhnOrgChannelFamilyPermissions
WHERE channel_family_id = channel_family_id_in
AND org_id = org_id_in
LOOP
found := 1;
current_members_val := cfp.current_members;
max_members_val := cfp.max_members;
END LOOP;
-- not found: either the channel fam doesn't have an entry in cfp, or the org doesn't have access to it.
-- either way, there are no available subscriptions
IF found IS NULL
THEN
RETURN 0;
END IF;
-- null max members? in that case, pass it on; NULL means infinite
IF max_members_val IS NULL
THEN
RETURN NULL;
END IF;
-- otherwise, return the delta
RETURN max_members_val - current_members_val;
END$$ language plpgsql;
CREATE OR REPLACE FUNCTION available_fve_family_subs(channel_family_id_in IN NUMERIC, org_id_in IN NUMERIC)
RETURNS NUMERIC
AS $$
declare
cfp record;
fve_current_members_val NUMERIC;
fve_max_members_val NUMERIC;
found NUMERIC;
BEGIN
for cfp in SELECT * FROM rhnOrgChannelFamilyPermissions
WHERE channel_family_id = channel_family_id_in
AND org_id = org_id_in
LOOP
found := 1;
fve_current_members_val := cfp.fve_current_members;
fve_max_members_val := cfp.fve_max_members;
END LOOP;
-- not found: either the channel fam doesn't have an entry in cfp, or the org doesn't have access to it.
-- either way, there are no available subscriptions
IF found IS NULL
THEN
RETURN 0;
END IF;
-- null max members? in that case, pass it on; NULL means infinite
IF fve_max_members_val IS NULL
THEN
RETURN NULL;
END IF;
-- otherwise, return the delta
RETURN fve_max_members_val - fve_current_members_val;
END$$ language plpgsql;
-- *******************************************************************
-- FUNCTION: channel_family_current_members
-- Calculates and returns the actual count of systems consuming
-- physical channel subscriptions.
-- Called by: update_family_counts
-- rhn_entitlements.repoll_virt_guest_entitlements
-- *******************************************************************
create or replace function channel_family_current_members(channel_family_id_in IN NUMERIC,
org_id_in IN NUMERIC)
returns numeric
as $$
declare
current_members_count numeric := 0;
begin
select count(distinct server_id)
into current_members_count
from rhnChannelFamilyServerPhysical cfsp
where cfsp.channel_family_id = channel_family_id_in
and cfsp.customer_id = org_id_in;
return current_members_count;
end$$ language plpgsql;
create or replace function cfam_curr_fve_members(
channel_family_id_in IN NUMERIC,
org_id_in IN NUMERIC)
returns numeric
as $$
declare
current_members_count numeric := 0;
begin
select count(distinct sc.server_id)
into current_members_count
from rhnServerChannel sc,
rhnChannelFamilyMembers cfm,
rhnServer s
where s.org_id = org_id_in
and s.id = sc.server_id
and cfm.channel_family_id = channel_family_id_in
and cfm.channel_id = sc.channel_id
and exists (
select 1
from rhnChannelFamilyServerFve cfsp
where cfsp.CHANNEL_FAMILY_ID = channel_family_id_in
and cfsp.server_id = s.id
);
return current_members_count;
end$$ language plpgsql;
CREATE OR REPLACE FUNCTION update_family_counts(channel_family_id_in IN NUMERIC,
org_id_in IN NUMERIC) returns void
AS $$
BEGIN
update rhnPrivateChannelFamily
set current_members = rhn_channel.channel_family_current_members(channel_family_id_in, org_id_in),
fve_current_members = rhn_channel.cfam_curr_fve_members(channel_family_id_in,org_id_in)
where org_id = org_id_in
and channel_family_id = channel_family_id_in;
END$$ language plpgsql;
create or replace function update_group_family_counts(group_label_in IN VARCHAR,
org_id_in IN NUMERIC)
returns void
as $$
declare
i record;
BEGIN
FOR i IN (
SELECT DISTINCT CFM.channel_family_id, SG.org_id
FROM rhnChannelFamilyMembers CFM
JOIN rhnServerChannel SC
ON SC.channel_id = CFM.channel_id
JOIN rhnServerGroupMembers SGM
ON SC.server_id = SGM.server_id
JOIN rhnServerGroup SG
ON SGM.server_group_id = SG.id
JOIN rhnServerGroupType SGT
ON SG.group_type = SGT.id
WHERE SGT.label = group_label_in
AND SG.org_id = org_id_in
AND SGT.is_base = 'Y'
) LOOP
perform rhn_channel.update_family_counts(i.channel_family_id, i.org_id);
END LOOP;
END$$ language plpgsql;
CREATE OR REPLACE FUNCTION available_chan_subscriptions(channel_id_in IN NUMERIC,
org_id_in IN NUMERIC)
RETURNS NUMERIC
AS $$
declare
channel_family_id_val NUMERIC;
BEGIN
SELECT channel_family_id INTO STRICT channel_family_id_val
FROM rhnChannelFamilyMembers
WHERE channel_id = channel_id_in;
RETURN rhn_channel.available_family_subscriptions(
channel_family_id_val, org_id_in);
END$$ language plpgsql;
CREATE OR REPLACE FUNCTION available_fve_chan_subs(channel_id_in IN NUMERIC,
org_id_in IN NUMERIC)
RETURNS NUMERIC
AS $$
declare
channel_family_id_val NUMERIC;
BEGIN
SELECT channel_family_id INTO STRICT channel_family_id_val
FROM rhnChannelFamilyMembers
WHERE channel_id = channel_id_in;
RETURN rhn_channel.available_fve_family_subs(
channel_family_id_val, org_id_in);
END$$ language plpgsql;
create or replace function unsubscribe_server_from_family(server_id_in in numeric,
channel_family_id_in in numeric)
returns void
as $$
begin
delete
from rhnServerChannel rsc
where rsc.server_id = server_id_in
and channel_id in (
select rcfm.channel_id
from rhnChannelFamilyMembers rcfm
where rcfm.channel_family_id = channel_family_id_in);
end$$ language plpgsql;
create or replace function get_org_id(channel_id_in in numeric)
returns numeric
as $$
declare
org_id_out numeric;
begin
select org_id into strict org_id_out
from rhnChannel
where id = channel_id_in;
return org_id_out;
end$$ language plpgsql;
create or replace function get_cfam_org_access(cfam_id_in in numeric, org_id_in in numeric)
returns numeric
as $$
begin
if exists(
select 1
from rhnOrgChannelFamilyPermissions cfp
where cfp.org_id = org_id_in
) then
return 1;
else
return 0;
end if;
end$$ language plpgsql;
create or replace function get_org_access(channel_id_in in numeric, org_id_in in numeric)
returns integer
as $$
begin
-- the idea: if we get past this query,
-- the org has access to the channel, else not
if exists(
select 1
from rhnChannelFamilyMembers CFM,
rhnOrgChannelFamilyPermissions CFP
where cfp.org_id = org_id_in
and CFM.channel_family_id = CFP.channel_family_id
and CFM.channel_id = channel_id_in
and (CFP.max_members > 0 or CFP.max_members is null or CFP.fve_max_members > 0 or CFP.fve_max_members is null or CFP.org_id = 1) )
then
return 1;
else
return 0;
end if;
end$$ language plpgsql;
-- check if a user has a given role, or if such a role is inferrable
-- returns NULL if OK, error message otherwise
create or replace function user_role_check_debug(channel_id_in in numeric,
user_id_in in numeric,
role_in in varchar)
returns varchar
as $$
declare
org_id numeric;
begin
org_id := rhn_user.get_org_id(user_id_in);
-- channel might be shared
if role_in = 'subscribe' and
rhn_channel.shared_user_role_check(channel_id_in, user_id_in, role_in) = 1 then
return NULL;
end if;
if role_in = 'manage' and
COALESCE(rhn_channel.get_org_id(channel_id_in), -1) <> org_id then
return 'channel_not_owned';
end if;
if role_in = 'subscribe' and
rhn_channel.get_org_access(channel_id_in, org_id) = 0 then
return 'channel_not_available';
end if;
-- channel admins have all roles
if rhn_user.check_role_implied(user_id_in, 'channel_admin') = 1 then
return NULL;
end if;
-- the subscribe permission is inferred
-- UNLESS the not_globally_subscribable flag is set
if role_in = 'subscribe'
then
if rhn_channel.org_channel_setting(channel_id_in,
org_id,
'not_globally_subscribable') = 0 then
return NULL;
end if;
end if;
-- all other roles (manage right now) are explicitly granted
if rhn_channel.direct_user_role_check(channel_id_in,
user_id_in, role_in) = 1 then
return NULL;
end if;
return 'direct_permission';
end$$ language plpgsql;
-- same as above, but with 1/0 output; useful in views, etc
create or replace function user_role_check(channel_id_in in numeric, user_id_in in numeric, role_in in varchar)
returns numeric
as $$
begin
if rhn_channel.user_role_check_debug(channel_id_in,
user_id_in, role_in) is NULL then
return 1;
else
return 0;
end if;
end$$ language plpgsql;
--
-- For multiorg phase II, this function simply checks to see if the user's
-- has a trust relationship that includes this channel by id.
--
create or replace function shared_user_role_check(channel_id in numeric, user_id in numeric, role in varchar)
returns numeric
as $$
declare
n numeric;
oid numeric;
begin
oid := rhn_user.get_org_id(user_id);
select 1 into n
from rhnSharedChannelView s
where s.id = channel_id and s.org_trust_id = oid;
if not found then
return 0;
end if;
return 1;
end$$ language plpgsql;
-- same as above, but returns 1 if user_id_in is null
-- This is useful in queries where user_id is not specified
create or replace function loose_user_role_check(channel_id_in in numeric, user_id_in in numeric, role_in in varchar)
returns numeric
as $$
begin
if user_id_in is null then
return 1;
end if;
return rhn_channel.user_role_check(channel_id_in, user_id_in, role_in);
end$$ language plpgsql;
-- directly checks the table, no inferred permissions
create or replace function direct_user_role_check(channel_id_in in numeric, user_id_in in numeric, role_in in varchar)
returns numeric
as $$
declare
throwaway numeric;
begin
-- the idea: if we get past this query, the user has the role, else no
select 1 into throwaway
from rhnChannelPermissionRole CPR,
rhnChannelPermission CP
where CP.user_id = user_id_in
and CP.channel_id = channel_id_in
and CPR.label = role_in
and CP.role_id = CPR.id;
if not found then
return 0;
end if;
return 1;
end$$ language plpgsql;
-- check if an org has a certain setting
create or replace function org_channel_setting(channel_id_in in numeric, org_id_in in numeric, setting_in in varchar)
returns numeric
as $$
declare
throwaway numeric;
begin
-- the idea: if we get past this query, the org has the setting
select 1 into throwaway
from rhnOrgChannelSettingsType OCST,
rhnOrgChannelSettings OCS
where OCS.org_id = org_id_in
and OCS.channel_id = channel_id_in
and OCST.label = setting_in
and OCS.setting_id = OCST.id;
if not found then
return 0;
end if;
return 1;
end$$ language plpgsql;
CREATE OR REPLACE FUNCTION channel_priority(channel_id_in IN numeric)
RETURNS numeric
AS $$
declare
channel_name varchar(256);
priority numeric;
end_of_life_val timestamptz;
org_id_val numeric;
BEGIN
select name, end_of_life, org_id