1 class ApplicationController < ActionController::Base
2 respond_to :html, :json, :js
4 around_filter :thread_clear
5 around_filter :thread_with_api_token, :except => [:render_exception, :render_not_found]
6 before_filter :find_object_by_uuid, :except => [:index, :render_exception, :render_not_found]
7 before_filter :check_user_agreements, :except => [:render_exception, :render_not_found]
11 rescue_from Exception,
12 :with => :render_exception
13 rescue_from ActiveRecord::RecordNotFound,
14 :with => :render_not_found
15 rescue_from ActionController::RoutingError,
16 :with => :render_not_found
17 rescue_from ActionController::UnknownController,
18 :with => :render_not_found
19 rescue_from ::AbstractController::ActionNotFound,
20 :with => :render_not_found
23 def unprocessable(message=nil)
25 @errors << message if message
26 render_error status: 422
29 def render_error(opts)
31 # json must come before html here, so it gets used as the
32 # default format when js is requested by the client. This lets
33 # ajax:error callback parse the response correctly, even though
35 f.json { render opts.merge(json: {success: false, errors: @errors}) }
36 f.html { render opts.merge(controller: 'application', action: 'error') }
40 def render_exception(e)
41 logger.error e.inspect
42 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
43 if @object.andand.errors.andand.full_messages.andand.any?
44 @errors = @object.errors.full_messages
48 self.render_error status: 422
51 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
52 logger.error e.inspect
53 @errors = ["Path not found"]
54 self.render_error status: 404
58 @objects ||= model_class.limit(1000).all
60 f.json { render json: @objects }
68 return render_not_found("object not found")
71 f.json { render json: @object }
73 if request.method == 'GET'
76 redirect_to params[:return_to] || @object
85 return render_not_found("object not found")
90 @object = model_class.new
94 updates = params[@object.class.to_s.underscore.singularize.to_sym]
95 updates.keys.each do |attr|
96 if @object.send(attr).is_a? Hash and updates[attr].is_a? String
97 updates[attr] = Oj.load updates[attr]
100 if @object.update_attributes updates
103 self.render_error status: 422
108 @object ||= model_class.new params[model_class.to_s.singularize.to_sym]
110 redirect_to(params[:return_to] || @object)
117 redirect_to(params[:return_to] || :back)
122 self.render_error status: 422
127 if Thread.current[:arvados_api_token]
128 Thread.current[:user] ||= User.current
130 logger.error "No API token in Thread"
136 controller_name.classify.constantize
139 def breadcrumb_page_name
140 (@breadcrumb_page_name ||
141 (@object.friendly_link_name if @object.respond_to? :friendly_link_name))
146 def find_object_by_uuid
147 if params[:id] and params[:id].match /\D/
148 params[:uuid] = params.delete :id
150 if params[:uuid].is_a? String
151 @object = model_class.find(params[:uuid])
153 @object = model_class.where(uuid: params[:uuid]).first
158 Thread.current[:arvados_api_token] = nil
159 Thread.current[:user] = nil
160 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
162 Rails.cache.delete_matched(/^request_#{Thread.current.object_id}_/)
165 def thread_with_api_token(login_optional = false)
167 try_redirect_to_login = true
168 if params[:api_token]
169 try_redirect_to_login = false
170 Thread.current[:arvados_api_token] = params[:api_token]
171 # Before copying the token into session[], do a simple API
172 # call to verify its authenticity.
174 session[:arvados_api_token] = params[:api_token]
175 if !request.format.json? and request.method == 'GET'
176 # Repeat this request with api_token in the (new) session
177 # cookie instead of the query string. This prevents API
178 # tokens from appearing in (and being inadvisedly copied
179 # and pasted from) browser Location bars.
180 redirect_to request.fullpath.sub(%r{([&\?]api_token=)[^&\?]*}, '')
185 @errors = ['Invalid API token']
186 self.render_error status: 401
188 elsif session[:arvados_api_token]
189 # In this case, the token must have already verified at some
190 # point, but it might have been revoked since. We'll try
191 # using it, and catch the exception if it doesn't work.
192 try_redirect_to_login = false
193 Thread.current[:arvados_api_token] = session[:arvados_api_token]
196 rescue ArvadosApiClient::NotLoggedInException
197 try_redirect_to_login = true
200 logger.debug "No token received, session is #{session.inspect}"
202 if try_redirect_to_login
203 unless login_optional
206 if request.method == 'GET'
207 redirect_to $arvados_api_client.arvados_login_url(return_to: request.url)
209 flash[:error] = "Either you are not logged in, or your session has timed out. I can't automatically log you in and re-attempt this request."
214 @errors = ['You do not seem to be logged in. You did not supply an API token with this request, and your session (if any) has timed out.']
215 self.render_error status: 422
219 # login is optional for this route so go on to the regular controller
220 Thread.current[:arvados_api_token] = nil
225 # Remove token in case this Thread is used for anything else.
226 Thread.current[:arvados_api_token] = nil
230 def thread_with_optional_api_token
231 thread_with_api_token(true) do
238 Link.where(uuid: 'just-verifying-my-api-token')
240 rescue ArvadosApiClient::NotLoggedInException
245 def ensure_current_user_is_admin
246 unless current_user and current_user.is_admin
247 @errors = ['Permission denied']
248 self.render_error status: 401
252 def check_user_agreements
253 if current_user && !current_user.is_active && current_user.is_invited
254 signatures = UserAgreement.signatures
255 @signed_ua_uuids = UserAgreement.signatures.map &:head_uuid
256 @required_user_agreements = UserAgreement.all.map do |ua|
257 if not @signed_ua_uuids.index ua.uuid
258 Collection.find(ua.uuid)
261 if @required_user_agreements.empty?
262 # No agreements to sign. Perhaps we just need to ask?
263 current_user.activate
264 if !current_user.is_active
265 logger.warn "#{current_user.uuid.inspect}: " +
266 "No user agreements to sign, but activate failed!"
269 if !current_user.is_active
270 render 'user_agreements/index'
277 return Rails.configuration.arvados_theme