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 => true})
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
26 # sets up the @user object for use by other methods. this is mostly called
27 # from the authorize method, but can be called elsewhere if authorisation
30 username, passwd = get_auth_data # parse from headers
31 # authenticate per-scheme
33 @user = nil # no authentication provided - perhaps first connect (client should retry after 401)
34 elsif username == 'token'
35 @user = User.authenticate(:token => passwd) # preferred - random token for user from db, passed in basic auth
37 @user = User.authenticate(:username => username, :password => passwd) # basic auth
41 def authorize(realm='Web Password', errormessage="Couldn't authenticate you")
42 # make the @user object from any auth sources we have
45 # handle authenticate pass/fail
47 # no auth, the user does not exist or the password was wrong
48 response.headers["Status"] = "Unauthorized"
49 response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
50 render :text => errormessage, :status => :unauthorized
55 def check_database_availability(need_api = false)
56 if OSM_STATUS == :database_offline or (need_api and OSM_STATUS == :api_offline)
57 redirect_to :controller => 'site', :action => 'offline'
61 def check_read_availability
62 if OSM_STATUS == :database_offline or OSM_STATUS == :api_offline
63 response.headers['Error'] = "Database offline for maintenance"
64 render :nothing => true, :status => :service_unavailable
69 def check_write_availability
70 if OSM_STATUS == :database_offline or OSM_STATUS == :api_offline or OSM_STATUS == :api_readonly
71 response.headers['Error'] = "Database offline for maintenance"
72 render :nothing => true, :status => :service_unavailable
77 # Report and error to the user
78 # (If anyone ever fixes Rails so it can set a http status "reason phrase",
79 # rather than only a status code and having the web engine make up a
80 # phrase from that, we can also put the error message into the status
81 # message. For now, rails won't let us)
82 def report_error(message)
83 render :text => message, :status => :bad_request
84 # Todo: some sort of escaping of problem characters in the message
85 response.headers['Error'] = message
90 # extract authorisation credentials from headers, returns user = nil if none
92 if request.env.has_key? 'X-HTTP_AUTHORIZATION' # where mod_rewrite might have put it
93 authdata = request.env['X-HTTP_AUTHORIZATION'].to_s.split
94 elsif request.env.has_key? 'REDIRECT_X_HTTP_AUTHORIZATION' # mod_fcgi
95 authdata = request.env['REDIRECT_X_HTTP_AUTHORIZATION'].to_s.split
96 elsif request.env.has_key? 'HTTP_AUTHORIZATION' # regular location
97 authdata = request.env['HTTP_AUTHORIZATION'].to_s.split
99 # only basic authentication supported
100 if authdata and authdata[0] == 'Basic'
101 user, pass = Base64.decode64(authdata[1]).split(':',2)