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.

TODO: convert the examples into real examples with specs

Examples:

Basic usage with User::authenticate

# We assume that User::[] will make a query and returns the requested
# User instance. This instance will be wrapped and cached.

class User
  def self.authenticate(creds)
    User[:name => creds['name'], :pass => creds['pass']]
  end
end

class Profiles < Ramaze::Controller
  helper :user

  def edit
    redirect_referrer unless logged_in?
    "Your profile is shown, your are logged in."
  end
end

class Accounts < Ramaze::Controller
  helper :user

  def 
    return unless request.post?
    (request.subset(:name, :pass))
    redirect Profiles.r(:edit)
  end

  def logout
    user_logout
    redirect_referer
  end
end

caching the authentication lookup with memcached

# Add the name of the cache you are going to use for the authentication
# and set all caches to use memcached

Ramaze::Cache.options do |cache|
  cache.names = [:session, :user]
  cache.default = Ramaze::Cache::MemCache
end

class User

  # Try to fetch the user from the cache, if that fails make a query.
  # We are using a ttl (time to live) of one hour, that's just to show
  # you how to do it and not necessary.
  def self.authenticate(credentials)
    cache = Ramaze::Cache.user

    if user = cache[credentials]
      return user
    elsif user = User[:name => creds['name'], :pass => creds['pass']]
      cache.store(credentials, user, :ttl => 3600)
    end
  end
end

Using a lambda instead of User::authenticate

# assumes all your controllers inherit from this one

class Controller < Ramaze::Controller
  trait :user_callback => lambda{|creds|
    User[:name => creds['name'], :pass => creds['pass']]
  }
end

Using a different model instead of User

# assumes all your controllers inherit from this one

class Controller < Ramaze::Controller
  trait :user_model => 
end

Author:

Defined Under Namespace

Classes: Wrapper

Constant Summary

RAMAZE_HELPER_USER =

Using this as key in request.env

'ramaze.helper.user'.freeze

Instance Method Summary (collapse)

Instance Method Details

- (true false) logged_in?

Whether the user is logged in already.

Returns:

  • (true false)

    whether the user is logged in already.

See Also:

  • Ramaze::Helper::User::Wrapper#_logged_in?

Author:

  • manveru



157
158
159
# File 'lib/ramaze/helper/user.rb', line 157

def logged_in?
  user._logged_in?
end

- (Ramaze::Helper::User::Wrapper) user

Use this method in your application, but do not use it in conditionals as it will never be nil or false.

Returns:

  • (Ramaze::Helper::User::Wrapper)

    wrapped return value from model or callback

Author:

  • manveru



112
113
114
115
116
117
118
119
120
# File 'lib/ramaze/helper/user.rb', line 112

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

- (nil Hash) user_login(creds = request.params)

shortcut for user._login but default argument are request.params

Parameters:

  • creds (Hash) (defaults to: request.params)

    the credentials that will be passed to callback or model

Returns:

  • (nil Hash)

    the given creds are returned on successful login

See Also:

  • Ramaze::Helper::User::Wrapper#_login

Author:

  • manveru



133
134
135
# File 'lib/ramaze/helper/user.rb', line 133

def (creds = request.params)
  user.(creds)
end

- (nil) user_logout

shortcut for user._logout

Returns:

  • (nil)

See Also:

  • Ramaze::Helper::User::Wrapper#_logout

Author:

  • manveru



146
147
148
# File 'lib/ramaze/helper/user.rb', line 146

def user_logout
  user._logout
end