]> git.openstreetmap.org Git - rails.git/blob - app/controllers/application.rb
pass common API error text through to Potlatch so it can be shown to the user
[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 => true})
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   ##
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
28   # is optional.
29   def setup_user_auth
30     username, passwd = get_auth_data # parse from headers
31     # authenticate per-scheme
32     if username.nil?
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
36     else
37       @user = User.authenticate(:username => username, :password => passwd) # basic auth
38     end
39   end
40
41   def authorize(realm='Web Password', errormessage="Couldn't authenticate you") 
42     # make the @user object from any auth sources we have
43     setup_user_auth
44
45     # handle authenticate pass/fail
46     unless @user
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
51       return false
52     end 
53   end 
54
55   def check_database_readable(need_api = false)
56     if OSM_STATUS == :database_offline or (need_api and OSM_STATUS == :api_offline)
57       redirect_to :controller => 'site', :action => 'offline'
58     end
59   end
60
61   def check_database_writable(need_api = false)
62     if OSM_STATUS == :database_offline or OSM_STATUS == :database_readonly or
63        (need_api and (OSM_STATUS == :api_offline or OSM_STATUS == :api_readonly))
64       redirect_to :controller => 'site', :action => 'offline'
65     end
66   end
67
68   def check_api_readable
69     if OSM_STATUS == :database_offline or OSM_STATUS == :api_offline
70       response.headers['Error'] = "Database offline for maintenance"
71       render :nothing => true, :status => :service_unavailable
72       return false
73     end
74   end
75
76   def check_api_writable
77     if OSM_STATUS == :database_offline or OSM_STATUS == :database_readonly or
78        OSM_STATUS == :api_offline or OSM_STATUS == :api_readonly
79       response.headers['Error'] = "Database offline for maintenance"
80       render :nothing => true, :status => :service_unavailable
81       return false
82     end
83   end
84
85   def require_public_data
86     unless @user.data_public?
87       response.headers['Error'] = "You must make your edits public to upload new data"
88       render :nothing => true, :status => :forbidden
89       return false
90     end
91   end
92
93   # Report and error to the user
94   # (If anyone ever fixes Rails so it can set a http status "reason phrase",
95   #  rather than only a status code and having the web engine make up a 
96   #  phrase from that, we can also put the error message into the status
97   #  message. For now, rails won't let us)
98   def report_error(message)
99     # Todo: some sort of escaping of problem characters in the message
100     response.headers['Error'] = message
101     render :text => message, :status => :bad_request
102   end
103
104 private 
105
106   # extract authorisation credentials from headers, returns user = nil if none
107   def get_auth_data 
108     if request.env.has_key? 'X-HTTP_AUTHORIZATION'          # where mod_rewrite might have put it 
109       authdata = request.env['X-HTTP_AUTHORIZATION'].to_s.split 
110     elsif request.env.has_key? 'REDIRECT_X_HTTP_AUTHORIZATION'          # mod_fcgi 
111       authdata = request.env['REDIRECT_X_HTTP_AUTHORIZATION'].to_s.split 
112     elsif request.env.has_key? 'HTTP_AUTHORIZATION'         # regular location
113       authdata = request.env['HTTP_AUTHORIZATION'].to_s.split
114     end 
115     # only basic authentication supported
116     if authdata and authdata[0] == 'Basic' 
117       user, pass = Base64.decode64(authdata[1]).split(':',2)
118     end 
119     return [user, pass] 
120   end 
121
122 end