Merge branch '2044-workbench-project-sharing'
[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(Contents Sharing Advanced)
13     else
14       %w(Contents 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                                 offset: params[:offset] || 0)
87     @logs = Log.limit(10).filter([['object_uuid', '=', @object.uuid]])
88     @users = User.limit(10000).
89       select(["uuid", "is_active", "first_name", "last_name"]).
90       filter([['is_active', '=', 'true']])
91     @groups = Group.limit(10000).
92       select(["uuid", "name", "description"])
93
94     begin
95       @share_links = Link.permissions_for(@object)
96       @user_is_manager = true
97     rescue ArvadosApiClient::AccessForbiddenException,
98            ArvadosApiClient::NotFoundException
99       @share_links = []
100       @user_is_manager = false
101     end
102
103     @objects_and_names = []
104     @objects.each do |object|
105       if !(name_links = @objects.links_for(object, 'name')).empty?
106         name_links.each do |name_link|
107           @objects_and_names << [object, name_link]
108         end
109       elsif object.respond_to? :name
110         @objects_and_names << [object, object]
111       else
112         @objects_and_names << [object,
113                                Link.new(owner_uuid: @object.uuid,
114                                         tail_uuid: @object.uuid,
115                                         head_uuid: object.uuid,
116                                         link_class: "name",
117                                         name: "")]
118       end
119     end
120     if params[:partial]
121       respond_to do |f|
122         f.json {
123           render json: {
124             content: render_to_string(partial: 'show_contents_rows.html',
125                                       formats: [:html],
126                                       locals: {
127                                         objects_and_names: @objects_and_names,
128                                         project: @object
129                                       }),
130             next_page_href: (next_page_offset and
131                              url_for(offset: next_page_offset, partial: true))
132           }
133         }
134       end
135     else
136       super
137     end
138   end
139
140   def create
141     @new_resource_attrs = (params['project'] || {}).merge(group_class: 'project')
142     @new_resource_attrs[:name] ||= 'New project'
143     super
144   end
145
146   def update
147     @updates = params['project']
148     super
149   end
150
151   def share_with
152     if not params[:uuids].andand.any?
153       @errors = ["No user/group UUIDs specified to share with."]
154       return render_error(status: 422)
155     end
156     results = {"success" => [], "failure" => {}}
157     params[:uuids].each do |shared_uuid|
158       begin
159         Link.create(tail_uuid: shared_uuid, link_class: "permission",
160                     name: "can_read", head_uuid: @object.uuid)
161       rescue ArvadosApiClient::ApiError => error
162         results["failure"][shared_uuid] = error.api_response.andand[:errors]
163       else
164         results["success"] << shared_uuid
165       end
166     end
167     status = (results["failure"].empty?) ? 200 : 422
168     respond_to do |f|
169       f.json { render(json: results, status: status) }
170     end
171   end
172 end