3235: Merge branch 'master' into 3235-top-nav-site-search
[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         dst.save!
75       end
76     end
77     session[:selected_move_items] = nil
78     redirect_to @object
79   end
80
81   def destroy
82     while (objects = Link.filter([['owner_uuid','=',@object.uuid],
83                                   ['tail_uuid','=',@object.uuid]])).any?
84       objects.each do |object|
85         object.destroy
86       end
87     end
88     while (objects = @object.contents(include_linked: false)).any?
89       objects.each do |object|
90         object.update_attributes! owner_uuid: current_user.uuid
91       end
92     end
93     if ArvadosBase::resource_class_for_uuid(@object.owner_uuid) == Group
94       params[:return_to] ||= group_path(@object.owner_uuid)
95     else
96       params[:return_to] ||= projects_path
97     end
98     super
99   end
100
101   def find_objects_for_index
102     @objects = all_projects
103     super
104   end
105
106   def show
107     if !@object
108       return render_not_found("object not found")
109     end
110     @objects = @object.contents(limit: 50,
111                                 include_linked: true,
112                                 filters: params[:filters],
113                                 offset: params[:offset] || 0)
114     @logs = Log.limit(10).filter([['object_uuid', '=', @object.uuid]])
115     @users = User.limit(10000).
116       select(["uuid", "is_active", "first_name", "last_name"]).
117       filter([['is_active', '=', 'true']])
118     @groups = Group.limit(10000).
119       select(["uuid", "name", "description"])
120
121     begin
122       @share_links = Link.permissions_for(@object)
123       @user_is_manager = true
124     rescue ArvadosApiClient::AccessForbiddenException,
125            ArvadosApiClient::NotFoundException
126       @share_links = []
127       @user_is_manager = false
128     end
129
130     @objects_and_names = get_objects_and_names @objects
131
132     if params[:partial]
133       respond_to do |f|
134         f.json {
135           render json: {
136             content: render_to_string(partial: 'show_contents_rows.html',
137                                       formats: [:html],
138                                       locals: {
139                                         objects_and_names: @objects_and_names,
140                                         project: @object
141                                       }),
142             next_page_href: (next_page_offset and
143                              url_for(offset: next_page_offset, filters: params[:filters], partial: true))
144           }
145         }
146       end
147     else
148       super
149     end
150   end
151
152   def create
153     @new_resource_attrs = (params['project'] || {}).merge(group_class: 'project')
154     @new_resource_attrs[:name] ||= 'New project'
155     super
156   end
157
158   def update
159     @updates = params['project']
160     super
161   end
162
163   helper_method :get_objects_and_names
164   def get_objects_and_names(objects)
165     objects_and_names = []
166     objects.each do |object|
167       if !(name_links = objects.links_for(object, 'name')).empty?
168         name_links.each do |name_link|
169           objects_and_names << [object, name_link]
170         end
171       elsif object.respond_to? :name
172         objects_and_names << [object, object]
173       else
174         objects_and_names << [object,
175                                Link.new(owner_uuid: @object.uuid,
176                                         tail_uuid: @object.uuid,
177                                         head_uuid: object.uuid,
178                                         link_class: "name",
179                                         name: "")]
180       end
181     end
182     objects_and_names
183   end
184
185   def share_with
186     if not params[:uuids].andand.any?
187       @errors = ["No user/group UUIDs specified to share with."]
188       return render_error(status: 422)
189     end
190     results = {"success" => [], "errors" => []}
191     params[:uuids].each do |shared_uuid|
192       begin
193         Link.create(tail_uuid: shared_uuid, link_class: "permission",
194                     name: "can_read", head_uuid: @object.uuid)
195       rescue ArvadosApiClient::ApiError => error
196         error_list = error.api_response.andand[:errors]
197         if error_list.andand.any?
198           results["errors"] += error_list.map { |e| "#{shared_uuid}: #{e}" }
199         else
200           error_code = error.api_status || "Bad status"
201           results["errors"] << "#{shared_uuid}: #{error_code} response"
202         end
203       else
204         results["success"] << shared_uuid
205       end
206     end
207     if results["errors"].empty?
208       results.delete("errors")
209       status = 200
210     else
211       status = 422
212     end
213     respond_to do |f|
214       f.json { render(json: results, status: status) }
215     end
216   end
217 end