summaryrefslogtreecommitdiff
path: root/sql/functions/auth.get_all_role_groups_for_user.sql
blob: f8c111d306ddedc420bacb97e72c3c96dab0ce47 (plain)
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
drop function auth.get_all_role_groups_for_user (
    p_user_id bigint
);

create or replace function auth.get_all_role_groups_for_user (
    p_user_id bigint
)
returns setof auth.role_groups_t
as $$
begin
    if auth.superuser_p(p_user_id) = true::bool then
        return query
        select null::bigint as user_role_group_id,
            p_user_id as user_id,
            id as role_group_id,
            name,
            description
        from auth.role_groups
        order by name asc;
    else
        return query
        select urg.id as user_role_group_id,
            urg.user_id,
            rg.id as role_group_id,
            rg.name,
            rg.description
        from auth.users_role_groups urg
        inner join auth.role_groups rg
            on urg.role_group_id = rg.id
        where urg.user_id = p_user_id
        order by rg.name asc;
    end if;

    return;
end;
$$ language plpgsql;