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)
-
- (Object) _callback
Returns the value of attribute _callback.
-
- (Object) _model
Returns the value of attribute _model.
-
- (Object) _user
Returns the value of attribute _user.
Instance Method Summary (collapse)
-
- (true false) _logged_in?
Whether the current user is logged in.
-
- (Ramaze::Helper::User::Wrapper) _login(creds = nil)
Wrapped return value from model or callback.
- - (Object) _logout
- - (Object) _persistence
- - (Object) _persistence=(obj)
-
- (Boolean) _would_login?(creds)
The callback should return an instance of the user, otherwise it should answer with nil.
-
- (Wrapper) initialize(model, callback)
constructor
A new instance of Wrapper.
-
- (Object) method_missing(meth, *args, &block)
Refer everything not known THINK: This might be quite confusing…
Constructor Details
- (Wrapper) initialize(model, callback)
A new instance of Wrapper
174 175 176 177 178 |
# File 'lib/ramaze/helper/user.rb', line 174 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
- (Object) method_missing(meth, *args, &block)
Refer everything not known THINK: This might be quite confusing… should we raise instead?
242 243 244 245 |
# File 'lib/ramaze/helper/user.rb', line 242 def method_missing(meth, *args, &block) return unless _user _user.send(meth, *args, &block) end |
Instance Attribute Details
- (Object) _callback
Returns the value of attribute _callback
172 173 174 |
# File 'lib/ramaze/helper/user.rb', line 172 def _callback @_callback end |
- (Object) _model
Returns the value of attribute _model
172 173 174 |
# File 'lib/ramaze/helper/user.rb', line 172 def _model @_model end |
- (Object) _user
Returns the value of attribute _user
172 173 174 |
# File 'lib/ramaze/helper/user.rb', line 172 def _user @_user end |
Instance Method Details
- (true false) _logged_in?
Whether the current user is logged in.
228 229 230 |
# File 'lib/ramaze/helper/user.rb', line 228 def _logged_in? !!_user end |
- (Ramaze::Helper::User::Wrapper) _login(creds = nil)
Wrapped return value from model or callback
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ramaze/helper/user.rb', line 187 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 |
- (Object) _logout
219 220 221 222 |
# File 'lib/ramaze/helper/user.rb', line 219 def _logout (_persistence || {}).clear Current.request.env['ramaze.helper.user'] = nil end |
- (Object) _persistence
236 237 238 |
# File 'lib/ramaze/helper/user.rb', line 236 def _persistence Current.session[:USER] end |
- (Object) _persistence=(obj)
232 233 234 |
# File 'lib/ramaze/helper/user.rb', line 232 def _persistence=(obj) Current.session[:USER] = obj end |
- (Boolean) _would_login?(creds)
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.
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/ramaze/helper/user.rb', line 203 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 |