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 :with => :render_exception
11 rescue_from ActiveRecord::RecordNotFound,
12 :with => :render_not_found
13 rescue_from ActionController::RoutingError,
14 :with => :render_not_found
15 rescue_from ActionController::UnknownController,
16 :with => :render_not_found
17 rescue_from ::AbstractController::ActionNotFound,
18 :with => :render_not_found
21 def unprocessable(message=nil)
23 @errors << message if message
24 render_error status: 422
27 def render_error(opts)
29 f.html { render opts.merge(controller: 'application', action: 'error') }
30 f.json { render opts.merge(json: {success: false, errors: @errors}) }
34 def render_exception(e)
35 logger.error e.inspect
36 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
37 if @object.andand.errors.andand.full_messages.andand.any?
38 @errors = @object.errors.full_messages
42 self.render_error status: 422
45 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
46 logger.error e.inspect
47 @errors = ["Path not found"]
48 self.render_error status: 404
53 @objects ||= model_class.all
55 f.json { render json: @objects }
62 return render_not_found("object not found")
65 f.json { render json: @object }
67 if request.method == 'GET'
70 redirect_to params[:return_to] || @object
78 return render_not_found("object not found")
83 @object = model_class.new
87 updates = params[@object.class.to_s.underscore.singularize.to_sym]
88 updates.keys.each do |attr|
89 if @object.send(attr).is_a? Hash and updates[attr].is_a? String
90 updates[attr] = Oj.load updates[attr]
93 if @object.update_attributes updates
96 self.render_error status: 422
101 @object ||= model_class.new params[model_class.to_s.singularize.to_sym]
103 redirect_to(params[:return_to] || @object)
108 redirect_to(params[:return_to] || :back)
110 self.render_error status: 422
115 if Thread.current[:arvados_api_token]
116 Thread.current[:user] ||= User.current
118 logger.error "No API token in Thread"
124 controller_name.classify.constantize
129 def find_object_by_uuid
130 if params[:id] and params[:id].match /\D/
131 params[:uuid] = params.delete :id
133 if params[:uuid].is_a? String
134 @object = model_class.find(params[:uuid])
136 @object = model_class.where(uuid: params[:uuid]).first
141 Thread.current[:arvados_api_token] = nil
142 Thread.current[:user] = nil
146 def thread_with_api_token(login_optional = false)
148 try_redirect_to_login = true
149 if params[:api_token]
150 try_redirect_to_login = false
151 Thread.current[:arvados_api_token] = params[:api_token]
152 # Before copying the token into session[], do a simple API
153 # call to verify its authenticity.
155 session[:arvados_api_token] = params[:api_token]
156 if !request.format.json? and request.method == 'GET'
157 # Repeat this request with api_token in the (new) session
158 # cookie instead of the query string. This prevents API
159 # tokens from appearing in (and being inadvisedly copied
160 # and pasted from) browser Location bars.
161 redirect_to request.fullpath.sub(%r{([&\?]api_token=)[^&\?]*}, '')
166 @errors = ['Invalid API token']
167 self.render_error status: 401
169 elsif session[:arvados_api_token]
170 # In this case, the token must have already verified at some
171 # point, but it might have been revoked since. We'll try
172 # using it, and catch the exception if it doesn't work.
173 try_redirect_to_login = false
174 Thread.current[:arvados_api_token] = session[:arvados_api_token]
177 rescue ArvadosApiClient::NotLoggedInException
178 try_redirect_to_login = true
181 logger.debug "No token received, session is #{session.inspect}"
183 if try_redirect_to_login
184 unless login_optional
187 if request.method == 'GET'
188 redirect_to $arvados_api_client.arvados_login_url(return_to: request.url)
190 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."
195 @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.']
196 self.render_error status: 422
200 # login is optional for this route so go on to the regular controller
201 Thread.current[:arvados_api_token] = nil
206 # Remove token in case this Thread is used for anything else.
207 Thread.current[:arvados_api_token] = nil
211 def thread_with_optional_api_token
212 thread_with_api_token(true) do
219 Link.where(uuid: 'just-verifying-my-api-token')
221 rescue ArvadosApiClient::NotLoggedInException
226 def ensure_current_user_is_admin
227 unless current_user and current_user.is_admin
228 @errors = ['Permission denied']
229 self.render_error status: 401
233 def check_user_agreements
234 if current_user && !current_user.is_active && current_user.is_invited
235 signatures = UserAgreement.signatures
236 @signed_ua_uuids = UserAgreement.signatures.map &:head_uuid
237 @required_user_agreements = UserAgreement.all.map do |ua|
238 if not @signed_ua_uuids.index ua.uuid
239 Collection.find(ua.uuid)
242 if @required_user_agreements.empty?
243 # No agreements to sign. Perhaps we just need to ask?
244 current_user.activate
245 if !current_user.is_active
246 logger.warn "#{current_user.uuid.inspect}: " +
247 "No user agreements to sign, but activate failed!"
250 if !current_user.is_active
251 render 'user_agreements/index'