add User model features and uuid index
[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, :except => :render_not_found
6
7   before_filter :set_remote_ip
8   before_filter :login_required, :except => :render_not_found
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   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
51   end
52
53   def render_error(e)
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
58     else
59       errors = [e.inspect]
60     end
61     render json: { errors: errors }, status: 422
62   end
63
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
67   end
68
69   def index
70     @objects ||= if params[:where]
71       where = params[:where]
72       where = JSON.parse(where) if where.is_a?(String)
73       conditions = ['1=1']
74       where.each do |attr,value|
75         if (!value.nil? and
76             attr.to_s.match(/^[a-z][_a-z0-9]+$/) and
77             model_class.columns.collect(&:name).index(attr))
78           if value.is_a? Array
79             conditions[0] << " and #{attr} in (?)"
80             conditions << value
81           else
82             conditions[0] << " and #{attr}=?"
83             conditions << value
84           end
85         end
86       end
87       model_class.where(*conditions)
88     end
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)
92     end
93     render_list
94   end
95
96   def show
97     if @object
98       render json: @object.as_api_response(:superuser)
99     else
100       render_not_found("object not found")
101     end
102   end
103
104   def create
105     @attrs = params[resource_name]
106     if @attrs.nil?
107       raise "no #{resource_name} (or #{resource_name.camelcase(:lower)}) provided with request #{params.inspect}"
108     end
109     if @attrs.class == String
110       @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
111     end
112     @object = model_class.new @attrs
113     @object.save
114     show
115   end
116
117   def update
118     @attrs = params[resource_name]
119     if @attrs.is_a? String
120       @attrs = uncamelcase_hash_keys(JSON.parse @attrs)
121     end
122     @object.update_attributes @attrs
123     show
124   end
125
126   protected
127
128   def model_class
129     controller_name.classify.constantize
130   end
131
132   def resource_name             # params[] key used by client
133     controller_name.singularize
134   end
135
136   def find_object_by_uuid
137     if params[:id] and params[:id].match /\D/
138       params[:uuid] = params.delete :id
139     end
140     @object = model_class.where('uuid=?', params[:uuid]).first
141   end
142
143   def self.accept_attribute_as_json(attr, force_class=nil)
144     before_filter lambda { accept_attribute_as_json attr, force_class }
145   end
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}")
152         end
153       end
154     end
155   end
156
157   def uncamelcase_params_hash_keys
158     self.params = uncamelcase_hash_keys(params)
159   end
160
161   def uncamelcase_hash_keys(h, max_depth=-1)
162     if h.is_a? Hash and max_depth != 0
163       nh = Hash.new
164       h.each do |k,v|
165         if k.class == String
166           nk = k.underscore
167         elsif k.class == Symbol
168           nk = k.to_s.underscore.to_sym
169         else
170           nk = k
171         end
172         nh[nk] = uncamelcase_hash_keys(v, max_depth-1)
173       end
174       h.replace(nh)
175     end
176     h
177   end
178
179   def render_list
180     @object_list = {
181       :kind  => "orvos##{resource_name}List",
182       :etag => "",
183       :self_link => "",
184       :next_page_token => "",
185       :next_link => "",
186       :items => @objects.as_api_response(:superuser)
187     }
188     render json: @object_list
189   end
190
191   def authenticate_api_token
192     unless Rails.configuration.
193       accept_api_token.
194       has_key?(params[:api_token] ||
195                cookies[:api_token])
196       render_error(Exception.new("Invalid API token"))
197     end
198   end
199
200 private
201   def set_remote_ip
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']
206     else
207       # Hopefully, we are not!
208       @remote_ip = request.env['REMOTE_ADDR']
209     end
210   end
211
212 end