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
6 @user = User.find_by_token(session[:token])
9 def authorize(realm='Web Password', errormessage="Could't authenticate you")
10 username, passwd = get_auth_data
13 if @user = User.authenticate(username, passwd)
14 # user exists and password is correct ... horray!
15 if @user.methods.include? 'lastlogin'
17 @session['lastlogin'] = user.lastlogin
18 @user.last.login = Time.now
20 @session["User.id"] = @user.id
23 # the user does not exist or the password was wrong
24 @response.headers["Status"] = "Unauthorized"
25 @response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
26 render_text(errormessage, 401)
33 # extract authorisation credentials
34 if request.env.has_key? 'X-HTTP_AUTHORIZATION'
35 # try to get it where mod_rewrite might have put it
36 authdata = @request.env['X-HTTP_AUTHORIZATION'].to_s.split
37 elsif request.env.has_key? 'HTTP_AUTHORIZATION'
38 # this is the regular location
39 authdata = @request.env['HTTP_AUTHORIZATION'].to_s.split
42 # at the moment we only support basic authentication
43 if authdata and authdata[0] == 'Basic'
44 user, pass = Base64.decode64(authdata[1]).split(':')[0..1]