2872: Fix tests broken in merge.
[arvados.git] / apps / workbench / app / controllers / users_controller.rb
1 class UsersController < ApplicationController
2   skip_before_filter :find_object_by_uuid, :only => [:welcome, :activity, :storage]
3   before_filter :ensure_current_user_is_admin, only: [:sudo, :unsetup, :setup]
4
5   def welcome
6     if current_user
7       params[:action] = 'home'
8       home
9     end
10   end
11
12   def activity
13     @breadcrumb_page_name = nil
14     @users = User.limit(params[:limit] || 1000).all
15     @user_activity = {}
16     @activity = {
17       logins: {},
18       jobs: {},
19       pipeline_instances: {}
20     }
21     @total_activity = {}
22     @spans = [['This week', Time.now.beginning_of_week, Time.now],
23               ['Last week',
24                Time.now.beginning_of_week.advance(weeks:-1),
25                Time.now.beginning_of_week],
26               ['This month', Time.now.beginning_of_month, Time.now],
27               ['Last month',
28                1.month.ago.beginning_of_month,
29                Time.now.beginning_of_month]]
30     @spans.each do |span, threshold_start, threshold_end|
31       @activity[:logins][span] = Log.
32         filter([[:event_type, '=', 'login'],
33                 [:object_kind, '=', 'arvados#user'],
34                 [:created_at, '>=', threshold_start],
35                 [:created_at, '<', threshold_end]])
36       @activity[:jobs][span] = Job.
37         filter([[:created_at, '>=', threshold_start],
38                 [:created_at, '<', threshold_end]])
39       @activity[:pipeline_instances][span] = PipelineInstance.
40         filter([[:created_at, '>=', threshold_start],
41                 [:created_at, '<', threshold_end]])
42       @activity.each do |type, act|
43         records = act[span]
44         @users.each do |u|
45           @user_activity[u.uuid] ||= {}
46           @user_activity[u.uuid][span + ' ' + type.to_s] ||= 0
47         end
48         records.each do |record|
49           @user_activity[record.modified_by_user_uuid] ||= {}
50           @user_activity[record.modified_by_user_uuid][span + ' ' + type.to_s] ||= 0
51           @user_activity[record.modified_by_user_uuid][span + ' ' + type.to_s] += 1
52           @total_activity[span + ' ' + type.to_s] ||= 0
53           @total_activity[span + ' ' + type.to_s] += 1
54         end
55       end
56     end
57     @users = @users.sort_by do |a|
58       [-@user_activity[a.uuid].values.inject(:+), a.full_name]
59     end
60     # Prepend a "Total" pseudo-user to the sorted list
61     @user_activity[nil] = @total_activity
62     @users = [OpenStruct.new(uuid: nil)] + @users
63   end
64
65   def storage
66     @breadcrumb_page_name = nil
67     @users = User.limit(params[:limit] || 1000).all
68     @user_storage = {}
69     total_storage = {}
70     @log_date = {}
71     @users.each do |u|
72       @user_storage[u.uuid] ||= {}
73       storage_log = Log.
74         filter([[:object_uuid, '=', u.uuid],
75                 [:event_type, '=', 'user-storage-report']]).
76         order(:created_at => :desc).
77         limit(1)
78       storage_log.each do |log_entry|
79         # We expect this block to only execute once since we specified limit(1)
80         @user_storage[u.uuid] = log_entry['properties']
81         @log_date[u.uuid] = log_entry['event_at']
82       end
83       total_storage.merge!(@user_storage[u.uuid]) { |k,v1,v2| v1 + v2 }
84     end
85     @users = @users.sort_by { |u|
86       [-@user_storage[u.uuid].values.push(0).inject(:+), u.full_name]}
87     # Prepend a "Total" pseudo-user to the sorted list
88     @users = [OpenStruct.new(uuid: nil)] + @users
89     @user_storage[nil] = total_storage
90   end
91
92   def show_pane_list
93     if current_user.andand.is_admin
94       super | %w(Admin)
95     else
96       super
97     end
98   end
99
100   def index_pane_list
101     if current_user.andand.is_admin
102       super | %w(Activity)
103     else
104       super
105     end
106   end
107
108   def sudo
109     resp = arvados_api_client.api(ApiClientAuthorization, '', {
110                                     api_client_authorization: {
111                                       owner_uuid: @object.uuid
112                                     }
113                                   })
114     redirect_to root_url(api_token: resp[:api_token])
115   end
116
117   def home
118     @showallalerts = false
119     @my_ssh_keys = AuthorizedKey.where(authorized_user_uuid: current_user.uuid)
120     @my_tag_links = {}
121
122     @my_jobs = Job.
123       limit(10).
124       order('created_at desc').
125       where(created_by: current_user.uuid)
126
127     @my_collections = Collection.
128       limit(10).
129       order('created_at desc').
130       where(created_by: current_user.uuid)
131     collection_uuids = @my_collections.collect &:uuid
132
133     @persist_state = {}
134     collection_uuids.each do |uuid|
135       @persist_state[uuid] = 'cache'
136     end
137
138     Link.limit(1000).filter([['head_uuid', 'in', collection_uuids],
139                              ['link_class', 'in', ['tag', 'resources']]]).
140       each do |link|
141       case link.link_class
142       when 'tag'
143         (@my_tag_links[link.head_uuid] ||= []) << link
144       when 'resources'
145         if link.name == 'wants'
146           @persist_state[link.head_uuid] = 'persistent'
147         end
148       end
149     end
150
151     @my_pipelines = PipelineInstance.
152       limit(10).
153       order('created_at desc').
154       where(created_by: current_user.uuid)
155
156     respond_to do |f|
157       f.js { render template: 'users/home.js' }
158       f.html { render template: 'users/home' }
159     end
160   end
161
162   def unsetup
163     if current_user.andand.is_admin
164       @object.unsetup
165     end
166     show
167   end
168
169   def setup
170     respond_to do |format|
171       if current_user.andand.is_admin
172         setup_params = {}
173         setup_params[:send_notification_email] = "#{Rails.configuration.send_user_setup_notification_email}"
174         if params['user_uuid'] && params['user_uuid'].size>0
175           setup_params[:uuid] = params['user_uuid']
176         end
177         if params['email'] && params['email'].size>0
178           user = {email: params['email']}
179           setup_params[:user] = user
180         end
181         if params['openid_prefix'] && params['openid_prefix'].size>0
182           setup_params[:openid_prefix] = params['openid_prefix']
183         end
184         if params['repo_name'] && params['repo_name'].size>0
185           setup_params[:repo_name] = params['repo_name']
186         end
187         if params['vm_uuid'] && params['vm_uuid'].size>0
188           setup_params[:vm_uuid] = params['vm_uuid']
189         end
190
191         if User.setup setup_params
192           format.js
193         else
194           self.render_error status: 422
195         end
196       else
197         self.render_error status: 422
198       end
199     end
200   end
201
202   def setup_popup
203     @vms = VirtualMachine.all.results
204
205     @current_selections = find_current_links @object
206
207     respond_to do |format|
208       format.html
209       format.js
210     end
211   end
212
213   protected
214
215   def find_current_links user
216     current_selections = {}
217
218     if !user
219       return current_selections
220     end
221
222     # oid login perm
223     oid_login_perms = Link.where(tail_uuid: user.email,
224                                    head_kind: 'arvados#user',
225                                    link_class: 'permission',
226                                    name: 'can_login')
227
228     if oid_login_perms.any?
229       prefix_properties = oid_login_perms.first.properties
230       current_selections[:identity_url_prefix] = prefix_properties[:identity_url_prefix]
231     end
232
233     # repo perm
234     repo_perms = Link.where(tail_uuid: user.uuid,
235                             head_kind: 'arvados#repository',
236                             link_class: 'permission',
237                             name: 'can_write')
238     if repo_perms.any?
239       repo_uuid = repo_perms.first.head_uuid
240       repos = Repository.where(head_uuid: repo_uuid)
241       if repos.any?
242         repo_name = repos.first.name
243         current_selections[:repo_name] = repo_name
244       end
245     end
246
247     # vm login perm
248     vm_login_perms = Link.where(tail_uuid: user.uuid,
249                               head_kind: 'arvados#virtualMachine',
250                               link_class: 'permission',
251                               name: 'can_login')
252     if vm_login_perms.any?
253       vm_uuid = vm_login_perms.first.head_uuid
254       current_selections[:vm_uuid] = vm_uuid
255     end
256
257     return current_selections
258   end
259
260 end