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