1 class ApplicationController < ActionController::Base
3 before_filter :uncamelcase_params_hash_keys
4 before_filter :find_object_by_uuid, :except => :index
5 before_filter :authenticate_api_token
7 before_filter :set_remote_ip
8 before_filter :login_required
10 before_filter :catch_redirect_hint
12 def catch_redirect_hint
14 if params.has_key?('redirect_to') then
15 session[:redirect_to] = params[:redirect_to]
23 respond_to do |format|
25 redirect_to '/auth/joshid'
28 render :json => { 'error' => 'Not logged in' }.to_json
35 return nil unless session[:user_id]
36 @current_user ||= User.find(session[:user_id]) rescue nil
40 before_filter :set_remote_ip
41 before_filter :login_required
46 respond_to do |format|
48 redirect_to '/auth/joshid'
51 render :json => { 'error' => 'Not logged in' }.to_json
58 return nil unless session[:user_id]
59 @current_user ||= User.find(session[:user_id]) rescue nil
63 before_filter :set_remote_ip
64 before_filter :login_required
69 respond_to do |format|
71 redirect_to '/auth/joshid'
74 render :json => { 'error' => 'Not logged in' }.to_json
81 return nil unless session[:user_id]
82 @current_user ||= User.find(session[:user_id]) rescue nil
86 unless Rails.application.config.consider_all_requests_local
87 rescue_from Exception,
88 :with => :render_error
89 rescue_from ActiveRecord::RecordNotFound,
90 :with => :render_not_found
91 rescue_from ActionController::RoutingError,
92 :with => :render_not_found
93 rescue_from ActionController::UnknownController,
94 :with => :render_not_found
95 rescue_from ActionController::UnknownAction,
96 :with => :render_not_found
100 logger.error e.inspect
101 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
102 if @object and @object.errors and @object.errors.full_messages
103 errors = @object.errors.full_messages
107 render json: { errors: errors }, status: 422
110 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
111 logger.error e.inspect
112 render json: { errors: ["Path not found"] }, status: 404
116 @objects ||= if params[:where]
117 where = params[:where]
118 where = JSON.parse(where) if where.is_a?(String)
120 where.each do |attr,value|
122 attr.to_s.match(/^[a-z][_a-z0-9]+$/) and
123 model_class.columns.collect(&:name).index(attr))
125 conditions[0] << " and #{attr} in (?)"
128 conditions[0] << " and #{attr}=?"
133 model_class.where(*conditions)
135 @objects ||= model_class.all
136 if params[:eager] and params[:eager] != '0' and params[:eager] != 0 and params[:eager] != ''
137 @objects.each(&:eager_load_associations)
144 render json: @object.as_api_response(:superuser)
146 render_not_found("object not found")
151 @attrs = params[resource_name]
153 raise "no #{resource_name} (or #{resource_name.camelcase(:lower)}) provided with request #{params.inspect}"
155 if @attrs.class == String
156 @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
158 @object = model_class.new @attrs
164 @attrs = params[resource_name]
165 if @attrs.is_a? String
166 @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
168 @object.update_attributes @attrs
175 controller_name.classify.constantize
178 def resource_name # params[] key used by client
179 controller_name.singularize
182 def find_object_by_uuid
183 if params[:id] and params[:id].match /\D/
184 params[:uuid] = params.delete :id
186 @object = model_class.where('uuid=?', params[:uuid]).first
189 def self.accept_attribute_as_json(attr, force_class=nil)
190 before_filter lambda { accept_attribute_as_json attr, force_class }
192 def accept_attribute_as_json(attr, force_class)
193 if params[resource_name].is_a? Hash
194 if params[resource_name][attr].is_a? String
195 params[resource_name][attr] = JSON.parse params[resource_name][attr]
196 if force_class and !params[resource_name][attr].is_a? force_class
197 raise TypeError.new("#{resource_name}[#{attr.to_s}] must be a #{force_class.to_s}")
203 def uncamelcase_params_hash_keys
204 self.params = uncamelcase_hash_keys(params)
207 def uncamelcase_hash_keys(h, max_depth=-1)
208 if h.is_a? Hash and max_depth != 0
213 elsif k.class == Symbol
214 nk = k.to_s.underscore.to_sym
218 nh[nk] = uncamelcase_hash_keys(v, max_depth-1)
227 :kind => "orvos##{resource_name}List",
230 :next_page_token => "",
232 :items => @objects.as_api_response(:superuser)
234 render json: @object_list
237 def authenticate_api_token
238 unless Rails.configuration.
240 has_key?(params[:api_token] ||
242 render_error(Exception.new("Invalid API token"))
248 # Caveat: this is highly dependent on the proxy setup. YMMV.
249 if request.headers.has_key?('HTTP_X_REAL_IP') then
250 # We're behind a reverse proxy
251 @remote_ip = request.headers['HTTP_X_REAL_IP']
253 # Hopefully, we are not!
254 @remote_ip = request.env['REMOTE_ADDR']