Merge branch 'master' into origin-2035-arv-mount-tags-folders
[arvados.git] / apps / workbench / app / controllers / folders_controller.rb
1 class FoldersController < ApplicationController
2   def model_class
3     Group
4   end
5
6   def index_pane_list
7     %w(My_folders Shared_with_me)
8   end
9
10   def remove_item
11     @removed_uuids = []
12     links = []
13     item = ArvadosBase.find params[:item_uuid]
14     if (item.class == Link and
15         item.link_class == 'name' and
16         item.tail_uuid = @object.uuid)
17       # Given uuid is a name link, linking an object to this
18       # folder. First follow the link to find the item we're removing,
19       # then delete the link.
20       links << item
21       item = ArvadosBase.find item.head_uuid
22     else
23       # Given uuid is an object. Delete all names.
24       links += Link.where(tail_uuid: @object.uuid,
25                           head_uuid: item.uuid,
26                           link_class: 'name')
27     end
28     links.each do |link|
29       @removed_uuids << link.uuid
30       link.destroy
31     end
32     if item.owner_uuid == @object.uuid
33       # Object is owned by this folder. Remove it from the folder by
34       # changing owner to the current user.
35       item.update_attributes owner_uuid: current_user
36       @removed_uuids << item.uuid
37     end
38   end
39
40   def index
41     @my_folders = []
42     @shared_with_me = []
43     @objects = Group.where(group_class: 'folder').order('name')
44     owner_of = {}
45     moretodo = true
46     while moretodo
47       moretodo = false
48       @objects.each do |folder|
49         if !owner_of[folder.uuid]
50           moretodo = true
51           owner_of[folder.uuid] = folder.owner_uuid
52         end
53         if owner_of[folder.owner_uuid]
54           if owner_of[folder.uuid] != owner_of[folder.owner_uuid]
55             owner_of[folder.uuid] = owner_of[folder.owner_uuid]
56             moretodo = true
57           end
58         end
59       end
60     end
61     @objects.each do |folder|
62       if owner_of[folder.uuid] == current_user.uuid
63         @my_folders << folder
64       else
65         @shared_with_me << folder
66       end
67     end
68   end
69
70   def show
71     @objects = @object.contents include_linked: true
72     @share_links = Link.filter([['head_uuid', '=', @object.uuid],
73                                 ['link_class', '=', 'permission']])
74     @logs = Log.limit(10).filter([['object_uuid', '=', @object.uuid]])
75
76     @objects_and_names = []
77     @objects.each do |object|
78       if !(name_links = @objects.links_for(object, 'name')).empty?
79         name_links.each do |name_link|
80           @objects_and_names << [object, name_link]
81         end
82       else
83         @objects_and_names << [object,
84                                Link.new(tail_uuid: @object.uuid,
85                                         head_uuid: object.uuid,
86                                         link_class: "name",
87                                         name: "")]
88       end
89     end
90
91     super
92   end
93
94   def create
95     @new_resource_attrs = (params['folder'] || {}).merge(group_class: 'folder')
96     @new_resource_attrs[:name] ||= 'New folder'
97     super
98   end
99 end