1 class ApplicationController < ActionController::Base
2 include SessionPersistence
4 protect_from_forgery :with => :exception
6 rescue_from CanCan::AccessDenied, :with => :deny_access
9 before_action :fetch_body
10 around_action :better_errors_allow_inline, :if => proc { Rails.env.development? }
12 attr_accessor :current_user
13 helper_method :current_user
17 self.current_user = User.where(:id => session[:user]).where("status IN ('active', 'confirmed', 'suspended')").first
19 if current_user.status == "suspended"
21 session_expires_automatically
23 redirect_to :controller => "users", :action => "suspended"
25 # don't allow access to any auth-requiring part of the site unless
26 # the new CTs have been seen (and accept/decline chosen).
27 elsif !current_user.terms_seen && flash[:skip_terms].nil?
28 flash[:notice] = t "users.terms.you need to accept or decline"
30 redirect_to :controller => "users", :action => "terms", :referer => params[:referer]
32 redirect_to :controller => "users", :action => "terms", :referer => request.fullpath
36 session[:user] = current_user.id if self.current_user = User.authenticate(:token => session[:token])
38 rescue StandardError => ex
39 logger.info("Exception authorizing user: #{ex}")
41 self.current_user = nil
47 redirect_to :controller => "users", :action => "login", :referer => request.fullpath
55 @oauth = current_user.access_token(Settings.oauth_key) if current_user && Settings.key?(:oauth_key)
59 # require the user to have cookies enabled in their browser
61 if request.cookies["_osm_session"].to_s == ""
62 if params[:cookie_test].nil?
63 session[:cookie_test] = true
64 redirect_to params.to_unsafe_h.merge(:cookie_test => "true")
67 flash.now[:warning] = t "application.require_cookies.cookies_needed"
70 session.delete(:cookie_test)
75 # sets up the current_user for use by other methods. this is mostly called
76 # from the authorize method, but can be called elsewhere if authorisation
79 # try and setup using OAuth
80 unless Authenticator.new(self, [:token]).allow?
81 username, passwd = get_auth_data # parse from headers
82 # authenticate per-scheme
83 self.current_user = if username.nil?
84 nil # no authentication provided - perhaps first connect (client should retry after 401)
85 elsif username == "token"
86 User.authenticate(:token => passwd) # preferred - random token for user from db, passed in basic auth
88 User.authenticate(:username => username, :password => passwd) # basic auth
92 # have we identified the user?
94 # check if the user has been banned
95 user_block = current_user.blocks.active.take
96 unless user_block.nil?
98 if user_block.zero_hour?
99 report_error t("application.setup_user_auth.blocked_zero_hour"), :forbidden
101 report_error t("application.setup_user_auth.blocked"), :forbidden
105 # if the user hasn't seen the contributor terms then don't
106 # allow editing - they have to go to the web site and see
107 # (but can decline) the CTs to continue.
108 if !current_user.terms_seen && flash[:skip_terms].nil?
110 report_error t("application.setup_user_auth.need_to_see_terms"), :forbidden
115 def check_database_readable(need_api = false)
116 if Settings.status == "database_offline" || (need_api && Settings.status == "api_offline")
118 report_error "Database offline for maintenance", :service_unavailable
120 redirect_to :controller => "site", :action => "offline"
125 def check_database_writable(need_api = false)
126 if Settings.status == "database_offline" || Settings.status == "database_readonly" ||
127 (need_api && (Settings.status == "api_offline" || Settings.status == "api_readonly"))
129 report_error "Database offline for maintenance", :service_unavailable
131 redirect_to :controller => "site", :action => "offline"
136 def check_api_readable
137 if api_status == :offline
138 report_error "Database offline for maintenance", :service_unavailable
143 def check_api_writable
144 unless api_status == :online
145 report_error "Database offline for maintenance", :service_unavailable
151 if Settings.status == "database_offline"
153 elsif Settings.status == "database_readonly"
161 status = database_status
163 if Settings.status == "api_offline"
165 elsif Settings.status == "api_readonly"
173 status = database_status
174 status = :offline if status == :online && Settings.status == "gpx_offline"
178 def require_public_data
179 unless current_user.data_public?
180 report_error "You must make your edits public to upload new data", :forbidden
185 # Report and error to the user
186 # (If anyone ever fixes Rails so it can set a http status "reason phrase",
187 # rather than only a status code and having the web engine make up a
188 # phrase from that, we can also put the error message into the status
189 # message. For now, rails won't let us)
190 def report_error(message, status = :bad_request)
191 # TODO: some sort of escaping of problem characters in the message
192 response.headers["Error"] = message
194 if request.headers["X-Error-Format"]&.casecmp("xml")&.zero?
195 result = OSM::API.new.get_xml_doc
196 result.root.name = "osmError"
197 result.root << (XML::Node.new("status") << "#{Rack::Utils.status_code(status)} #{Rack::Utils::HTTP_STATUS_CODES[status]}")
198 result.root << (XML::Node.new("message") << message)
200 render :xml => result.to_s
202 render :plain => message, :status => status
206 def preferred_languages(reset = false)
207 @preferred_languages = nil if reset
208 @preferred_languages ||= if params[:locale]
209 Locale.list(params[:locale])
211 current_user.preferred_languages
213 Locale.list(http_accept_language.user_preferred_languages)
217 helper_method :preferred_languages
219 def set_locale(reset = false)
220 if current_user&.languages&.empty? && !http_accept_language.user_preferred_languages.empty?
221 current_user.languages = http_accept_language.user_preferred_languages
225 I18n.locale = Locale.available.preferred(preferred_languages(reset))
227 response.headers["Vary"] = "Accept-Language"
228 response.headers["Content-Language"] = I18n.locale.to_s
231 def api_call_handle_error
233 rescue ActiveRecord::RecordNotFound => ex
235 rescue LibXML::XML::Error, ArgumentError => ex
236 report_error ex.message, :bad_request
237 rescue ActiveRecord::RecordInvalid => ex
238 message = "#{ex.record.class} #{ex.record.id}: "
239 ex.record.errors.each { |attr, msg| message << "#{attr}: #{msg} (#{ex.record[attr].inspect})" }
240 report_error message, :bad_request
241 rescue OSM::APIError => ex
242 report_error ex.message, ex.status
243 rescue AbstractController::ActionNotFound => ex
245 rescue StandardError => ex
246 logger.info("API threw unexpected #{ex.class} exception: #{ex.message}")
247 ex.backtrace.each { |l| logger.info(l) }
248 report_error "#{ex.class}: #{ex.message}", :internal_server_error
252 # asserts that the request method is the +method+ given as a parameter
253 # or raises a suitable error. +method+ should be a symbol, e.g: :put or :get.
254 def assert_method(method)
255 ok = request.send((method.to_s.downcase + "?").to_sym)
256 raise OSM::APIBadMethodError, method unless ok
260 # wrap an api call in a timeout
262 OSM::Timer.timeout(Settings.api_timeout, Timeout::Error) do
265 rescue Timeout::Error
266 raise OSM::APITimeoutError
270 # wrap a web page in a timeout
272 OSM::Timer.timeout(Settings.web_timeout, Timeout::Error) do
275 rescue ActionView::Template::Error => ex
278 if ex.is_a?(Timeout::Error) ||
279 (ex.is_a?(ActiveRecord::StatementInvalid) && ex.message =~ /execution expired/)
280 render :action => "timeout"
284 rescue Timeout::Error
285 render :action => "timeout"
289 # ensure that there is a "user" instance variable
291 render_unknown_user params[:display_name] unless @user = User.active.find_by(:display_name => params[:display_name])
295 # render a "no such user" page
296 def render_unknown_user(name)
297 @title = t "users.no_such_user.title"
298 @not_found_user = name
300 respond_to do |format|
301 format.html { render :template => "users/no_such_user", :status => :not_found }
302 format.all { head :not_found }
307 # Unfortunately if a PUT or POST request that has a body fails to
308 # read it then Apache will sometimes fail to return the response it
309 # is given to the client properly, instead erroring:
311 # https://issues.apache.org/bugzilla/show_bug.cgi?id=44782
313 # To work round this we call rewind on the body here, which is added
314 # as a filter, to force it to be fetched from Apache into a file.
320 append_content_security_policy_directives(
321 :child_src => %w[http://127.0.0.1:8111 https://127.0.0.1:8112],
322 :frame_src => %w[http://127.0.0.1:8111 https://127.0.0.1:8112],
323 :connect_src => [Settings.nominatim_url, Settings.overpass_url, Settings.fossgis_osrm_url, Settings.graphhopper_url],
324 :form_action => %w[render.openstreetmap.org],
325 :style_src => %w['unsafe-inline']
328 if Settings.status == "database_offline" || Settings.status == "api_offline"
329 flash.now[:warning] = t("layouts.osm_offline")
330 elsif Settings.status == "database_readonly" || Settings.status == "api_readonly"
331 flash.now[:warning] = t("layouts.osm_read_only")
334 request.xhr? ? "xhr" : "map"
337 def allow_thirdparty_images
338 append_content_security_policy_directives(:img_src => %w[*])
342 editor = if params[:editor]
344 elsif current_user&.preferred_editor
345 current_user.preferred_editor
347 Settings.default_editor
353 helper_method :preferred_editor
356 if Settings.key?(:totp_key)
357 cookies["_osm_totp_token"] = {
358 :value => ROTP::TOTP.new(Settings.totp_key, :interval => 3600).now,
359 :domain => "openstreetmap.org",
360 :expires => 1.hour.from_now
365 def better_errors_allow_inline
368 append_content_security_policy_directives(
369 :script_src => %w['unsafe-inline'],
370 :style_src => %w['unsafe-inline']
377 # Use capabilities from the oauth token if it exists and is a valid access token
378 if Authenticator.new(self, [:token]).allow?
379 Ability.new(nil).merge(Capability.new(current_token))
381 Ability.new(current_user)
385 def deny_access(_exception)
388 report_error t("oauth.permissions.missing"), :forbidden
391 respond_to do |format|
392 format.html { redirect_to :controller => "errors", :action => "forbidden" }
393 format.any { report_error t("application.permission_denied"), :forbidden }
396 respond_to do |format|
397 format.html { redirect_to :controller => "users", :action => "login", :referer => request.fullpath }
398 format.any { head :forbidden }
407 # extract authorisation credentials from headers, returns user = nil if none
409 if request.env.key? "X-HTTP_AUTHORIZATION" # where mod_rewrite might have put it
410 authdata = request.env["X-HTTP_AUTHORIZATION"].to_s.split
411 elsif request.env.key? "REDIRECT_X_HTTP_AUTHORIZATION" # mod_fcgi
412 authdata = request.env["REDIRECT_X_HTTP_AUTHORIZATION"].to_s.split
413 elsif request.env.key? "HTTP_AUTHORIZATION" # regular location
414 authdata = request.env["HTTP_AUTHORIZATION"].to_s.split
416 # only basic authentication supported
417 user, pass = Base64.decode64(authdata[1]).split(":", 2) if authdata && authdata[0] == "Basic"
421 # override to stop oauth plugin sending errors
422 def invalid_oauth_response; end