forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Splitting Core::ControllerHelpers up into modules
Fixes solidusio#2079
- Loading branch information
Showing
18 changed files
with
245 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ branches: | |
- 1-1-stable | ||
- 1-2-stable | ||
- master | ||
- 2079-refactor-controller-helpers | ||
rvm: | ||
- 1.8.7 | ||
- 1.9.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
require 'cancan' | ||
|
||
class Spree::BaseController < ApplicationController | ||
include Spree::Core::ControllerHelpers | ||
include Spree::Core::ControllerHelpers::Auth | ||
include Spree::Core::ControllerHelpers::RespondWith | ||
include Spree::Core::ControllerHelpers::Common | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Spree | ||
class StoreController < Spree::BaseController | ||
include Spree::Core::ControllerHelpers::Order | ||
|
||
# Convenience method for firing instrumentation events with the default payload hash | ||
def fire_event(name, extra_payload = {}) | ||
ActiveSupport::Notifications.instrument(name, default_notification_payload.merge(extra_payload)) | ||
end | ||
|
||
# Creates the hash that is sent as the payload for all notifications. Specific notifications will | ||
# add additional keys as appropriate. Override this method if you need additional data when | ||
# responding to a notification | ||
def default_notification_payload | ||
{:user => try_spree_current_user, :order => current_order} | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module Spree | ||
module Core | ||
module ControllerHelpers | ||
module Auth | ||
def self.included(base) | ||
base.class_eval do | ||
include SslRequirement | ||
|
||
helper_method :try_spree_current_user | ||
|
||
rescue_from CanCan::AccessDenied do |exception| | ||
return unauthorized | ||
end | ||
|
||
# Needs to be overriden so that we use Spree's Ability rather than anyone else's. | ||
def current_ability | ||
@current_ability ||= Spree::Ability.new(try_spree_current_user) | ||
end | ||
|
||
# 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. | ||
def unauthorized | ||
format.html do | ||
if try_spree_current_user | ||
flash.now[:error] = t(:authorization_failure) | ||
render 'spree/shared/unauthorized', :layout => Spree::Config[:layout], :status => 401 | ||
else | ||
store_location | ||
url = respond_to?(:spree_login_path) ? spree_login_path : root_path | ||
redirect_to url | ||
end | ||
end | ||
format.xml do | ||
request_http_basic_authentication 'Web Password' | ||
end | ||
format.json do | ||
render :text => "Not Authorized \n", :status => 401 | ||
end | ||
end | ||
|
||
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| | ||
if respond_to?(route) | ||
disallowed_urls << send(route) | ||
end | ||
end | ||
|
||
disallowed_urls.map!{ |url| url[/\/\w+$/] } | ||
unless disallowed_urls.include?(request.fullpath) | ||
session['user_return_to'] = request.fullpath.gsub('//', '/') | ||
end | ||
end | ||
|
||
# proxy method to *possible* spree_current_user method | ||
# Authentication extensions (such as spree_auth_devise) are meant to provide spree_current_user | ||
def try_spree_current_user | ||
respond_to?(:spree_current_user) ? spree_current_user : nil | ||
end | ||
|
||
def redirect_back_or_default(default) | ||
redirect_to(session["user_return_to"] || default) | ||
session["user_return_to"] = nil | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.