1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class UsersController < ApplicationController
6 skip_around_filter :require_thread_api_token, only: :welcome
7 skip_before_filter :check_user_agreements, only: [:welcome, :inactive]
8 skip_before_filter :check_user_profile, only: [:welcome, :inactive, :profile]
9 skip_before_filter :find_object_by_uuid, only: [:welcome, :activity, :storage]
10 before_filter :ensure_current_user_is_admin, only: [:sudo, :unsetup, :setup]
13 if params[:uuid] == current_user.uuid
16 if request.url.include?("/users/#{current_user.uuid}")
19 redirect_to(params[:return_to] || project_path(params[:uuid]))
30 redirect_to (params[:return_to] || '/')
35 if current_user.andand.is_invited
36 redirect_to (params[:return_to] || '/')
41 params[:offer_return_to] ||= params[:return_to]
45 @breadcrumb_page_name = nil
46 @users = User.limit(params[:limit])
51 pipeline_instances: {}
54 @spans = [['This week', Time.now.beginning_of_week, Time.now],
56 Time.now.beginning_of_week.advance(weeks:-1),
57 Time.now.beginning_of_week],
58 ['This month', Time.now.beginning_of_month, Time.now],
60 1.month.ago.beginning_of_month,
61 Time.now.beginning_of_month]]
62 @spans.each do |span, threshold_start, threshold_end|
63 @activity[:logins][span] = Log.select(%w(uuid modified_by_user_uuid)).
64 filter([[:event_type, '=', 'login'],
65 [:object_kind, '=', 'arvados#user'],
66 [:created_at, '>=', threshold_start],
67 [:created_at, '<', threshold_end]])
68 @activity[:jobs][span] = Job.select(%w(uuid modified_by_user_uuid)).
69 filter([[:created_at, '>=', threshold_start],
70 [:created_at, '<', threshold_end]])
71 @activity[:pipeline_instances][span] = PipelineInstance.select(%w(uuid modified_by_user_uuid)).
72 filter([[:created_at, '>=', threshold_start],
73 [:created_at, '<', threshold_end]])
74 @activity.each do |type, act|
77 @user_activity[u.uuid] ||= {}
78 @user_activity[u.uuid][span + ' ' + type.to_s] ||= 0
80 records.each do |record|
81 @user_activity[record.modified_by_user_uuid] ||= {}
82 @user_activity[record.modified_by_user_uuid][span + ' ' + type.to_s] ||= 0
83 @user_activity[record.modified_by_user_uuid][span + ' ' + type.to_s] += 1
84 @total_activity[span + ' ' + type.to_s] ||= 0
85 @total_activity[span + ' ' + type.to_s] += 1
89 @users = @users.sort_by do |a|
90 [-@user_activity[a.uuid].values.inject(:+), a.full_name]
92 # Prepend a "Total" pseudo-user to the sorted list
93 @user_activity[nil] = @total_activity
94 @users = [OpenStruct.new(uuid: nil)] + @users
98 @breadcrumb_page_name = nil
99 @users = User.limit(params[:limit])
104 @user_storage[u.uuid] ||= {}
106 filter([[:object_uuid, '=', u.uuid],
107 [:event_type, '=', 'user-storage-report']]).
108 order(:created_at => :desc).
111 storage_log.each do |log_entry|
112 # We expect this block to only execute once since we specified limit(1)
113 @user_storage[u.uuid] = log_entry['properties']
114 @log_date[u.uuid] = log_entry['event_at']
116 total_storage.merge!(@user_storage[u.uuid]) { |k,v1,v2| v1 + v2 }
118 @users = @users.sort_by { |u|
119 [-@user_storage[u.uuid].values.push(0).inject(:+), u.full_name]}
120 # Prepend a "Total" pseudo-user to the sorted list
121 @users = [OpenStruct.new(uuid: nil)] + @users
122 @user_storage[nil] = total_storage
126 if current_user.andand.is_admin
134 if current_user.andand.is_admin
142 resp = arvados_api_client.api(ApiClientAuthorization, '', {
143 api_client_authorization: {
144 owner_uuid: @object.uuid
147 redirect_to root_url(api_token: resp[:api_token])
151 @my_ssh_keys = AuthorizedKey.where(authorized_user_uuid: current_user.uuid)
156 order('created_at desc').
157 where(created_by: current_user.uuid)
159 @my_collections = Collection.
161 order('created_at desc').
162 where(created_by: current_user.uuid)
163 collection_uuids = @my_collections.collect &:uuid
166 collection_uuids.each do |uuid|
167 @persist_state[uuid] = 'cache'
170 Link.filter([['head_uuid', 'in', collection_uuids],
171 ['link_class', 'in', ['tag', 'resources']]]).
175 (@my_tag_links[link.head_uuid] ||= []) << link
177 if link.name == 'wants'
178 @persist_state[link.head_uuid] = 'persistent'
183 @my_pipelines = PipelineInstance.
185 order('created_at desc').
186 where(created_by: current_user.uuid)
189 f.js { render template: 'users/home.js' }
190 f.html { render template: 'users/home' }
195 if current_user.andand.is_admin
202 respond_to do |format|
203 if current_user.andand.is_admin
205 setup_params[:send_notification_email] = "#{Rails.configuration.send_user_setup_notification_email}"
206 if params['user_uuid'] && params['user_uuid'].size>0
207 setup_params[:uuid] = params['user_uuid']
209 if params['email'] && params['email'].size>0
210 user = {email: params['email']}
211 setup_params[:user] = user
213 if params['openid_prefix'] && params['openid_prefix'].size>0
214 setup_params[:openid_prefix] = params['openid_prefix']
216 if params['vm_uuid'] && params['vm_uuid'].size>0
217 setup_params[:vm_uuid] = params['vm_uuid']
220 setup_resp = User.setup setup_params
223 setup_resp[:items].each do |item|
224 if item[:head_kind] == "arvados#virtualMachine"
230 new_groups = params[:groups].split(',').map(&:strip).select{|i| !i.empty?}
231 if vm_link and new_groups != vm_link[:properties][:groups]
232 vm_login_link = Link.where(uuid: vm_link[:uuid])
233 if vm_login_link.items_available > 0
234 link = vm_login_link.results.first
235 props = link.properties
236 props[:groups] = new_groups
244 self.render_error status: 422
247 self.render_error status: 422
253 @vms = VirtualMachine.all.results
255 @current_selections = find_current_links @object
257 respond_to do |format|
265 Link.where(tail_uuid: @object.uuid,
266 link_class: 'permission',
269 if perm_link.properties.andand[:username]
270 @my_vm_logins[perm_link.head_uuid] ||= []
271 @my_vm_logins[perm_link.head_uuid] << perm_link.properties[:username]
274 @my_virtual_machines = VirtualMachine.where(uuid: @my_vm_logins.keys)
278 @my_ssh_keys = AuthorizedKey.where(key_type: 'SSH', owner_uuid: @object.uuid)
281 def add_ssh_key_popup
282 respond_to do |format|
289 respond_to do |format|
290 key_params = {'key_type' => 'SSH'}
291 key_params['authorized_user_uuid'] = current_user.uuid
293 if params['name'] && params['name'].size>0
294 key_params['name'] = params['name'].strip
296 if params['public_key'] && params['public_key'].size>0
297 key_params['public_key'] = params['public_key'].strip
300 if !key_params['name'] && params['public_key'].andand.size>0
301 split_key = key_params['public_key'].split
302 key_params['name'] = split_key[-1] if (split_key.size == 3)
305 new_key = AuthorizedKey.create! key_params
309 self.render_error status: 422
314 def request_shell_access
315 logger.warn "request_access: #{params.inspect}"
316 params['request_url'] = request.url
317 RequestShellAccessReporter.send_request(current_user, params).deliver
322 def find_current_links user
323 current_selections = {}
326 return current_selections
330 oid_login_perms = Link.where(tail_uuid: user.email,
331 head_kind: 'arvados#user',
332 link_class: 'permission',
335 if oid_login_perms.any?
336 prefix_properties = oid_login_perms.first.properties
337 current_selections[:identity_url_prefix] = prefix_properties[:identity_url_prefix]
341 repo_perms = Link.where(tail_uuid: user.uuid,
342 head_kind: 'arvados#repository',
343 link_class: 'permission',
346 repo_uuid = repo_perms.first.head_uuid
347 repos = Repository.where(head_uuid: repo_uuid)
349 repo_name = repos.first.name
350 current_selections[:repo_name] = repo_name
355 vm_login_perms = Link.where(tail_uuid: user.uuid,
356 head_kind: 'arvados#virtualMachine',
357 link_class: 'permission',
359 if vm_login_perms.any?
360 vm_perm = vm_login_perms.first
361 vm_uuid = vm_perm.head_uuid
362 current_selections[:vm_uuid] = vm_uuid
363 current_selections[:groups] = vm_perm.properties[:groups].andand.join(', ')
366 return current_selections