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, :except => :render_not_found
7 before_filter :set_remote_ip
8 before_filter :login_required, :except => :render_not_found
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 unless Rails.application.config.consider_all_requests_local
41 rescue_from Exception,
42 :with => :render_error
43 rescue_from ActiveRecord::RecordNotFound,
44 :with => :render_not_found
45 rescue_from ActionController::RoutingError,
46 :with => :render_not_found
47 rescue_from ActionController::UnknownController,
48 :with => :render_not_found
49 rescue_from ActionController::UnknownAction,
50 :with => :render_not_found
54 logger.error e.inspect
55 logger.error e.backtrace.collect { |x| x + "\n" }.join('') if e.backtrace
56 if @object and @object.errors and @object.errors.full_messages
57 errors = @object.errors.full_messages
61 render json: { errors: errors }, status: 422
64 def render_not_found(e=ActionController::RoutingError.new("Path not found"))
65 logger.error e.inspect
66 render json: { errors: ["Path not found"] }, status: 404
70 @objects ||= if params[:where]
71 where = params[:where]
72 where = JSON.parse(where) if where.is_a?(String)
74 where.each do |attr,value|
76 attr.to_s.match(/^[a-z][_a-z0-9]+$/) and
77 model_class.columns.collect(&:name).index(attr))
79 conditions[0] << " and #{attr} in (?)"
82 conditions[0] << " and #{attr}=?"
87 model_class.where(*conditions)
89 @objects ||= model_class.all
90 if params[:eager] and params[:eager] != '0' and params[:eager] != 0 and params[:eager] != ''
91 @objects.each(&:eager_load_associations)
98 render json: @object.as_api_response(:superuser)
100 render_not_found("object not found")
105 @attrs = params[resource_name]
107 raise "no #{resource_name} (or #{resource_name.camelcase(:lower)}) provided with request #{params.inspect}"
109 if @attrs.class == String
110 @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
112 @object = model_class.new @attrs
118 @attrs = params[resource_name]
119 if @attrs.is_a? String
120 @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
122 @object.update_attributes @attrs
129 controller_name.classify.constantize
132 def resource_name # params[] key used by client
133 controller_name.singularize
136 def find_object_by_uuid
137 if params[:id] and params[:id].match /\D/
138 params[:uuid] = params.delete :id
140 @object = model_class.where('uuid=?', params[:uuid]).first
143 def self.accept_attribute_as_json(attr, force_class=nil)
144 before_filter lambda { accept_attribute_as_json attr, force_class }
146 def accept_attribute_as_json(attr, force_class)
147 if params[resource_name].is_a? Hash
148 if params[resource_name][attr].is_a? String
149 params[resource_name][attr] = JSON.parse params[resource_name][attr]
150 if force_class and !params[resource_name][attr].is_a? force_class
151 raise TypeError.new("#{resource_name}[#{attr.to_s}] must be a #{force_class.to_s}")
157 def uncamelcase_params_hash_keys
158 self.params = uncamelcase_hash_keys(params)
161 def uncamelcase_hash_keys(h, max_depth=-1)
162 if h.is_a? Hash and max_depth != 0
167 elsif k.class == Symbol
168 nk = k.to_s.underscore.to_sym
172 nh[nk] = uncamelcase_hash_keys(v, max_depth-1)
181 :kind => "orvos##{resource_name}List",
184 :next_page_token => "",
186 :items => @objects.as_api_response(:superuser)
188 render json: @object_list
191 def authenticate_api_token
192 unless Rails.configuration.
194 has_key?(params[:api_token] ||
196 render_error(Exception.new("Invalid API token"))
202 # Caveat: this is highly dependent on the proxy setup. YMMV.
203 if request.headers.has_key?('HTTP_X_REAL_IP') then
204 # We're behind a reverse proxy
205 @remote_ip = request.headers['HTTP_X_REAL_IP']
207 # Hopefully, we are not!
208 @remote_ip = request.env['REMOTE_ADDR']