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