1 class ApplicationController < ActionController::Base
3 around_filter :thread_with_api_token, :except => [:render_exception, :render_not_found]
4 before_filter :find_object_by_uuid, :except => [:index, :render_exception, :render_not_found]
6 unless Rails.application.config.consider_all_requests_local
8 :with => :render_exception
9 rescue_from ActiveRecord::RecordNotFound,
10 :with => :render_not_found
11 rescue_from ActionController::RoutingError,
12 :with => :render_not_found
13 rescue_from ActionController::UnknownController,
14 :with => :render_not_found
15 rescue_from ActionController::UnknownAction,
16 :with => :render_not_found
19 def unprocessable(message=nil)
21 @errors << message if message
22 render_error status: 422
25 def render_error(opts)
27 f.html { render opts.merge(controller: 'application', action: 'error') }
28 f.json { render opts.merge(json: {success: false, errors: @errors}) }
32 def render_exception(e)
33 logger.error e.inspect
34 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
35 if @object and @object.errors and @object.errors.full_messages
36 @errors = @object.errors.full_messages
40 self.render_error status: 422
43 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
44 logger.error e.inspect
45 @errors = ["Path not found"]
46 self.render_error status: 404
51 @objects ||= model_class.all
53 f.json { render json: @objects }
59 render_not_found("object not found")
62 f.json { render json: @object }
68 if Thread.current[:orvos_api_token]
69 @current_user ||= User.current
71 logger.error "No API token in Thread"
79 controller_name.classify.constantize
82 def find_object_by_uuid
83 if params[:id] and params[:id].match /\D/
84 params[:uuid] = params.delete :id
86 @object = model_class.where(uuid: params[:uuid]).first
89 def thread_with_api_token
91 try_redirect_to_login = true
93 try_redirect_to_login = false
94 Thread.current[:orvos_api_token] = params[:api_token]
95 # Before copying the token into session[], do a simple API
96 # call to verify its authenticity.
98 session[:orvos_api_token] = params[:api_token]
99 if !request.format.json? and request.method == 'GET'
100 # Repeat this request with api_token in the (new) session
101 # cookie instead of the query string. This prevents API
102 # tokens from appearing in (and being inadvisedly copied
103 # and pasted from) browser Location bars.
104 redirect_to request.fullpath.sub(%r{([&\?]api_token=)[^&\?]*}, '')
109 @errors = ['Invalid API token']
110 self.render_error status: 401
112 elsif session[:orvos_api_token]
113 # In this case, the token must have already verified at some
114 # point, but it might have been revoked since. We'll try
115 # using it, and catch the exception if it doesn't work.
116 try_redirect_to_login = false
117 Thread.current[:orvos_api_token] = session[:orvos_api_token]
120 rescue OrvosApiClient::NotLoggedInException
121 try_redirect_to_login = true
124 if try_redirect_to_login
127 redirect_to $orvos_api_client.orvos_login_url(return_to: request.url)
130 @errors = ['No API token supplied -- can\'t really do anything.']
131 self.render_error status: 422
136 # Remove token in case this Thread is used for anything else.
137 Thread.current[:orvos_api_token] = nil
143 Link.where(uuid: 'just-verifying-my-api-token')
145 rescue OrvosApiClient::NotLoggedInException
150 def ensure_current_user_is_admin
151 unless current_user and current_user.is_admin
152 @errors = ['Permission denied']
153 self.render_error status: 401