Module: Auth::Behavior::Core::ControllerExtensions

Defined in:
lib/auth/behavior/core/controller_extensions.rb

Defined Under Namespace

Modules: ClassMethods, CurrentUser

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/auth/behavior/core/controller_extensions.rb', line 2

def self.included(base)
  base.class_eval do
    include Auth::Behavior::Core::ControllerExtensions::CurrentUser
    extend Auth::Behavior::Core::ControllerExtensions::ClassMethods
    helper_method :new_session_path, :current_user
    hide_action :current_user, :find_current_session, :require_login, :require_logout, :login!, :logout!,
                :redirect_back_or_default, :new_session_path, :store_location
  end
end

Instance Method Details

#login!(user, options = {}) ⇒ Object

Forcibly logs in the current client as the specified user.

The options hash is unused, and is reserved for other behaviors to make use of. For instance, the “remember me” behavior checks for a :remember option and, if true, sets a remembrance token cookie.



34
35
36
37
38
# File 'lib/auth/behavior/core/controller_extensions.rb', line 34

def login!(user, options = {})
  session[:session_token] = user.persistence_token
  session[:active_at] = Time.now
  @current_user = user
end

#logout!(options = {}) ⇒ Object

Forcibly logs out the current client.

The options hash is unused, and is reserved for other behaviors to make use of.



44
45
46
# File 'lib/auth/behavior/core/controller_extensions.rb', line 44

def logout!(options = {})
  session[:session_token] = session[:active_at] = nil
end

#redirect_back_or_default(path, notice = nil) ⇒ Object



48
49
50
51
# File 'lib/auth/behavior/core/controller_extensions.rb', line 48

def redirect_back_or_default(path, notice = nil)
  flash[:notice] = notice if notice
  redirect_to session.delete(:destination) || path
end

#require_loginObject



12
13
14
15
16
17
18
19
# File 'lib/auth/behavior/core/controller_extensions.rb', line 12

def                                     
  unless current_user
    store_location
    flash[:notice] = @session_timeout_message || Auth.
     = Auth. ? send(Auth.) : Auth.default_destination
    redirect_to 
  end
end

#require_logoutObject



25
26
27
# File 'lib/auth/behavior/core/controller_extensions.rb', line 25

def require_logout
  redirect_back_or_default Auth.default_destination, Auth.logout_required_message if current_user
end

#store_location(url = request.respond_to?(:fullpath) ? request.fullpath : request.request_uri) ⇒ Object



21
22
23
# File 'lib/auth/behavior/core/controller_extensions.rb', line 21

def store_location(url = request.respond_to?(:fullpath) ? request.fullpath : request.request_uri)
  session[:destination] = url
end