Class: Ramaze::Helper::UserHelper::Wrapper
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- Ramaze::Helper::UserHelper::Wrapper
- Defined in:
- lib/ramaze/helper/user.rb
Overview
Wrapper for the ever-present “user” in your application. It wraps around an arbitrary instance and worries about authentication and storing information about the user in the session.
In order to not interfere with the wrapped instance/model we start our methods with an underscore.
Patches and suggestions are highly appreciated.
Instance Attribute Summary collapse
-
#_callback ⇒ Object
Returns the value of attribute _callback.
-
#_model ⇒ Object
Returns the value of attribute _model.
-
#_user ⇒ Object
Returns the value of attribute _user.
Instance Method Summary collapse
-
#_logged_in? ⇒ true false
Whether the current user is logged in.
-
#_login(creds = nil) ⇒ Ramaze::Helper::User::Wrapper
Wrapped return value from model or callback.
- #_logout ⇒ Object
- #_persistence ⇒ Object
- #_persistence=(obj) ⇒ Object
-
#_would_login?(creds) ⇒ Boolean
The callback should return an instance of the user, otherwise it should answer with nil.
-
#initialize(model, callback) ⇒ Wrapper
constructor
A new instance of Wrapper.
-
#method_missing(meth, *args, &block) ⇒ Object
Refer everything not known THINK: This might be quite confusing…
Constructor Details
#initialize(model, callback) ⇒ Wrapper
Returns a new instance of Wrapper.
195 196 197 198 199 |
# File 'lib/ramaze/helper/user.rb', line 195 def initialize(model, callback) @_model, @_callback = model, callback @_user = nil _login end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Refer everything not known THINK: This might be quite confusing… should we raise instead?
277 278 279 280 |
# File 'lib/ramaze/helper/user.rb', line 277 def method_missing(meth, *args, &block) return unless _user _user.send(meth, *args, &block) end |
Instance Attribute Details
#_callback ⇒ Object
Returns the value of attribute _callback.
193 194 195 |
# File 'lib/ramaze/helper/user.rb', line 193 def _callback @_callback end |
#_model ⇒ Object
Returns the value of attribute _model.
193 194 195 |
# File 'lib/ramaze/helper/user.rb', line 193 def _model @_model end |
#_user ⇒ Object
Returns the value of attribute _user.
193 194 195 |
# File 'lib/ramaze/helper/user.rb', line 193 def _user @_user end |
Instance Method Details
#_logged_in? ⇒ true false
Returns whether the current user is logged in.
261 262 263 |
# File 'lib/ramaze/helper/user.rb', line 261 def _logged_in? !!_user end |
#_login(creds = nil) ⇒ Ramaze::Helper::User::Wrapper
Returns wrapped return value from model or callback.
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/ramaze/helper/user.rb', line 209 def _login(creds = nil) if creds if @_user = _would_login?(creds) Current.session.resid! self._persistence = {:credentials => creds} end elsif persistence = self._persistence @_user = _would_login?(persistence[:credentials]) end end |
#_logout ⇒ Object
249 250 251 252 253 |
# File 'lib/ramaze/helper/user.rb', line 249 def _logout (_persistence || {}).clear Current.request.env['ramaze.helper.user'] = nil Current.session.resid! end |
#_persistence ⇒ Object
269 270 271 |
# File 'lib/ramaze/helper/user.rb', line 269 def _persistence Current.session[:USER] end |
#_persistence=(obj) ⇒ Object
265 266 267 |
# File 'lib/ramaze/helper/user.rb', line 265 def _persistence=(obj) Current.session[:USER] = obj end |
#_would_login?(creds) ⇒ Boolean
The callback should return an instance of the user, otherwise it should answer with nil.
This will not actually login, just check whether the credentials would result in a user.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/ramaze/helper/user.rb', line 227 def _would_login?(creds) return unless creds if c = @_callback c.call(creds) elsif _model.respond_to?(:authenticate) _model.authenticate(creds) else Log.warn( "Helper::User has no callback and there is no %p::authenticate" \ % _model ) nil end end |