| 1 |
module AuthenticatedSystem |
| 2 |
protected |
| 3 |
|
| 4 |
|
| 5 |
def logged_in? |
| 6 |
current_user != :false |
| 7 |
end |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
def current_user |
| 12 |
@current_user ||= (login_from_session || login_from_basic_auth || login_from_cookie || :false) |
| 13 |
end |
| 14 |
|
| 15 |
|
| 16 |
def current_user=(new_user) |
| 17 |
session[:user_id] = (new_user.nil? || new_user.is_a?(Symbol)) ? nil : new_user.id |
| 18 |
@current_user = new_user |
| 19 |
end |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
def authorized? |
| 34 |
logged_in? |
| 35 |
end |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def login_required |
| 52 |
authorized? || access_denied |
| 53 |
end |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
def access_denied |
| 64 |
respond_to do |accepts| |
| 65 |
accepts.html do |
| 66 |
store_location |
| 67 |
flash[:error] = "Action requires login" |
| 68 |
redirect_to :controller => '/sessions', :action => 'new' |
| 69 |
end |
| 70 |
accepts.xml do |
| 71 |
headers["Status"] = "Unauthorized" |
| 72 |
headers["WWW-Authenticate"] = %(Basic realm="Web Password") |
| 73 |
render :text => "Could't authenticate you", :status => '401 Unauthorized' |
| 74 |
end |
| 75 |
end |
| 76 |
false |
| 77 |
end |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
def store_location |
| 83 |
session[:return_to] = request.request_uri |
| 84 |
end |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
def redirect_back_or_default(default) |
| 89 |
session[:return_to] ? redirect_to(session[:return_to]) : redirect_to(default) |
| 90 |
session[:return_to] = nil |
| 91 |
end |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
def self.included(base) |
| 96 |
base.send :helper_method, :current_user, :logged_in? |
| 97 |
end |
| 98 |
|
| 99 |
|
| 100 |
def login_from_session |
| 101 |
self.current_user = User.find_by_id(session[:user_id]) if session[:user_id] |
| 102 |
end |
| 103 |
|
| 104 |
|
| 105 |
def login_from_basic_auth |
| 106 |
username, passwd = get_auth_data |
| 107 |
self.current_user = User.authenticate(username, passwd) if username && passwd |
| 108 |
end |
| 109 |
|
| 110 |
|
| 111 |
def login_from_cookie |
| 112 |
user = cookies[:auth_token] && User.find_by_remember_token(cookies[:auth_token]) |
| 113 |
if user && user.remember_token? |
| 114 |
user.remember_me |
| 115 |
cookies[:auth_token] = { :value => user.remember_token, :expires => user.remember_token_expires_at } |
| 116 |
self.current_user = user |
| 117 |
end |
| 118 |
end |
| 119 |
|
| 120 |
private |
| 121 |
@@http_auth_headers = %w(X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION Authorization) |
| 122 |
|
| 123 |
def get_auth_data |
| 124 |
auth_key = @@http_auth_headers.detect { |h| request.env.has_key?(h) } |
| 125 |
auth_data = request.env[auth_key].to_s.split unless auth_key.blank? |
| 126 |
return auth_data && auth_data[0] == 'Basic' ? Base64.decode64(auth_data[1]).split(':')[0..1] : [nil, nil] |
| 127 |
end |
| 128 |
end |