8784: Fix test for latest firefox.
[arvados.git] / services / api / lib / create_permission_view.sql
1 CREATE TEMPORARY VIEW permission_view AS
2 WITH RECURSIVE
3 perm_value (name, val) AS (
4      VALUES
5      ('can_read',   1::smallint),
6      ('can_login',  1),
7      ('can_write',  2),
8      ('can_manage', 3)
9      ),
10 perm_edges (tail_uuid, head_uuid, val, follow) AS (
11        SELECT links.tail_uuid,
12               links.head_uuid,
13               pv.val,
14               (pv.val = 3 OR groups.uuid IS NOT NULL) AS follow
15               FROM links
16               LEFT JOIN perm_value pv ON pv.name = links.name
17               LEFT JOIN groups ON pv.val<3 AND groups.uuid = links.head_uuid
18               WHERE links.link_class = 'permission'
19        UNION ALL
20        SELECT owner_uuid, uuid, 3, true FROM groups
21        ),
22 perm (val, follow, user_uuid, target_uuid) AS (
23      SELECT 3::smallint             AS val,
24             true                    AS follow,
25             users.uuid::varchar(32) AS user_uuid,
26             users.uuid::varchar(32) AS target_uuid
27             FROM users
28      UNION
29      SELECT LEAST(perm.val, edges.val)::smallint AS val,
30             edges.follow                         AS follow,
31             perm.user_uuid::varchar(32)          AS user_uuid,
32             edges.head_uuid::varchar(32)         AS target_uuid
33             FROM perm
34             INNER JOIN perm_edges edges
35             ON perm.follow AND edges.tail_uuid = perm.target_uuid
36 )
37 SELECT user_uuid,
38        target_uuid,
39        val AS perm_level,
40        CASE follow WHEN true THEN target_uuid ELSE NULL END AS target_owner_uuid
41        FROM perm;