Sha256: 8f738f96a2b1c759f2c9e4b1174c0d9da1e9f1f279e148252081d8a6a7b7beeb

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

module SessionsHelper

	# Sign in a user with a cookie (remove on browser close)
	# 
	# * *Args*		:
	# 	- a user to sign in
	# * *Returns*	:
	#
	def sign_in(user)
		cookies[:remember_token] = user.remember_token
		self.current_user = user
	end

	# Sign in a user with a cookie (remove after 2 months)
	# 
	# * *Args*		:
	# 	- a user to sign in
	# * *Returns*	:
	#
	def sign_in_permanent(user)
		cookies[:remember_token] = { value: user.remember_token, expires: Time.now + 2592000 }
		self.current_user = user
	end

	def current_user=(user)
		@current_user = user
	end

	# Provid the user currently signed in.
	# 
	# * *Returns*	:
	# 	- the user currently signed in
	def current_user
		@current_user ||= User.find_by_remember_token(cookies[:remember_token])
	end


	# Check if there is a user signed in.
	# 
	# * *Returns*	:
	# 	- true or false
	def signed_in?
		!current_user.nil?
	end

	# Sign out a user by removing the cookie.
	def sign_out
		self.current_user = nil
		cookies.delete(:remember_token)
	end

	# Store the current url in session's variable
  # 
  # * *Args*    :
  # 
  # * *Returns* :
  #
  def store_location
    cookies[:return_to] = { value: request.fullpath, expires: 1.minute.from_now }
  end

  # Redirect the user to the stored url or the default one provided
  # 
  # * *Args*    :
  #   - default path to redirect to
  # * *Returns* :
  #
  def redirect_back_or(default, message = nil)
    redirect_to(cookies[:return_to] || default, message)
    session.delete(:return_to)
  end

  def authorize_level?(level = 3)
    current_user && current_user.level <= level
  end

  def authorize_level(level = 3)
    unless authorize_level?(level)
      store_location
      redirect_to login_path, error: "Pas autorisé"
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
snapuser-0.1.1 app/helpers/sessions_helper.rb
snapuser-0.1.0 app/helpers/sessions_helper.rb