Module: Spree::Core::ControllerHelpers::Auth

Extended by:
ActiveSupport::Concern
Includes:
TokenGenerator
Included in:
BaseController
Defined in:
lib/spree/core/controller_helpers/auth.rb

Instance Method Summary collapse

Methods included from TokenGenerator

#generate_guest_token

Instance Method Details

#current_abilityObject

Needs to be overriden so that we use Spree’s Ability rather than anyone else’s.



18
19
20
# File 'lib/spree/core/controller_helpers/auth.rb', line 18

def current_ability
  @current_ability ||= Spree::Ability.new(try_spree_current_user)
end

#redirect_back_or_default(default) ⇒ Object



22
23
24
25
# File 'lib/spree/core/controller_helpers/auth.rb', line 22

def redirect_back_or_default(default)
  redirect_to(session['spree_user_return_to'] || request.env['HTTP_REFERER'] || default)
  session['spree_user_return_to'] = nil
end

#redirect_unauthorized_accessObject

Redirect as appropriate when an access request fails. The default action is to redirect to the login screen. Override this method in your controllers if you want to have special behavior in case the user is not authorized to access the requested action. For example, a popup window might simply close itself.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/spree/core/controller_helpers/auth.rb', line 63

def redirect_unauthorized_access
  if try_spree_current_user
    flash[:error] = Spree.t(:authorization_failure)
    redirect_to spree.forbidden_path
  else
    store_location
    if respond_to?(:spree_login_path)
      redirect_to 
    elsif spree.respond_to?(:root_path)
      redirect_to spree.root_path
    else
      redirect_to main_app.respond_to?(:root_path) ? main_app.root_path : '/'
    end
  end
end

#set_guest_tokenObject



27
28
29
30
31
# File 'lib/spree/core/controller_helpers/auth.rb', line 27

def set_guest_token
  if cookies.signed[:guest_token].blank?
    cookies.permanent.signed[:guest_token] = generate_guest_token
  end
end

#store_locationObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/spree/core/controller_helpers/auth.rb', line 33

def store_location
  # disallow return to login, logout, signup pages
  authentication_routes = [:spree_signup_path, :spree_login_path, :spree_logout_path]
  disallowed_urls = []
  authentication_routes.each do |route|
    disallowed_urls << send(route) if respond_to?(route)
  end

  disallowed_urls.map! { |url| url[/\/\w+$/] }
  unless disallowed_urls.include?(request.fullpath)
    session['spree_user_return_to'] = request.fullpath.gsub('//', '/')
  end
end

#try_spree_current_userObject

proxy method to possible spree_current_user method Authentication extensions (such as spree_auth_devise) are meant to provide spree_current_user



49
50
51
52
53
54
55
56
57
58
# File 'lib/spree/core/controller_helpers/auth.rb', line 49

def try_spree_current_user
  # This one will be defined by apps looking to hook into Spree
  # As per authentication_helpers.rb
  if respond_to?(:spree_current_user)
    spree_current_user
  # This one will be defined by Devise
  elsif respond_to?(:current_spree_user)
    current_spree_user
  end
end