16007: Account for all the edges. still wip.
[arvados.git] / services / api / db / migrate / 20200501150153_permission_table.rb
1 class PermissionTable < ActiveRecord::Migration[5.0]
2   def up
3     create_table :materialized_permissions, :id => false do |t|
4       t.string :user_uuid
5       t.string :target_uuid
6       t.integer :perm_level
7       t.boolean :traverse_owned
8     end
9     add_index :materialized_permissions, [:user_uuid, :target_uuid], unique: true, name: 'permission_user_target'
10
11     ActiveRecord::Base.connection.execute %{
12 create or replace function project_subtree (starting_uuid varchar(27))
13 returns table (target_uuid varchar(27))
14 STABLE
15 language SQL
16 as $$
17 WITH RECURSIVE
18         project_subtree(uuid) as (
19         values (starting_uuid)
20         union
21         select groups.uuid from groups join project_subtree on (groups.owner_uuid = project_subtree.uuid)
22         )
23         select uuid from project_subtree;
24 $$;
25 }
26
27     ActiveRecord::Base.connection.execute %{
28 create or replace function project_subtree_with_trash_at (starting_uuid varchar(27), starting_trash_at timestamp)
29 returns table (target_uuid varchar(27), trash_at timestamp)
30 STABLE
31 language SQL
32 as $$
33 WITH RECURSIVE
34         project_subtree(uuid, trash_at) as (
35         values (starting_uuid, starting_trash_at)
36         union
37         select groups.uuid, LEAST(project_subtree.trash_at, groups.trash_at)
38           from groups join project_subtree on (groups.owner_uuid = project_subtree.uuid)
39         )
40         select uuid, trash_at from project_subtree;
41 $$;
42 }
43
44     create_table :trashed_groups, :id => false do |t|
45       t.string :group_uuid
46       t.datetime :trash_at
47     end
48     add_index :trashed_groups, :group_uuid, :unique => true
49
50         ActiveRecord::Base.connection.execute %{
51 create or replace function compute_trashed ()
52 returns table (uuid varchar(27), trash_at timestamp)
53 STABLE
54 language SQL
55 as $$
56 select ps.target_uuid as group_uuid, ps.trash_at from groups,
57   lateral project_subtree_with_trash_at(groups.uuid, groups.trash_at) ps
58   where groups.owner_uuid like '_____-tpzed-_______________'
59 $$;
60 }
61
62         ActiveRecord::Base.connection.execute("INSERT INTO trashed_groups select * from compute_trashed()")
63
64         ActiveRecord::Base.connection.execute %{
65 create or replace function should_traverse_owned (starting_uuid varchar(27),
66                                                   starting_perm integer)
67   returns bool
68 STABLE
69 language SQL
70 as $$
71 select starting_uuid like '_____-j7d0g-_______________' or
72        (starting_uuid like '_____-tpzed-_______________' and starting_perm >= 3);
73 $$;
74 }
75
76
77         ActiveRecord::Base.connection.execute %{
78 create or replace function permission_graph_edges ()
79   returns table (tail_uuid varchar(27), head_uuid varchar(27), val integer)
80 STABLE
81 language SQL
82 as $$
83            select groups.owner_uuid, groups.uuid, (3) from groups
84           union
85             select users.owner_uuid, users.uuid, (3) from users
86           union
87             select links.tail_uuid,
88                    links.head_uuid,
89                    CASE
90                      WHEN links.name = 'can_read'   THEN 1
91                      WHEN links.name = 'can_login'  THEN 1
92                      WHEN links.name = 'can_write'  THEN 2
93                      WHEN links.name = 'can_manage' THEN 3
94                    END as val
95           from links
96           where links.link_class='permission'
97 $$;
98 }
99
100         # Get a set of permission by searching the graph and following
101         # ownership and permission links.
102         #
103         # edges() - a subselect with the union of ownership and permission links
104         #
105         # traverse_graph() - recursive query, from the starting node,
106         # self-join with edges to find outgoing permissions.
107         # Re-runs the query on new rows until there are no more results.
108         # This accomplishes a breadth-first search of the permission graph.
109         #
110         ActiveRecord::Base.connection.execute %{
111 create or replace function search_permission_graph (starting_uuid varchar(27),
112                                                     starting_perm integer)
113   returns table (target_uuid varchar(27), val integer, traverse_owned bool)
114 STABLE
115 language SQL
116 as $$
117 WITH RECURSIVE edges(tail_uuid, head_uuid, val) as (
118           select * from permission_graph_edges()
119         ),
120         traverse_graph(target_uuid, val, traverse_owned) as (
121             values (starting_uuid, starting_perm,
122                     should_traverse_owned(starting_uuid, starting_perm))
123           union
124             (select edges.head_uuid,
125                     least(edges.val, traverse_graph.val,
126                           case traverse_graph.traverse_owned
127                             when true then null
128                             else 0
129                           end),
130                     should_traverse_owned(edges.head_uuid, edges.val)
131              from edges
132              join traverse_graph on (traverse_graph.target_uuid = edges.tail_uuid)))
133         select target_uuid, max(val), bool_or(traverse_owned) from traverse_graph
134         group by (target_uuid) ;
135 $$;
136 }
137
138
139   # owned_by_user_perms(perm_origin_uuid, target_uuid, val, traverse_owned) as (
140   #   select users.owner_uuid as perm_origin_uuid, u.target_uuid, u.val, u.traverse_owned
141   #     from users, lateral search_permission_graph(users.uuid, 3) as u
142   #     where users.owner_uuid not in (select target_uuid from perm_from_start) and
143   #           users.uuid in (select target_uuid from perm_from_start)
144   # ),
145
146   # owned_by_group_perms(perm_origin_uuid, target_uuid, val, traverse_owned) as (
147   #   select groups.owner_uuid as perm_origin_uuid, groups.uuid, 3, true
148   #     from groups
149   #     where groups.owner_uuid not in (select target_uuid from perm_from_start) and
150   #           groups.uuid in (select target_uuid from perm_from_start)
151   # ),
152
153
154         ActiveRecord::Base.connection.execute %{
155 create or replace function compute_permission_subgraph (perm_origin_uuid varchar(27),
156                                                         starting_uuid varchar(27),
157                                                         starting_perm integer)
158 returns table (user_uuid varchar(27), target_uuid varchar(27), val integer, traverse_owned bool)
159 STABLE
160 language SQL
161 as $$
162 with
163 perm_from_start(perm_origin_uuid, target_uuid, val, traverse_owned) as (
164   select perm_origin_uuid, target_uuid, val, traverse_owned
165     from search_permission_graph(starting_uuid, starting_perm)),
166
167   edges(tail_uuid, head_uuid, val) as (
168         select * from permission_graph_edges()),
169
170   additional_perms(perm_origin_uuid, target_uuid, val, traverse_owned) as (
171     select edges.tail_uuid as perm_origin_uuid, ps.target_uuid, ps.val,
172            should_traverse_owned(ps.target_uuid, ps.val)
173       from edges, lateral search_permission_graph(edges.head_uuid, edges.val) as ps
174       where (not (edges.tail_uuid = perm_origin_uuid and
175                  edges.head_uuid = starting_uuid and
176                  edges.val = starting_perm)) and
177             edges.tail_uuid not in (select target_uuid from perm_from_start) and
178             edges.head_uuid in (select target_uuid from perm_from_start)),
179
180   partial_perms(perm_origin_uuid, target_uuid, val, traverse_owned) as (
181       select * from perm_from_start
182     union
183       select * from additional_perms
184   ),
185
186   user_identity_perms(perm_origin_uuid, target_uuid, val, traverse_owned) as (
187     select users.uuid as perm_origin_uuid, ps.target_uuid, ps.val, ps.traverse_owned
188       from users, lateral search_permission_graph(users.uuid, 3) as ps
189       where users.owner_uuid not in (select target_uuid from partial_perms where traverse_owned) and
190       users.uuid in (select target_uuid from partial_perms)
191   ),
192
193   all_perms(perm_origin_uuid, target_uuid, val, traverse_owned) as (
194       select * from partial_perms
195     union
196       select * from user_identity_perms
197   )
198
199   select v.user_uuid, v.target_uuid, max(v.perm_level), bool_or(v.traverse_owned) from
200     (select materialized_permissions.user_uuid,
201          u.target_uuid,
202          least(u.val, materialized_permissions.perm_level) as perm_level,
203          u.traverse_owned
204       from all_perms as u
205       join materialized_permissions on (u.perm_origin_uuid = materialized_permissions.target_uuid)
206       where materialized_permissions.traverse_owned
207     union
208       select perm_origin_uuid as user_uuid, target_uuid, val as perm_level, traverse_owned
209         from all_perms
210         where perm_origin_uuid like '_____-tpzed-_______________') as v
211     group by v.user_uuid, v.target_uuid
212 $$;
213      }
214
215     ActiveRecord::Base.connection.execute "DROP MATERIALIZED VIEW IF EXISTS materialized_permission_view;"
216
217   end
218   def down
219     drop_table :materialized_permissions
220     drop_table :trashed_groups
221
222     ActiveRecord::Base.connection.execute "DROP function project_subtree (varchar);"
223     ActiveRecord::Base.connection.execute "DROP function project_subtree_with_trash_at (varchar, timestamp);"
224     ActiveRecord::Base.connection.execute "DROP function compute_trashed ();"
225     ActiveRecord::Base.connection.execute "DROP function search_permission_graph(varchar, integer);"
226     ActiveRecord::Base.connection.execute "DROP function compute_permission_subgraph (varchar, varchar, integer);"
227     ActiveRecord::Base.connection.execute "DROP function should_traverse_owned(varchar, integer);"
228     ActiveRecord::Base.connection.execute "DROP function permission_graph_edges();"
229   end
230 end