Module: Ramaze::Helper::UserHelper
- Defined in:
- lib/ramaze/helper/user.rb
Overview
This helper provides a convenience wrapper for handling authentication and persistence of users.
On every request, when you use the #user method for the first time, we confirm the authentication and store the returned object in the request.env, usually this will involve a request to your database.
On every request it checks authentication again and retrieves the model, we are not using a normal cache for this as it may lead to behaviour that is very hard to predict and debug.
You can however, add your own caching quite easily.
Defined Under Namespace
Classes: Wrapper
Constant Summary collapse
- RAMAZE_HELPER_USER =
Using this as key in request.env
'ramaze.helper.user'.freeze
Instance Method Summary collapse
-
#logged_in? ⇒ TrueClass|FalseClass
Checks if the user is logged in and returns true if this is the case and false otherwise.
-
#user ⇒ Ramaze::Helper::User::Wrapper
Use this method in your application, but do not use it in conditionals as it will never be nil or false.
-
#user_login(creds = request.params) ⇒ nil Hash[Hash]
This method is used to authenticate a user against the supplied credentials (which default to “request.params“).
-
#user_logout ⇒ NilClass
Shortcut for user._logout.
Instance Method Details
#logged_in? ⇒ TrueClass|FalseClass
Checks if the user is logged in and returns true if this is the case and false otherwise.
178 179 180 |
# File 'lib/ramaze/helper/user.rb', line 178 def logged_in? user._logged_in? end |
#user ⇒ Ramaze::Helper::User::Wrapper
Use this method in your application, but do not use it in conditionals as it will never be nil or false.
107 108 109 110 111 112 113 114 115 |
# File 'lib/ramaze/helper/user.rb', line 107 def user env = request.env found = env[RAMAZE_HELPER_USER] return found if found model, callback = ancestral_trait.values_at(:user_model, :user_callback) model ||= ::User unless callback env[RAMAZE_HELPER_USER] = Wrapper.new(model, callback) end |
#user_login(creds = request.params) ⇒ nil Hash[Hash]
This method is used to authenticate a user against the supplied credentials (which default to “request.params“).
This method is a proxy to user._login which returns the value as returned by “Ramaze::Helper::User::Wrapper#_login“.
The supplied argument should be a hash with the user’s credentials. The credentials hash may use any naming for the hash keys as long as they are consistent with the model which authenticates them (through the “authenticate()“ method) such as:
{"username" =>"name", "password" => "the_passwd"}
On success it returns a hash of the credentials embedded within a hash whose only key is ‘:credentials’ such as the following:
{:credentials=>{"username"=>"myuser", "password"=>"mypassword"}}
On failure to authenticate this method returns nil.
153 154 155 |
# File 'lib/ramaze/helper/user.rb', line 153 def user_login(creds = request.params) user._login(creds) end |
#user_logout ⇒ NilClass
Shortcut for user._logout
165 166 167 |
# File 'lib/ramaze/helper/user.rb', line 165 def user_logout user._logout end |