Merge branch 'master' into 2985-selection-button-move-copy
[arvados.git] / apps / workbench / app / controllers / projects_controller.rb
1 class ProjectsController < ApplicationController
2   def model_class
3     Group
4   end
5
6   def index_pane_list
7     %w(Projects)
8   end
9
10   def show_pane_list
11     if @user_is_manager
12       %w(Data_collections Jobs_and_pipelines Pipeline_templates Subprojects Other_objects Sharing Advanced)
13     else
14       %w(Data_collections Jobs_and_pipelines Pipeline_templates Subprojects Other_objects Advanced)
15     end
16   end
17
18   def remove_item
19     params[:item_uuids] = [params[:item_uuid]]
20     remove_items
21     render template: 'projects/remove_items'
22   end
23
24   def remove_items
25     @removed_uuids = []
26     links = []
27     params[:item_uuids].collect { |uuid| ArvadosBase.find uuid }.each do |item|
28       if (item.class == Link and
29           item.link_class == 'name' and
30           item.tail_uuid == @object.uuid)
31         # Given uuid is a name link, linking an object to this
32         # project. First follow the link to find the item we're removing,
33         # then delete the link.
34         links << item
35         item = ArvadosBase.find item.head_uuid
36       else
37         # Given uuid is an object. Delete all names.
38         links += Link.where(tail_uuid: @object.uuid,
39                             head_uuid: item.uuid,
40                             link_class: 'name')
41       end
42       links.each do |link|
43         @removed_uuids << link.uuid
44         link.destroy
45       end
46       if item.owner_uuid == @object.uuid
47         # Object is owned by this project. Remove it from the project by
48         # changing owner to the current user.
49         item.update_attributes owner_uuid: current_user.uuid
50         @removed_uuids << item.uuid
51       end
52     end
53   end
54
55   def move_items
56     target_uuid = params['target']
57     uuids_to_add = session[:selected_move_items]
58
59     uuids_to_add.
60       collect { |x| ArvadosBase::resource_class_for_uuid(x) }.
61       uniq.
62       each do |resource_class|
63       resource_class.filter([['uuid','in',uuids_to_add]]).each do |dst|
64         if resource_class == Collection
65           dst = Link.new(owner_uuid: target_uuid,
66                          tail_uuid: target_uuid,
67                          head_uuid: dst.uuid,
68                          link_class: 'name',
69                          name: target_uuid)
70         else
71           dst.owner_uuid = target_uuid
72           dst.tail_uuid = target_uuid if dst.class == Link
73         end
74         begin
75           dst.save!
76         rescue
77           dst.name += " (#{Time.now.localtime})" if dst.respond_to? :name=
78           dst.save!
79         end
80       end
81     end
82     session[:selected_move_items] = nil
83     redirect_to @object
84   end
85
86   def destroy
87     while (objects = Link.filter([['owner_uuid','=',@object.uuid],
88                                   ['tail_uuid','=',@object.uuid]])).any?
89       objects.each do |object|
90         object.destroy
91       end
92     end
93     while (objects = @object.contents(include_linked: false)).any?
94       objects.each do |object|
95         object.update_attributes! owner_uuid: current_user.uuid
96       end
97     end
98     if ArvadosBase::resource_class_for_uuid(@object.owner_uuid) == Group
99       params[:return_to] ||= group_path(@object.owner_uuid)
100     else
101       params[:return_to] ||= projects_path
102     end
103     super
104   end
105
106   def find_objects_for_index
107     @objects = all_projects
108     super
109   end
110
111   def show
112     if !@object
113       return render_not_found("object not found")
114     end
115     @objects = @object.contents(limit: 50,
116                                 include_linked: true,
117                                 filters: params[:filters],
118                                 offset: params[:offset] || 0)
119     @logs = Log.limit(10).filter([['object_uuid', '=', @object.uuid]])
120     @users = User.limit(10000).
121       select(["uuid", "is_active", "first_name", "last_name"]).
122       filter([['is_active', '=', 'true']])
123     @groups = Group.limit(10000).
124       select(["uuid", "name", "description"])
125
126     begin
127       @share_links = Link.permissions_for(@object)
128       @user_is_manager = true
129     rescue ArvadosApiClient::AccessForbiddenException,
130            ArvadosApiClient::NotFoundException
131       @share_links = []
132       @user_is_manager = false
133     end
134
135     @objects_and_names = get_objects_and_names @objects
136
137     if params[:partial]
138       respond_to do |f|
139         f.json {
140           render json: {
141             content: render_to_string(partial: 'show_contents_rows.html',
142                                       formats: [:html],
143                                       locals: {
144                                         objects_and_names: @objects_and_names,
145                                         project: @object
146                                       }),
147             next_page_href: (next_page_offset and
148                              url_for(offset: next_page_offset, filters: params[:filters], partial: true))
149           }
150         }
151       end
152     else
153       super
154     end
155   end
156
157   def create
158     @new_resource_attrs = (params['project'] || {}).merge(group_class: 'project')
159     @new_resource_attrs[:name] ||= 'New project'
160     super
161   end
162
163   def update
164     @updates = params['project']
165     super
166   end
167
168   helper_method :get_objects_and_names
169   def get_objects_and_names(objects)
170     objects_and_names = []
171     objects.each do |object|
172       if !(name_links = objects.links_for(object, 'name')).empty?
173         name_links.each do |name_link|
174           objects_and_names << [object, name_link]
175         end
176       elsif object.respond_to? :name
177         objects_and_names << [object, object]
178       else
179         objects_and_names << [object,
180                                Link.new(owner_uuid: @object.uuid,
181                                         tail_uuid: @object.uuid,
182                                         head_uuid: object.uuid,
183                                         link_class: "name",
184                                         name: "")]
185       end
186     end
187     objects_and_names
188   end
189
190   def share_with
191     if not params[:uuids].andand.any?
192       @errors = ["No user/group UUIDs specified to share with."]
193       return render_error(status: 422)
194     end
195     results = {"success" => [], "errors" => []}
196     params[:uuids].each do |shared_uuid|
197       begin
198         Link.create(tail_uuid: shared_uuid, link_class: "permission",
199                     name: "can_read", head_uuid: @object.uuid)
200       rescue ArvadosApiClient::ApiError => error
201         error_list = error.api_response.andand[:errors]
202         if error_list.andand.any?
203           results["errors"] += error_list.map { |e| "#{shared_uuid}: #{e}" }
204         else
205           error_code = error.api_status || "Bad status"
206           results["errors"] << "#{shared_uuid}: #{error_code} response"
207         end
208       else
209         results["success"] << shared_uuid
210       end
211     end
212     if results["errors"].empty?
213       results.delete("errors")
214       status = 200
215     else
216       status = 422
217     end
218     respond_to do |f|
219       f.json { render(json: results, status: status) }
220     end
221   end
222 end