Authenticate against auth.clinicalfuture.com
[arvados.git] / app / controllers / application_controller.rb
1 class ApplicationController < ActionController::Base
2   protect_from_forgery
3   before_filter :uncamelcase_params_hash_keys
4   before_filter :find_object_by_uuid, :except => :index
5   before_filter :authenticate_api_token
6
7   before_filter :set_remote_ip
8   before_filter :login_required
9
10   before_filter :catch_redirect_hint
11
12   def catch_redirect_hint
13     if !current_user
14       if params.has_key?('redirect_to') then
15         session[:redirect_to] = params[:redirect_to]
16       end
17     end
18   end
19
20   # Authentication
21   def login_required
22     if !current_user
23       respond_to do |format|
24         format.html  {
25           redirect_to '/auth/joshid'
26         }
27         format.json {
28           render :json => { 'error' => 'Not logged in' }.to_json
29         }
30       end
31     end
32   end
33
34   def current_user
35     return nil unless session[:user_id]
36     @current_user ||= User.find(session[:user_id]) rescue nil
37   end
38   # /Authentication
39
40   before_filter :set_remote_ip
41   before_filter :login_required
42
43   # Authentication
44   def login_required
45     if !current_user
46       respond_to do |format|
47         format.html  {
48           redirect_to '/auth/joshid'
49         }
50         format.json {
51           render :json => { 'error' => 'Not logged in' }.to_json
52         }
53       end
54     end
55   end
56
57   def current_user
58     return nil unless session[:user_id]
59     @current_user ||= User.find(session[:user_id]) rescue nil
60   end
61   # /Authentication
62
63   before_filter :set_remote_ip
64   before_filter :login_required
65
66   # Authentication
67   def login_required
68     if !current_user
69       respond_to do |format|
70         format.html  {
71           redirect_to '/auth/joshid'
72         }
73         format.json {
74           render :json => { 'error' => 'Not logged in' }.to_json
75         }
76       end
77     end
78   end
79
80   def current_user
81     return nil unless session[:user_id]
82     @current_user ||= User.find(session[:user_id]) rescue nil
83   end
84   # /Authentication
85
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
97   end
98
99   def render_error(e)
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
104     else
105       errors = [e.inspect]
106     end
107     render json: { errors: errors }, status: 422
108   end
109
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
113   end
114
115   def index
116     @objects ||= if params[:where]
117       where = params[:where]
118       where = JSON.parse(where) if where.is_a?(String)
119       conditions = ['1=1']
120       where.each do |attr,value|
121         if (!value.nil? and
122             attr.to_s.match(/^[a-z][_a-z0-9]+$/) and
123             model_class.columns.collect(&:name).index(attr))
124           if value.is_a? Array
125             conditions[0] << " and #{attr} in (?)"
126             conditions << value
127           else
128             conditions[0] << " and #{attr}=?"
129             conditions << value
130           end
131         end
132       end
133       model_class.where(*conditions)
134     end
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)
138     end
139     render_list
140   end
141
142   def show
143     if @object
144       render json: @object.as_api_response(:superuser)
145     else
146       render_not_found("object not found")
147     end
148   end
149
150   def create
151     @attrs = params[resource_name]
152     if @attrs.nil?
153       raise "no #{resource_name} (or #{resource_name.camelcase(:lower)}) provided with request #{params.inspect}"
154     end
155     if @attrs.class == String
156       @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
157     end
158     @object = model_class.new @attrs
159     @object.save
160     show
161   end
162
163   def update
164     @attrs = params[resource_name]
165     if @attrs.is_a? String
166       @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
167     end
168     @object.update_attributes @attrs
169     show
170   end
171
172   protected
173
174   def model_class
175     controller_name.classify.constantize
176   end
177
178   def resource_name             # params[] key used by client
179     controller_name.singularize
180   end
181
182   def find_object_by_uuid
183     if params[:id] and params[:id].match /\D/
184       params[:uuid] = params.delete :id
185     end
186     @object = model_class.where('uuid=?', params[:uuid]).first
187   end
188
189   def self.accept_attribute_as_json(attr, force_class=nil)
190     before_filter lambda { accept_attribute_as_json attr, force_class }
191   end
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}")
198         end
199       end
200     end
201   end
202
203   def uncamelcase_params_hash_keys
204     self.params = uncamelcase_hash_keys(params)
205   end
206
207   def uncamelcase_hash_keys(h, max_depth=-1)
208     if h.is_a? Hash and max_depth != 0
209       nh = Hash.new
210       h.each do |k,v|
211         if k.class == String
212           nk = k.underscore
213         elsif k.class == Symbol
214           nk = k.to_s.underscore.to_sym
215         else
216           nk = k
217         end
218         nh[nk] = uncamelcase_hash_keys(v, max_depth-1)
219       end
220       h.replace(nh)
221     end
222     h
223   end
224
225   def render_list
226     @object_list = {
227       :kind  => "orvos##{resource_name}List",
228       :etag => "",
229       :self_link => "",
230       :next_page_token => "",
231       :next_link => "",
232       :items => @objects.as_api_response(:superuser)
233     }
234     render json: @object_list
235   end
236
237   def authenticate_api_token
238     unless Rails.configuration.
239       accept_api_token.
240       has_key?(params[:api_token] ||
241                cookies[:api_token])
242       render_error(Exception.new("Invalid API token"))
243     end
244   end
245
246 private
247   def set_remote_ip
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']
252     else
253       # Hopefully, we are not!
254       @remote_ip = request.env['REMOTE_ADDR']
255     end
256   end
257
258 end