2760: Merge branch '2760-not-all-objects-in-folders' refs #2760
[arvados.git] / services / api / app / controllers / arvados / v1 / repositories_controller.rb
1 class Arvados::V1::RepositoriesController < ApplicationController
2   skip_before_filter :find_object_by_uuid, :only => :get_all_permissions
3   skip_before_filter :render_404_if_no_object, :only => :get_all_permissions
4   before_filter :admin_required, :only => :get_all_permissions
5   def get_all_permissions
6     @users = {}
7     User.includes(:authorized_keys).all.each do |u|
8       @users[u.uuid] = u
9     end
10     @user_aks = {}
11     @repo_info = {}
12     @repos = Repository.includes(:permissions).all
13     @repos.each do |repo|
14       gitolite_permissions = ''
15       perms = []
16       repo.permissions.each do |perm|
17         if ArvadosModel::resource_class_for_uuid(perm.tail_uuid) == Group
18           @users.each do |user_uuid, user|
19             user.group_permissions.each do |group_uuid, perm_mask|
20               if perm_mask[:write]
21                 perms << {name: 'can_write', user_uuid: user_uuid}
22               elsif perm_mask[:read]
23                 perms << {name: 'can_read', user_uuid: user_uuid}
24               end
25             end
26           end
27         else
28           perms << {name: perm.name, user_uuid: perm.tail_uuid}
29         end
30       end
31       # Owner of the repository, and all admins, can RW
32       ([repo.owner_uuid] + @users.keys).each do |user_uuid|
33         %w(can_read can_write).each do |name|
34           perms << {name: name, user_uuid: user_uuid}
35         end
36       end
37       perms.each do |perm|
38         user_uuid = perm[:user_uuid]
39         @user_aks[user_uuid] = @users[user_uuid].andand.authorized_keys.andand.
40           collect do |ak|
41           {
42             public_key: ak.public_key,
43             authorized_key_uuid: ak.uuid
44           }
45         end || []
46         if @user_aks[user_uuid].any?
47           @repo_info[repo.uuid] ||= {
48             uuid: repo.uuid,
49             name: repo.name,
50             push_url: repo.push_url,
51             fetch_url: repo.fetch_url,
52             user_permissions: {}
53           }
54           ri = (@repo_info[repo.uuid][:user_permissions][user_uuid] ||= {})
55           ri[perm[:name]] = true
56         end
57       end
58     end
59     @repo_info.values.each do |repo_users|
60       repo_users[:user_permissions].each do |user_uuid,perms|
61         if perms['can_write']
62           perms[:gitolite_permissions] = 'RW'
63           perms['can_read'] = true
64         elsif perms['can_read']
65           perms[:gitolite_permissions] = 'R'
66         end
67       end
68     end
69     render json: {
70       kind: 'arvados#RepositoryPermissionSnapshot',
71       repositories: @repo_info.values,
72       user_keys: @user_aks
73     }
74   end
75 end