]> git.openstreetmap.org Git - rails.git/blob - app/controllers/application.rb
Add a "database readonly" state that allows all writes to the database
[rails.git] / app / controllers / application.rb
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
4
5   if OSM_STATUS == :database_readonly or OSM_STATUS == :database_offline
6     session :off
7   end
8
9   def authorize_web
10     if session[:user]
11       @user = User.find(session[:user], :conditions => "visible = 1")
12     elsif session[:token]
13       @user = User.authenticate(:token => session[:token])
14       session[:user] = @user.id
15     end
16   rescue Exception => ex
17     logger.info("Exception authorizing user: #{ex.to_s}")
18     @user = nil
19   end
20
21   def require_user
22     redirect_to :controller => 'user', :action => 'login', :referer => request.request_uri unless @user
23   end
24
25   def authorize(realm='Web Password', errormessage="Couldn't authenticate you") 
26     username, passwd = get_auth_data # parse from headers
27     # authenticate per-scheme
28     if username.nil?
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
32     else
33       @user = User.authenticate(:username => username, :password => passwd) # basic auth
34     end
35
36     # handle authenticate pass/fail
37     unless @user
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
42       return false
43     end 
44   end 
45
46   def check_database_readable(need_api = false)
47     if OSM_STATUS == :database_offline or (need_api and OSM_STATUS == :api_offline)
48       redirect_to :controller => 'site', :action => 'offline'
49     end
50   end
51
52   def check_database_writable(need_api = false)
53     if OSM_STATUS == :database_offline or OSM_STATUS == :database_readonly or
54        (need_api and (OSM_STATUS == :api_offline or OSM_STATUS == :api_readonly))
55       redirect_to :controller => 'site', :action => 'offline'
56     end
57   end
58
59   def check_api_readable
60     if OSM_STATUS == :database_offline or OSM_STATUS == :api_offline
61       response.headers['Error'] = "Database offline for maintenance"
62       render :nothing => true, :status => :service_unavailable
63       return false
64     end
65   end
66
67   def check_api_writable
68     if OSM_STATUS == :database_offline or OSM_STATUS == :database_readonly or
69        OSM_STATUS == :api_offline or OSM_STATUS == :api_readonly
70       response.headers['Error'] = "Database offline for maintenance"
71       render :nothing => true, :status => :service_unavailable
72       return false
73     end
74   end
75
76   # Report and error to the user
77   # (If anyone ever fixes Rails so it can set a http status "reason phrase",
78   #  rather than only a status code and having the web engine make up a 
79   #  phrase from that, we can also put the error message into the status
80   #  message. For now, rails won't let us)
81   def report_error(message)
82     render :nothing => true, :status => :bad_request
83     # Todo: some sort of escaping of problem characters in the message
84     response.headers['Error'] = message
85   end
86
87 private 
88
89   # extract authorisation credentials from headers, returns user = nil if none
90   def get_auth_data 
91     if request.env.has_key? 'X-HTTP_AUTHORIZATION'          # where mod_rewrite might have put it 
92       authdata = request.env['X-HTTP_AUTHORIZATION'].to_s.split 
93     elsif request.env.has_key? 'HTTP_AUTHORIZATION'         # regular location
94       authdata = request.env['HTTP_AUTHORIZATION'].to_s.split
95     end 
96     # only basic authentication supported
97     if authdata and authdata[0] == 'Basic' 
98       user, pass = Base64.decode64(authdata[1]).split(':',2)
99     end 
100     return [user, pass] 
101   end 
102
103 end