Class: Ramaze::Helper::User::Wrapper

Inherits:
BlankSlate show all
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

Instance Method Summary collapse

Constructor Details

#initialize(model, callback) ⇒ Wrapper

Returns a new instance of Wrapper.



163
164
165
166
167
# File 'lib/ramaze/helper/user.rb', line 163

def initialize(model, callback)
  @_model, @_callback = model, callback
  @_user = nil
  
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?



222
223
224
225
# File 'lib/ramaze/helper/user.rb', line 222

def method_missing(meth, *args, &block)
  return unless _user
  _user.send(meth, *args, &block)
end

Instance Attribute Details

#_callbackObject

Returns the value of attribute _callback.



161
162
163
# File 'lib/ramaze/helper/user.rb', line 161

def _callback
  @_callback
end

#_modelObject

Returns the value of attribute _model.



161
162
163
# File 'lib/ramaze/helper/user.rb', line 161

def _model
  @_model
end

#_userObject

Returns the value of attribute _user.



161
162
163
# File 'lib/ramaze/helper/user.rb', line 161

def _user
  @_user
end

Instance Method Details

#_logged_in?true false

Returns whether the current user is logged in.

Returns:

  • (true false)

    whether the current user is logged in.

See Also:

Author:

  • manveru



208
209
210
# File 'lib/ramaze/helper/user.rb', line 208

def _logged_in?
  !!_user
end

#_login(creds = _persistence) ⇒ Ramaze::Helper::User::Wrapper

Returns wrapped return value from model or callback.

Parameters:

  • creds (Hash) (defaults to: _persistence)

    this hash will be stored in the session on successful login

Returns:

See Also:

Author:

  • manveru



174
175
176
177
178
# File 'lib/ramaze/helper/user.rb', line 174

def (creds = _persistence)
  if @_user = _would_login?(creds)
    self._persistence = creds
  end
end

#_logoutObject

See Also:

Author:

  • manveru



199
200
201
202
# File 'lib/ramaze/helper/user.rb', line 199

def _logout
  _persistence.clear
  Current.request.env['ramaze.helper.user'] = nil
end

#_persistenceObject



216
217
218
# File 'lib/ramaze/helper/user.rb', line 216

def _persistence
  Current.session[:USER] || {}
end

#_persistence=(creds) ⇒ Object



212
213
214
# File 'lib/ramaze/helper/user.rb', line 212

def _persistence=(creds)
  Current.session[:USER] = creds
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.

Returns:

  • (Boolean)


185
186
187
188
189
190
191
192
193
194
# File 'lib/ramaze/helper/user.rb', line 185

def _would_login?(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