class Ramaze::Helper::UserHelper::Wrapper

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.

Attributes

_callback[RW]
_model[RW]
_user[RW]

Public Class Methods

new(model, callback) click to toggle source
# File lib/ramaze/helper/user.rb, line 195
def initialize(model, callback)
  @_model, @_callback = model, callback
  @_user = nil
  _login
end

Public Instance Methods

_logged_in?() click to toggle source

@author manveru @api internal @see Ramaze::Helper::User#logged_in? @return [true false] whether the current user is logged in.

# File lib/ramaze/helper/user.rb, line 261
def _logged_in?
  !!_user
end
_login(creds = nil) click to toggle source

@author manveru @see Ramaze::Helper::User#user_login @param [Hash] creds this hash will be stored in the session on

successful login

@return [Ramaze::Helper::User::Wrapper] wrapped return value from

model or callback
# 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() click to toggle source

@author manveru @api internal @see Ramaze::Helper::User#user_logout

# File lib/ramaze/helper/user.rb, line 249
def _logout
  (_persistence || {}).clear
  Current.request.env['ramaze.helper.user'] = nil
  Current.session.resid!
end
_persistence() click to toggle source
# File lib/ramaze/helper/user.rb, line 269
def _persistence
  Current.session[:USER]
end
_persistence=(obj) click to toggle source
# File lib/ramaze/helper/user.rb, line 265
def _persistence=(obj)
  Current.session[:USER] = obj
end
_would_login?(creds) click to toggle source

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.

# 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
method_missing(meth, *args, &block) click to toggle source

Refer everything not known THINK: This might be quite confusing… should we raise instead?

# File lib/ramaze/helper/user.rb, line 277
def method_missing(meth, *args, &block)
  return unless _user
  _user.send(meth, *args, &block)
end