1 # Filters added to this controller will be run for all controllers in the application.
2 # Likewise, all the methods added will be available for all controllers.
3 class ApplicationController < ActionController::Base
5 if OSM_STATUS == :database_offline
11 @user = User.find(session[:user], :conditions => "visible = 1")
13 @user = User.authenticate(:token => session[:token])
14 session[:user] = @user.id
16 rescue Exception => ex
17 logger.info("Exception authorizing user: #{ex.to_s}")
22 redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri unless @user
25 def authorize(realm='Web Password', errormessage="Couldn't authenticate you")
26 username, passwd = get_auth_data # parse from headers
27 # authenticate per-scheme
29 @user = nil # no authentication provided - perhaps first connect (client should retry after 401)
30 elsif username == 'token'
31 @user = User.authenticate(:token => passwd) # preferred - random token for user from db, passed in basic auth
33 @user = User.authenticate(:username => username, :password => passwd) # basic auth
36 # handle authenticate pass/fail
38 # no auth, the user does not exist or the password was wrong
39 response.headers["Status"] = "Unauthorized"
40 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
41 render :text => errormessage, :status => :unauthorized
46 def check_database_availability(need_api = false)
47 if OSM_STATUS == :database_offline or (need_api and OSM_STATUS == :api_offline)
48 redirect_to :controller => 'site', :action => 'offline'
52 def check_read_availability
53 if OSM_STATUS == :database_offline or OSM_STATUS == :api_offline
54 response.headers['Error'] = "Database offline for maintenance"
55 render :nothing => true, :status => :service_unavailable
60 def check_write_availability
61 if OSM_STATUS == :database_offline or OSM_STATUS == :api_offline or OSM_STATUS == :api_readonly
62 response.headers['Error'] = "Database offline for maintenance"
63 render :nothing => true, :status => :service_unavailable
68 # Report and error to the user
69 # (If anyone ever fixes Rails so it can set a http status "reason phrase",
70 # rather than only a status code and having the web engine make up a
71 # phrase from that, we can also put the error message into the status
72 # message. For now, rails won't let us)
73 def report_error(message)
74 render :text => message, :status => :bad_request
75 # Todo: some sort of escaping of problem characters in the message
76 response.headers['Error'] = message
81 # extract authorisation credentials from headers, returns user = nil if none
83 if request.env.has_key? 'X-HTTP_AUTHORIZATION' # where mod_rewrite might have put it
84 authdata = request.env['X-HTTP_AUTHORIZATION'].to_s.split
85 elsif request.env.has_key? 'REDIRECT_X_HTTP_AUTHORIZATION' # mod_fcgi
86 authdata = request.env['REDIRECT_X_HTTP_AUTHORIZATION'].to_s.split
87 elsif request.env.has_key? 'HTTP_AUTHORIZATION' # regular location
88 authdata = request.env['HTTP_AUTHORIZATION'].to_s.split
90 # only basic authentication supported
91 if authdata and authdata[0] == 'Basic'
92 user, pass = Base64.decode64(authdata[1]).split(':',2)