1 class ApplicationController < ActionController::Base
3 around_filter :thread_clear
4 around_filter :thread_with_api_token, :except => [:render_exception, :render_not_found]
5 before_filter :find_object_by_uuid, :except => [:index, :render_exception, :render_not_found]
6 before_filter :check_user_agreements, :except => [:render_exception, :render_not_found]
10 rescue_from Exception,
11 :with => :render_exception
12 rescue_from ActiveRecord::RecordNotFound,
13 :with => :render_not_found
14 rescue_from ActionController::RoutingError,
15 :with => :render_not_found
16 rescue_from ActionController::UnknownController,
17 :with => :render_not_found
18 rescue_from ::AbstractController::ActionNotFound,
19 :with => :render_not_found
22 def unprocessable(message=nil)
24 @errors << message if message
25 render_error status: 422
28 def render_error(opts)
30 # json must come before html here, so it gets used as the
31 # default format when js is requested by the client. This lets
32 # ajax:error callback parse the response correctly, even though
34 f.json { render opts.merge(json: {success: false, errors: @errors}) }
35 f.html { render opts.merge(controller: 'application', action: 'error') }
39 def render_exception(e)
40 logger.error e.inspect
41 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
42 if @object.andand.errors.andand.full_messages.andand.any?
43 @errors = @object.errors.full_messages
47 self.render_error status: 422
50 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
51 logger.error e.inspect
52 @errors = ["Path not found"]
53 self.render_error status: 404
58 @objects ||= model_class.limit(1000).all
60 f.json { render json: @objects }
67 return render_not_found("object not found")
70 f.json { render json: @object }
72 if request.method == 'GET'
75 redirect_to params[:return_to] || @object
83 return render_not_found("object not found")
88 @object = model_class.new
92 updates = params[@object.class.to_s.underscore.singularize.to_sym]
93 updates.keys.each do |attr|
94 if @object.send(attr).is_a? Hash and updates[attr].is_a? String
95 updates[attr] = Oj.load updates[attr]
98 if @object.update_attributes updates
101 self.render_error status: 422
106 @object ||= model_class.new params[model_class.to_s.singularize.to_sym]
108 redirect_to(params[:return_to] || @object)
113 redirect_to(params[:return_to] || :back)
115 self.render_error status: 422
120 if Thread.current[:arvados_api_token]
121 Thread.current[:user] ||= User.current
123 logger.error "No API token in Thread"
129 controller_name.classify.constantize
134 def find_object_by_uuid
135 if params[:id] and params[:id].match /\D/
136 params[:uuid] = params.delete :id
138 if params[:uuid].is_a? String
139 @object = model_class.find(params[:uuid])
141 @object = model_class.where(uuid: params[:uuid]).first
146 Thread.current[:arvados_api_token] = nil
147 Thread.current[:user] = nil
151 def thread_with_api_token(login_optional = false)
153 try_redirect_to_login = true
154 if params[:api_token]
155 try_redirect_to_login = false
156 Thread.current[:arvados_api_token] = params[:api_token]
157 # Before copying the token into session[], do a simple API
158 # call to verify its authenticity.
160 session[:arvados_api_token] = params[:api_token]
161 if !request.format.json? and request.method == 'GET'
162 # Repeat this request with api_token in the (new) session
163 # cookie instead of the query string. This prevents API
164 # tokens from appearing in (and being inadvisedly copied
165 # and pasted from) browser Location bars.
166 redirect_to request.fullpath.sub(%r{([&\?]api_token=)[^&\?]*}, '')
171 @errors = ['Invalid API token']
172 self.render_error status: 401
174 elsif session[:arvados_api_token]
175 # In this case, the token must have already verified at some
176 # point, but it might have been revoked since. We'll try
177 # using it, and catch the exception if it doesn't work.
178 try_redirect_to_login = false
179 Thread.current[:arvados_api_token] = session[:arvados_api_token]
182 rescue ArvadosApiClient::NotLoggedInException
183 try_redirect_to_login = true
186 logger.debug "No token received, session is #{session.inspect}"
188 if try_redirect_to_login
189 unless login_optional
192 if request.method == 'GET'
193 redirect_to $arvados_api_client.arvados_login_url(return_to: request.url)
195 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."
200 @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.']
201 self.render_error status: 422
205 # login is optional for this route so go on to the regular controller
206 Thread.current[:arvados_api_token] = nil
211 # Remove token in case this Thread is used for anything else.
212 Thread.current[:arvados_api_token] = nil
216 def thread_with_optional_api_token
217 thread_with_api_token(true) do
224 Link.where(uuid: 'just-verifying-my-api-token')
226 rescue ArvadosApiClient::NotLoggedInException
231 def ensure_current_user_is_admin
232 unless current_user and current_user.is_admin
233 @errors = ['Permission denied']
234 self.render_error status: 401
238 def check_user_agreements
239 if current_user && !current_user.is_active && current_user.is_invited
240 signatures = UserAgreement.signatures
241 @signed_ua_uuids = UserAgreement.signatures.map &:head_uuid
242 @required_user_agreements = UserAgreement.all.map do |ua|
243 if not @signed_ua_uuids.index ua.uuid
244 Collection.find(ua.uuid)
247 if @required_user_agreements.empty?
248 # No agreements to sign. Perhaps we just need to ask?
249 current_user.activate
250 if !current_user.is_active
251 logger.warn "#{current_user.uuid.inspect}: " +
252 "No user agreements to sign, but activate failed!"
255 if !current_user.is_active
256 render 'user_agreements/index'
263 return Rails.configuration.arvados_theme