17755: Merge branch 'main' into 17755-add-singularity-to-compute-image
[arvados.git] / services / api / lib / create_permission_view.sql
1 -- Copyright (C) The Arvados Authors. All rights reserved.
2 --
3 -- SPDX-License-Identifier: AGPL-3.0
4
5 -- Note: this is not the current code used for permission checks (that is
6 -- materialized_permission_view), but is retained here for migration purposes.
7
8 CREATE TEMPORARY VIEW permission_view AS
9 WITH RECURSIVE
10 perm_value (name, val) AS (
11      VALUES
12      ('can_read',   1::smallint),
13      ('can_login',  1),
14      ('can_write',  2),
15      ('can_manage', 3)
16      ),
17 perm_edges (tail_uuid, head_uuid, val, follow) AS (
18        SELECT links.tail_uuid,
19               links.head_uuid,
20               pv.val,
21               (pv.val = 3 OR groups.uuid IS NOT NULL) AS follow
22               FROM links
23               LEFT JOIN perm_value pv ON pv.name = links.name
24               LEFT JOIN groups ON pv.val<3 AND groups.uuid = links.head_uuid
25               WHERE links.link_class = 'permission'
26        UNION ALL
27        SELECT owner_uuid, uuid, 3, true FROM groups
28        ),
29 perm (val, follow, user_uuid, target_uuid) AS (
30      SELECT 3::smallint             AS val,
31             true                    AS follow,
32             users.uuid::varchar(32) AS user_uuid,
33             users.uuid::varchar(32) AS target_uuid
34             FROM users
35      UNION
36      SELECT LEAST(perm.val, edges.val)::smallint AS val,
37             edges.follow                         AS follow,
38             perm.user_uuid::varchar(32)          AS user_uuid,
39             edges.head_uuid::varchar(32)         AS target_uuid
40             FROM perm
41             INNER JOIN perm_edges edges
42             ON perm.follow AND edges.tail_uuid = perm.target_uuid
43 )
44 SELECT user_uuid,
45        target_uuid,
46        val AS perm_level,
47        CASE follow WHEN true THEN target_uuid ELSE NULL END AS target_owner_uuid
48        FROM perm;