Sha256: d53ebb75c5a762e0e498b9345d8acc809d7b172f348b553eb3a10d4be0a91321

Contents?: true

Size: 874 Bytes

Versions: 1

Compression:

Stored size: 874 Bytes

Contents

module Shield
  module Helpers
    def ensure_authenticated(model)
      return if authenticated(model)

      session[:return_to] = request.fullpath
      redirect_to_login
    end

    def authenticated(model)
      @_authenticated ||= {}
      @_authenticated[model] ||= model[session[model.to_s]]
    end

    def redirect_to_login
      redirect "/login"
    end

    def redirect_to_stored(default = "/")
      redirect(session.delete(:return_to) || default)
    end

    def login(model, username, password)
      instance = model.authenticate(username, password)

      if instance
        session[model.to_s] = instance.id
        return true
      else
        return false
      end
    end

    def logout(model)
      session.delete(model.to_s)
      session.delete(:return_to)

      @_authenticated.delete(model) if defined?(@_authenticated)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shield-0.0.1 lib/shield/helpers.rb