### Home Classes Methods

### In Files

- lib/ramaze/helper/user.rb

### Namespace

- CLASS Ramaze::Helper::UserHelper::Wrapper

### Methods

- #logged_in?
- #user
- #user_login
- #user_logout

### Class/Module Index

Quicksearch

- Ramaze
- Ramaze::App
- Ramaze::AppGraph
- Ramaze::Bin
- Ramaze::Bin::Console
- Ramaze::Bin::Create
- Ramaze::Bin::Helper
- Ramaze::Bin::Restart
- Ramaze::Bin::Runner
- Ramaze::Bin::Start
- Ramaze::Bin::Status
- Ramaze::Bin::Stop
- Ramaze::Cache
- Ramaze::Cache::LRU
- Ramaze::Cache::LocalMemCache
- Ramaze::Cache::MemCache
- Ramaze::Cache::Redis
- Ramaze::Cache::Sequel
- Ramaze::Controller
- Ramaze::CoreExtensions
- Ramaze::CoreExtensions::Array
- Ramaze::CoreExtensions::Binding
- Ramaze::CoreExtensions::Kernel
- Ramaze::CoreExtensions::Numeric
- Ramaze::CoreExtensions::Object
- Ramaze::CoreExtensions::Proc
- Ramaze::CoreExtensions::String
- Ramaze::Current
- Ramaze::DefaultController
- Ramaze::Dictionary
- Ramaze::Fiber
- Ramaze::Files
- Ramaze::GemSetup
- Ramaze::Gestalt
- Ramaze::Helper
- Ramaze::Helper::Auth
- Ramaze::Helper::Bench
- Ramaze::Helper::BlueForm
- Ramaze::Helper::BlueForm::Form
- Ramaze::Helper::CSRF
- Ramaze::Helper::Cache
- Ramaze::Helper::Cache::SingletonMethods
- Ramaze::Helper::Disqus
- Ramaze::Helper::Email
- Ramaze::Helper::Erector
- Ramaze::Helper::Flash
- Ramaze::Helper::Formatting
- Ramaze::Helper::Gestalt
- Ramaze::Helper::Gravatar
- Ramaze::Helper::Identity
- Ramaze::Helper::Layout
- Ramaze::Helper::Layout::SingletonMethods
- Ramaze::Helper::Link
- Ramaze::Helper::Localize
- Ramaze::Helper::Localize::Dictionary
- Ramaze::Helper::Localize::Parser
- Ramaze::Helper::Markaby
- Ramaze::Helper::Maruku
- Ramaze::Helper::Paginate
- Ramaze::Helper::Paginate::Paginator
- Ramaze::Helper::Paginate::Paginator::ArrayPager
- Ramaze::Helper::Paginate::Paginator::DataMapperPager
- Ramaze::Helper::Remarkably
- Ramaze::Helper::RequestAccessor
- Ramaze::Helper::SendFile
- Ramaze::Helper::SimpleCaptcha
- Ramaze::Helper::Stack
- Ramaze::Helper::Thread
- Ramaze::Helper::Ultraviolet
- Ramaze::Helper::Upload
- Ramaze::Helper::Upload::ClassMethods
- Ramaze::Helper::Upload::UploadedFile
- Ramaze::Helper::UserHelper
- Ramaze::Helper::UserHelper::Wrapper
- Ramaze::Helper::XHTML
- Ramaze::LRUHash
- Ramaze::Logger
- Ramaze::Logger::Analogger
- Ramaze::Logger::Growl
- Ramaze::Logger::Informer
- Ramaze::Logger::Knotify
- Ramaze::Logger::LogHub
- Ramaze::Logger::Logger
- Ramaze::Logger::RotatingInformer
- Ramaze::Logger::Syslog
- Ramaze::Logger::Xosd
- Ramaze::Logging
- Ramaze::MiddlewareCompiler
- Ramaze::Plugin
- Ramaze::Reloader
- Ramaze::Reloader::Hooks
- Ramaze::Reloader::WatchInotify
- Ramaze::Reloader::WatchStat
- Ramaze::Request
- Ramaze::Response
- Ramaze::SpecLogger
- Ramaze::Struct
- Ramaze::View
- Ramaze::View::Erector
- Ramaze::View::Erubis
- Ramaze::View::Ezamar
- Ramaze::View::Gestalt
- Ramaze::View::Haml
- Ramaze::View::Liquid
- Ramaze::View::Liquid::Tag
- Ramaze::View::Lokar
- Ramaze::View::Mustache
- Ramaze::View::Mustache::RamazeContext
- Ramaze::View::Nagoro
- Ramaze::View::Remarkably
- Ramaze::View::Sass
- Ramaze::View::Slippers
- Ramaze::View::Tagz
- Ramaze::View::Tagz::Methods
- Ramaze::View::Tenjin
- Nagoro
- Nagoro::Pipe
- Nagoro::Pipe::RenderPartial
- Bacon
- Bacon::PrettyOutput
- Erector
- Erector::Widget
- Rack
- Rack::RouteExceptions
- Tenjin
- Tenjin::Template
- BlankSlate
- Controller
- Fiber
- FiberError
- MainController
- Object
- OrderedSet
- Thread

No matching classes.

# Ramaze::Helper::UserHelper

This helper provides a convenience wrapper for handling authentication and persistence of users.

On every request, when you use the {UserHelper#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.

@example 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 login
        return unless request.post?
        user_login(request.subset(:name, :pass))
        redirect Profiles.r(:edit)
      end

      def logout
        user_logout
        redirect_referer
      end
    end

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.

@example 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

@example 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

@example Using a different model instead of User

    # assumes all your controllers inherit from this one

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

@author manveru

### Constants

RAMAZE_HELPER_USER

  Using this as key in request.env

### Public Instance Methods

logged_in?() click to toggle source

@return [true false] whether the user is logged in already.

@api external @see Ramaze::Helper::User::Wrapper#_logged_in? @author manveru

    # File lib/ramaze/helper/user.rb, line 153
    def logged_in?
      user._logged_in?
    end

user() click to toggle source

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

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

    model or callback

@api external @author manveru

    # File lib/ramaze/helper/user.rb, line 108
    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

user_login(creds = request.params) click to toggle source

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

@param [Hash] creds the credentials that will be passed to callback or

    model

@return [nil Hash] the given creds are returned on successful login

@api external @see Ramaze::Helper::User::Wrapper#_login @author manveru

    # File lib/ramaze/helper/user.rb, line 129
    def user_login(creds = request.params)
      user._login(creds)
    end

user_logout() click to toggle source

shortcut for user._logout

@return [nil]

@api external @see Ramaze::Helper::User::Wrapper#_logout @author manveru

    # File lib/ramaze/helper/user.rb, line 142
    def user_logout
      user._logout
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.