Module: Sinatra::Security::Helpers
- Defined in:
- lib/sinatra/security/helpers.rb
Instance Method Summary collapse
- #__USER__ ⇒ Object
-
#authenticate(opts) ⇒ String?
Internally used by the POST /login route handler.
-
#current_user(finder = lambda { |id| __USER__[id] }) ⇒ User
Returns the currently logged in user, identified through session.
-
#ensure_current_user(user) ⇒ Object
Used for simple atomic authorization rules on a per action / route basis.
- #logged_in? ⇒ true, false
-
#logout! ⇒ Object
The method says it all.
-
#redirect_to_return_url(session_key = :return_to, default = '/') ⇒ Object
Dynamic redirection based on the return path that was set.
-
#require_login(login_url = settings.login_url) ⇒ Object
The main gateway.
- #should_return_to?(path, ignored = settings.ignored_by_return_to) ⇒ Boolean
Instance Method Details
#__USER__ ⇒ Object
126 127 128 |
# File 'lib/sinatra/security/helpers.rb', line 126 def __USER__ settings.login_user_class end |
#authenticate(opts) ⇒ String?
Internally used by the POST /login route handler.
119 120 121 122 123 |
# File 'lib/sinatra/security/helpers.rb', line 119 def authenticate(opts) if user = __USER__.authenticate(opts[:username], opts[:password]) session[:user] = user.id end end |
#current_user(finder = lambda { |id| __USER__[id] }) ⇒ User
77 78 79 |
# File 'lib/sinatra/security/helpers.rb', line 77 def current_user(finder = lambda { |id| __USER__[id] }) @current_user ||= finder.call(session[:user]) if session[:user] end |
#ensure_current_user(user) ⇒ Object
Used for simple atomic authorization rules on a per action / route basis.
101 102 103 |
# File 'lib/sinatra/security/helpers.rb', line 101 def ensure_current_user(user) halt 404 unless user == current_user end |
#logged_in? ⇒ true, false
83 84 85 |
# File 'lib/sinatra/security/helpers.rb', line 83 def logged_in? !! current_user end |
#logout! ⇒ Object
The method says it all. Mostly for keeping responsibility where it belongs, instead of letting the application code deal with the session keys themselves.
108 109 110 |
# File 'lib/sinatra/security/helpers.rb', line 108 def logout! session.delete(:user) end |
#redirect_to_return_url(session_key = :return_to, default = '/') ⇒ Object
Dynamic redirection based on the return path that was set.
47 48 49 |
# File 'lib/sinatra/security/helpers.rb', line 47 def redirect_to_return_url(session_key = :return_to, default = '/') redirect session.delete(:return_to) || default end |
#require_login(login_url = settings.login_url) ⇒ Object
The main gateway. This method will redirect if no user is currently authenticated.
18 19 20 21 22 23 24 25 |
# File 'lib/sinatra/security/helpers.rb', line 18 def require_login(login_url = settings.login_url) return if logged_in? if should_return_to?(request.fullpath) session[:return_to] = request.fullpath end redirect login_url end |
#should_return_to?(path, ignored = settings.ignored_by_return_to) ⇒ Boolean
131 132 133 |
# File 'lib/sinatra/security/helpers.rb', line 131 def should_return_to?(path, ignored = settings.ignored_by_return_to) !(path =~ ignored) end |