spree_auth 0.70.7 → 1.0.0.rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of spree_auth might be problematic. Click here for more details.
- data/LICENSE +2 -2
- data/README.md +0 -4
- data/app/controllers/resource_controller_decorator.rb +13 -13
- data/app/controllers/spree/admin/admin_controller_decorator.rb +19 -0
- data/app/controllers/spree/admin/admin_orders_controller_decorator.rb +14 -0
- data/app/controllers/spree/admin/admin_resource_controller_decorator.rb +3 -0
- data/app/controllers/spree/base_controller_decorator.rb +33 -32
- data/app/controllers/spree/checkout_controller_decorator.rb +41 -0
- data/app/controllers/spree/orders_controller_decorator.rb +15 -0
- data/app/controllers/{user_passwords_controller.rb → spree/user_passwords_controller.rb} +13 -14
- data/app/controllers/{user_registrations_controller.rb → spree/user_registrations_controller.rb} +11 -13
- data/app/controllers/{user_sessions_controller.rb → spree/user_sessions_controller.rb} +15 -17
- data/app/controllers/{users_controller.rb → spree/users_controller.rb} +8 -12
- data/app/helpers/spree/users_helper.rb +15 -0
- data/app/mailers/spree/user_mailer.rb +10 -0
- data/app/models/spree/ability.rb +65 -0
- data/app/models/spree/auth_configuration.rb +6 -0
- data/app/models/{spree_current_order_decorator.rb → spree/current_order_decorator.rb} +1 -3
- data/app/models/{order_decorator.rb → spree/order_decorator.rb} +1 -1
- data/app/models/spree/tokenized_permission.rb +5 -0
- data/app/models/spree/user.rb +87 -0
- data/app/overrides/auth_admin_login_navigation_bar.rb +2 -2
- data/app/overrides/auth_shared_login_bar.rb +2 -2
- data/app/views/spree/checkout/registration.html.erb +20 -0
- data/app/views/spree/layouts/admin/_login_nav.html.erb +8 -0
- data/app/views/{shared → spree/shared}/_flashes.html.erb +0 -0
- data/app/views/{shared → spree/shared}/_login.html.erb +3 -3
- data/app/views/spree/shared/_login_bar.html.erb +6 -0
- data/app/views/{shared → spree/shared}/_user_form.html.erb +1 -1
- data/app/views/{shared → spree/shared}/unauthorized.html.erb +0 -0
- data/app/views/{user_mailer → spree/user_mailer}/reset_password_instructions.text.erb +0 -0
- data/app/views/spree/user_passwords/edit.html.erb +15 -0
- data/app/views/spree/user_passwords/new.html.erb +13 -0
- data/app/views/spree/user_registrations/new.html.erb +22 -0
- data/app/views/{user_sessions → spree/user_sessions}/authorization_failure.html.erb +0 -0
- data/app/views/{user_sessions → spree/user_sessions}/new.html.erb +2 -2
- data/app/views/spree/users/edit.html.erb +11 -0
- data/app/views/{users → spree/users}/show.html.erb +2 -2
- data/config/initializers/devise.rb +6 -6
- data/config/locales/en.yml +5 -5
- data/config/routes.rb +12 -9
- data/db/default/users.rb +22 -12
- data/db/migrate/20101026184950_rename_columns_for_devise.rb +33 -33
- data/db/migrate/20101214150824_convert_user_remember_field.rb +7 -7
- data/db/migrate/20101217012656_create_tokenized_permissions.rb +3 -8
- data/db/migrate/20101219201531_tokens_for_legacy_orders.rb +7 -3
- data/db/migrate/20111007143030_namespace_tokenized_permission.rb +5 -0
- data/db/migrate/20111206075712_migrate_tokenized_permissions.rb +24 -0
- data/db/seeds.rb +1 -1
- data/lib/spree/auth/engine.rb +25 -0
- data/lib/spree/auth.rb +15 -0
- data/lib/spree/token_resource.rb +17 -17
- data/lib/spree_auth.rb +1 -24
- metadata +63 -55
- data/app/controllers/admin_controller_decorator.rb +0 -13
- data/app/controllers/admin_orders_controller_decorator.rb +0 -15
- data/app/controllers/admin_resource_controller_decorator.rb +0 -3
- data/app/controllers/checkout_controller_decorator.rb +0 -42
- data/app/controllers/orders_controller_decorator.rb +0 -17
- data/app/helpers/users_helper.rb +0 -13
- data/app/models/ability.rb +0 -64
- data/app/models/spree_auth_configuration.rb +0 -4
- data/app/models/tokenized_permission.rb +0 -3
- data/app/models/user.rb +0 -85
- data/app/models/user_mailer.rb +0 -13
- data/app/views/checkout/registration.html.erb +0 -20
- data/app/views/layouts/admin/_login_nav.html.erb +0 -8
- data/app/views/shared/_login_bar.html.erb +0 -6
- data/app/views/user_passwords/edit.html.erb +0 -15
- data/app/views/user_passwords/new.html.erb +0 -13
- data/app/views/user_registrations/new.html.erb +0 -22
- data/app/views/users/edit.html.erb +0 -11
- data/config/cucumber.yml +0 -10
- data/lib/spree/auth/config.rb +0 -22
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2007-2011,
|
1
|
+
Copyright (c) 2007-2011, Spree Commerce, Inc. and other contributors
|
2
2
|
All rights reserved.
|
3
3
|
|
4
4
|
Redistribution and use in source and binary forms, with or without modification,
|
@@ -23,4 +23,4 @@ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
23
23
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24
24
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25
25
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
26
|
-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
26
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
CHANGED
@@ -4,22 +4,22 @@ module ResourceController
|
|
4
4
|
module Helpers
|
5
5
|
module Internal
|
6
6
|
protected
|
7
|
-
|
8
|
-
|
7
|
+
# Calls the before block for the action, if one is present.
|
8
|
+
def before(action)
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
resource = case action
|
11
|
+
when :index, :new, :create
|
12
|
+
model
|
13
|
+
else object
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
if resource.respond_to? :token
|
17
|
+
authorize! action, resource, session[:access_token]
|
18
|
+
else
|
19
|
+
authorize! action, resource
|
20
|
+
end
|
21
|
+
invoke_callbacks *self.class.send(action).before
|
20
22
|
end
|
21
|
-
invoke_callbacks *self.class.send(action).before
|
22
|
-
end
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.expand_path('../../base_controller_decorator', __FILE__)
|
2
|
+
Spree::Admin::BaseController.class_eval do
|
3
|
+
before_filter :authorize_admin
|
4
|
+
|
5
|
+
def authorize_admin
|
6
|
+
begin
|
7
|
+
model = model_class
|
8
|
+
rescue
|
9
|
+
model = Object
|
10
|
+
end
|
11
|
+
authorize! :admin, model
|
12
|
+
authorize! params[:action].to_sym, model
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
def model_class
|
17
|
+
"Spree::#{controller_name.classify}".constantize
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Spree::Admin::OrdersController.class_eval do
|
2
|
+
before_filter :check_authorization
|
3
|
+
|
4
|
+
private
|
5
|
+
def check_authorization
|
6
|
+
load_order
|
7
|
+
session[:access_token] ||= params[:token]
|
8
|
+
|
9
|
+
resource = @order || Spree::Order
|
10
|
+
action = params[:action].to_sym
|
11
|
+
|
12
|
+
authorize! action, resource, session[:access_token]
|
13
|
+
end
|
14
|
+
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
Spree::BaseController.class_eval do
|
2
|
-
|
3
2
|
before_filter :set_current_user
|
4
3
|
|
5
4
|
# graceful error handling for cancan authorization exceptions
|
@@ -8,41 +7,43 @@ Spree::BaseController.class_eval do
|
|
8
7
|
end
|
9
8
|
|
10
9
|
private
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
10
|
+
# Needs to be overriden so that we use Spree's Ability rather than anyone else's.
|
11
|
+
def current_ability
|
12
|
+
@current_ability ||= Spree::Ability.new(current_user)
|
13
|
+
end
|
14
|
+
# Redirect as appropriate when an access request fails. The default action is to redirect to the login screen.
|
15
|
+
# Override this method in your controllers if you want to have special behavior in case the user is not authorized
|
16
|
+
# to access the requested action. For example, a popup window might simply close itself.
|
17
|
+
def unauthorized
|
18
|
+
respond_to do |format|
|
19
|
+
format.html do
|
20
|
+
if current_user
|
21
|
+
flash.now[:error] = t(:authorization_failure)
|
22
|
+
render 'spree/shared/unauthorized', :layout => '/spree/layouts/spree_application'
|
23
|
+
else
|
24
|
+
store_location
|
25
|
+
redirect_to spree.login_path and return
|
26
|
+
end
|
27
|
+
end
|
28
|
+
format.xml do
|
29
|
+
request_http_basic_authentication 'Web Password'
|
30
|
+
end
|
31
|
+
format.json do
|
32
|
+
render :text => "Not Authorized \n", :status => 401
|
24
33
|
end
|
25
|
-
end
|
26
|
-
format.xml do
|
27
|
-
request_http_basic_authentication 'Web Password'
|
28
|
-
end
|
29
|
-
format.json do
|
30
|
-
render :text => "Not Authorized \n", :status => 401
|
31
34
|
end
|
32
35
|
end
|
33
|
-
end
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
def store_location
|
38
|
+
# disallow return to login, logout, signup pages
|
39
|
+
disallowed_urls = [spree.signup_url, spree.login_url, spree.destroy_user_session_path]
|
40
|
+
disallowed_urls.map!{ |url| url[/\/\w+$/] }
|
41
|
+
unless disallowed_urls.include?(request.fullpath)
|
42
|
+
session['user_return_to'] = request.fullpath.gsub('//', '/')
|
43
|
+
end
|
41
44
|
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def set_current_user
|
45
|
-
User.current = current_user
|
46
|
-
end
|
47
45
|
|
46
|
+
def set_current_user
|
47
|
+
Spree::User.current = current_user
|
48
|
+
end
|
48
49
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
Spree::CheckoutController.class_eval do
|
2
|
+
before_filter :check_authorization
|
3
|
+
before_filter :check_registration, :except => [:registration, :update_registration]
|
4
|
+
|
5
|
+
helper 'spree/users'
|
6
|
+
|
7
|
+
def registration
|
8
|
+
@user = Spree::User.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def update_registration
|
12
|
+
# hack - temporarily change the state to something other than cart so we can validate the order email address
|
13
|
+
current_order.state = 'address'
|
14
|
+
if current_order.update_attributes(params[:order])
|
15
|
+
redirect_to checkout_path
|
16
|
+
else
|
17
|
+
@user = Spree::User.new
|
18
|
+
render 'registration'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def check_authorization
|
24
|
+
authorize!(:edit, current_order, session[:access_token])
|
25
|
+
end
|
26
|
+
|
27
|
+
# Introduces a registration step whenever the +registration_step+ preference is true.
|
28
|
+
def check_registration
|
29
|
+
return unless Spree::Auth::Config[:registration_step]
|
30
|
+
return if current_user or current_order.email
|
31
|
+
store_location
|
32
|
+
redirect_to spree.checkout_registration_path
|
33
|
+
end
|
34
|
+
|
35
|
+
# Overrides the equivalent method defined in Spree::Core. This variation of the method will ensure that users
|
36
|
+
# are redirected to the tokenized order url unless authenticated as a registered user.
|
37
|
+
def completion_route
|
38
|
+
return order_path(@order) if current_user
|
39
|
+
spree.token_order_path(@order, @order.token)
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Spree::OrdersController.class_eval do
|
2
|
+
before_filter :check_authorization
|
3
|
+
|
4
|
+
private
|
5
|
+
def check_authorization
|
6
|
+
session[:access_token] ||= params[:token]
|
7
|
+
order = current_order || Spree::Order.find_by_number(params[:id])
|
8
|
+
|
9
|
+
if order
|
10
|
+
authorize! :edit, order, session[:access_token]
|
11
|
+
else
|
12
|
+
authorize! :create, Spree::Order
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
class UserPasswordsController < Devise::PasswordsController
|
2
|
-
include
|
3
|
-
helper
|
4
|
-
|
5
|
-
after_filter :associate_user
|
1
|
+
class Spree::UserPasswordsController < Devise::PasswordsController
|
2
|
+
include Spree::Core::ControllerHelpers
|
3
|
+
helper 'spree/users', 'spree/base'
|
4
|
+
|
5
|
+
after_filter :associate_user
|
6
6
|
|
7
7
|
def new
|
8
8
|
super
|
@@ -21,7 +21,7 @@ class UserPasswordsController < Devise::PasswordsController
|
|
21
21
|
|
22
22
|
if resource.errors.empty?
|
23
23
|
set_flash_message(:notice, :send_instructions) if is_navigational_format?
|
24
|
-
respond_with resource, :location => login_path
|
24
|
+
respond_with resource, :location => spree.login_path
|
25
25
|
else
|
26
26
|
respond_with_navigational(resource){ render_with_scope :new }
|
27
27
|
end
|
@@ -34,13 +34,12 @@ class UserPasswordsController < Devise::PasswordsController
|
|
34
34
|
def update
|
35
35
|
super
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
private
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
39
|
+
|
40
|
+
def associate_user
|
41
|
+
return unless current_user and current_order
|
42
|
+
current_order.associate_user!(current_user)
|
43
|
+
session[:guest_token] = nil
|
44
|
+
end
|
46
45
|
end
|
data/app/controllers/{user_registrations_controller.rb → spree/user_registrations_controller.rb}
RENAMED
@@ -1,6 +1,6 @@
|
|
1
|
-
class UserRegistrationsController < Devise::RegistrationsController
|
2
|
-
include
|
3
|
-
helper
|
1
|
+
class Spree::UserRegistrationsController < Devise::RegistrationsController
|
2
|
+
include Spree::Core::ControllerHelpers
|
3
|
+
helper 'spree/users', 'spree/base'
|
4
4
|
|
5
5
|
ssl_required
|
6
6
|
after_filter :associate_user, :only => :create
|
@@ -51,15 +51,13 @@ class UserRegistrationsController < Devise::RegistrationsController
|
|
51
51
|
end
|
52
52
|
|
53
53
|
protected
|
54
|
+
def check_permissions
|
55
|
+
authorize!(:create, resource)
|
56
|
+
end
|
54
57
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
return unless current_user and current_order
|
61
|
-
current_order.associate_user!(current_user)
|
62
|
-
session[:guest_token] = nil
|
63
|
-
end
|
64
|
-
|
58
|
+
def associate_user
|
59
|
+
return unless current_user and current_order
|
60
|
+
current_order.associate_user!(current_user)
|
61
|
+
session[:guest_token] = nil
|
62
|
+
end
|
65
63
|
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
class UserSessionsController < Devise::SessionsController
|
2
|
-
include
|
3
|
-
helper
|
1
|
+
class Spree::UserSessionsController < Devise::SessionsController
|
2
|
+
include Spree::Core::ControllerHelpers
|
3
|
+
helper 'spree/users', 'spree/base'
|
4
4
|
|
5
|
-
include Spree::CurrentOrder
|
5
|
+
include Spree::Core::CurrentOrder
|
6
6
|
|
7
7
|
after_filter :associate_user, :only => :create
|
8
8
|
|
@@ -20,7 +20,7 @@ class UserSessionsController < Devise::SessionsController
|
|
20
20
|
if user_signed_in?
|
21
21
|
respond_to do |format|
|
22
22
|
format.html {
|
23
|
-
flash
|
23
|
+
flash.notice = t(:logged_in_succesfully)
|
24
24
|
redirect_back_or_default(products_path)
|
25
25
|
}
|
26
26
|
format.js {
|
@@ -29,7 +29,7 @@ class UserSessionsController < Devise::SessionsController
|
|
29
29
|
}
|
30
30
|
end
|
31
31
|
else
|
32
|
-
flash[:error] =
|
32
|
+
flash.now[:error] = t('devise.failure.invalid')
|
33
33
|
render :new
|
34
34
|
end
|
35
35
|
end
|
@@ -40,19 +40,17 @@ class UserSessionsController < Devise::SessionsController
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def nav_bar
|
43
|
-
render :partial =>
|
43
|
+
render :partial => 'spree/shared/nav_bar'
|
44
44
|
end
|
45
45
|
|
46
46
|
private
|
47
|
+
def associate_user
|
48
|
+
return unless current_user and current_order
|
49
|
+
current_order.associate_user!(current_user)
|
50
|
+
session[:guest_token] = nil
|
51
|
+
end
|
47
52
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
session[:guest_token] = nil
|
52
|
-
end
|
53
|
-
|
54
|
-
def accurate_title
|
55
|
-
I18n.t(:log_in)
|
56
|
-
end
|
57
|
-
|
53
|
+
def accurate_title
|
54
|
+
t(:log_in)
|
55
|
+
end
|
58
56
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class UsersController < Spree::BaseController
|
1
|
+
class Spree::UsersController < Spree::BaseController
|
2
2
|
prepend_before_filter :load_object, :only => [:show, :edit, :update]
|
3
3
|
prepend_before_filter :authorize_actions, :only => :new
|
4
4
|
|
@@ -7,7 +7,7 @@ class UsersController < Spree::BaseController
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def create
|
10
|
-
@user = User.new(params[:user])
|
10
|
+
@user = Spree::User.new(params[:user])
|
11
11
|
if @user.save
|
12
12
|
|
13
13
|
if current_order
|
@@ -17,24 +17,21 @@ class UsersController < Spree::BaseController
|
|
17
17
|
|
18
18
|
redirect_back_or_default(root_url)
|
19
19
|
else
|
20
|
-
render
|
20
|
+
render :new
|
21
21
|
end
|
22
|
-
|
23
22
|
end
|
24
23
|
|
25
24
|
def update
|
26
25
|
if @user.update_attributes(params[:user])
|
27
26
|
if params[:user][:password].present?
|
28
27
|
# this logic needed b/c devise wants to log us out after password changes
|
29
|
-
user = User.reset_password_by_token(params[:user])
|
28
|
+
user = Spree::User.reset_password_by_token(params[:user])
|
30
29
|
sign_in(@user, :event => :authentication, :bypass => !Spree::Auth::Config[:signout_after_password_change])
|
31
30
|
end
|
32
|
-
|
33
|
-
redirect_to account_url
|
31
|
+
redirect_to spree.account_url, :notice => t(:account_updated)
|
34
32
|
else
|
35
|
-
render
|
33
|
+
render :edit
|
36
34
|
end
|
37
|
-
|
38
35
|
end
|
39
36
|
|
40
37
|
private
|
@@ -44,11 +41,10 @@ class UsersController < Spree::BaseController
|
|
44
41
|
end
|
45
42
|
|
46
43
|
def authorize_actions
|
47
|
-
authorize! params[:action].to_sym, User
|
44
|
+
authorize! params[:action].to_sym, Spree::User
|
48
45
|
end
|
49
46
|
|
50
47
|
def accurate_title
|
51
|
-
|
48
|
+
t(:my_account)
|
52
49
|
end
|
53
|
-
|
54
50
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Spree
|
2
|
+
module UsersHelper
|
3
|
+
def password_style(user)
|
4
|
+
ActiveSupport::Deprecation.warn '[SPREE] Password style has be depreciated due to the removal of OpenID from the Auth Gem. '
|
5
|
+
'Please install the spree_social gem to regain this functionality and more.'
|
6
|
+
''
|
7
|
+
end
|
8
|
+
|
9
|
+
def openid_style(user)
|
10
|
+
ActiveSupport::Deprecation.warn '[SPREE] Password style has be depreciated due to the removal of OpenID from the Auth Gem. '
|
11
|
+
'Please install the spree_social gem to regain this functionality and more.'
|
12
|
+
'display:none'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
class Spree::UserMailer < ActionMailer::Base
|
2
|
+
def reset_password_instructions(user)
|
3
|
+
default_url_options[:host] = Spree::Config[:site_url]
|
4
|
+
|
5
|
+
@edit_password_reset_url = spree.edit_user_password_url(:reset_password_token => user.reset_password_token)
|
6
|
+
|
7
|
+
mail(:to => user.email,
|
8
|
+
:subject => Spree::Config[:site_name] + ' ' + I18n.t(:password_reset_instructions))
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# Implementation class for Cancan gem. Instead of overriding this class, consider adding new permissions
|
2
|
+
# using the special +register_ability+ method which allows extensions to add their own abilities.
|
3
|
+
#
|
4
|
+
# See http://github.com/ryanb/cancan for more details on cancan.
|
5
|
+
module Spree
|
6
|
+
class Ability
|
7
|
+
include CanCan::Ability
|
8
|
+
|
9
|
+
class_attribute :abilities
|
10
|
+
self.abilities = Set.new
|
11
|
+
|
12
|
+
# Allows us to go beyond the standard cancan initialize method which makes it difficult for engines to
|
13
|
+
# modify the default +Ability+ of an application. The +ability+ argument must be a class that includes
|
14
|
+
# the +CanCan::Ability+ module. The registered ability should behave properly as a stand-alone class
|
15
|
+
# and therefore should be easy to test in isolation.
|
16
|
+
def self.register_ability(ability)
|
17
|
+
self.abilities.add(ability)
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(user)
|
21
|
+
self.clear_aliased_actions
|
22
|
+
|
23
|
+
# override cancan default aliasing (we don't want to differentiate between read and index)
|
24
|
+
alias_action :edit, :to => :update
|
25
|
+
alias_action :new, :to => :create
|
26
|
+
alias_action :new_action, :to => :create
|
27
|
+
alias_action :show, :to => :read
|
28
|
+
|
29
|
+
user ||= Spree::User.new
|
30
|
+
if user.has_role? 'admin'
|
31
|
+
can :manage, :all
|
32
|
+
else
|
33
|
+
#############################
|
34
|
+
can :read, User do |resource|
|
35
|
+
resource == user
|
36
|
+
end
|
37
|
+
can :update, User do |resource|
|
38
|
+
resource == user
|
39
|
+
end
|
40
|
+
can :create, User
|
41
|
+
#############################
|
42
|
+
can :read, Order do |order, token|
|
43
|
+
order.user == user || order.token && token == order.token
|
44
|
+
end
|
45
|
+
can :update, Order do |order, token|
|
46
|
+
order.user == user || order.token && token == order.token
|
47
|
+
end
|
48
|
+
can :create, Order
|
49
|
+
#############################
|
50
|
+
can :read, Product
|
51
|
+
can :index, Product
|
52
|
+
#############################
|
53
|
+
can :read, Taxon
|
54
|
+
can :index, Taxon
|
55
|
+
#############################
|
56
|
+
end
|
57
|
+
|
58
|
+
#include any abilities registered by extensions, etc.
|
59
|
+
Ability.abilities.each do |clazz|
|
60
|
+
ability = clazz.send(:new, user)
|
61
|
+
@rules = rules + ability.send(:rules)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -1,5 +1,4 @@
|
|
1
|
-
Spree::CurrentOrder.module_eval do
|
2
|
-
|
1
|
+
Spree::Core::CurrentOrder.module_eval do
|
3
2
|
# Associate the new order with the currently authenticated user before saving
|
4
3
|
def before_save_new_order
|
5
4
|
@current_order.user ||= current_user
|
@@ -10,5 +9,4 @@ Spree::CurrentOrder.module_eval do
|
|
10
9
|
return if current_user
|
11
10
|
session[:access_token] = @current_order.token
|
12
11
|
end
|
13
|
-
|
14
12
|
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module Spree
|
2
|
+
class User < ActiveRecord::Base
|
3
|
+
devise :database_authenticatable, :token_authenticatable, :registerable, :recoverable,
|
4
|
+
:rememberable, :trackable, :validatable, :encryptable, :encryptor => 'authlogic_sha512'
|
5
|
+
|
6
|
+
has_many :orders
|
7
|
+
has_and_belongs_to_many :roles, :join_table => 'spree_roles_users'
|
8
|
+
belongs_to :ship_address, :foreign_key => 'ship_address_id', :class_name => 'Spree::Address'
|
9
|
+
belongs_to :bill_address, :foreign_key => 'bill_address_id', :class_name => 'Spree::Address'
|
10
|
+
|
11
|
+
before_save :check_admin
|
12
|
+
before_validation :set_login
|
13
|
+
|
14
|
+
# Setup accessible (or protected) attributes for your model
|
15
|
+
attr_accessible :email, :password, :password_confirmation, :remember_me, :persistence_token
|
16
|
+
|
17
|
+
users_table_name = User.table_name
|
18
|
+
roles_table_name = Role.table_name
|
19
|
+
|
20
|
+
scope :admin, lambda { includes(:roles).where("#{roles_table_name}.name" => "admin") }
|
21
|
+
scope :registered, where("#{users_table_name}.email NOT LIKE ?", "%@example.net")
|
22
|
+
|
23
|
+
# has_role? simply needs to return true or false whether a user has a role or not.
|
24
|
+
def has_role?(role_in_question)
|
25
|
+
roles.any? { |role| role.name == role_in_question.to_s }
|
26
|
+
end
|
27
|
+
|
28
|
+
# Creates an anonymous user. An anonymous user is basically an auto-generated +User+ account that is created for the customer
|
29
|
+
# behind the scenes and its completely transparently to the customer. All +Orders+ must have a +User+ so this is necessary
|
30
|
+
# when adding to the "cart" (which is really an order) and before the customer has a chance to provide an email or to register.
|
31
|
+
def self.anonymous!
|
32
|
+
token = User.generate_token(:persistence_token)
|
33
|
+
User.create(:email => "#{token}@example.net", :password => token, :password_confirmation => token, :persistence_token => token)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.admin_created?
|
37
|
+
User.admin.count > 0
|
38
|
+
end
|
39
|
+
|
40
|
+
def anonymous?
|
41
|
+
email =~ /@example.net$/
|
42
|
+
end
|
43
|
+
|
44
|
+
def send_reset_password_instructions
|
45
|
+
generate_reset_password_token!
|
46
|
+
UserMailer.reset_password_instructions(self).deliver
|
47
|
+
end
|
48
|
+
|
49
|
+
protected
|
50
|
+
def password_required?
|
51
|
+
!persisted? || password.present? || password_confirmation.present?
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
def check_admin
|
56
|
+
return if self.class.admin_created?
|
57
|
+
admin_role = Role.find_or_create_by_name 'admin'
|
58
|
+
self.roles << admin_role
|
59
|
+
end
|
60
|
+
|
61
|
+
def set_login
|
62
|
+
# for now force login to be same as email, eventually we will make this configurable, etc.
|
63
|
+
self.login ||= self.email if self.email
|
64
|
+
end
|
65
|
+
|
66
|
+
# Generate a friendly string randomically to be used as token.
|
67
|
+
def self.friendly_token
|
68
|
+
SecureRandom.base64(15).tr('+/=', '-_ ').strip.delete("\n")
|
69
|
+
end
|
70
|
+
|
71
|
+
# Generate a token by looping and ensuring does not already exist.
|
72
|
+
def self.generate_token(column)
|
73
|
+
loop do
|
74
|
+
token = friendly_token
|
75
|
+
break token unless find(:first, :conditions => { column => token })
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.current
|
80
|
+
Thread.current[:user]
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.current=(user)
|
84
|
+
Thread.current[:user] = user
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
Deface::Override.new(:virtual_path => "layouts/admin",
|
1
|
+
Deface::Override.new(:virtual_path => "spree/layouts/admin",
|
2
2
|
:name => "auth_admin_login_navigation_bar",
|
3
3
|
:replace => "[data-hook='admin_login_navigation_bar'], #admin_login_navigation_bar[data-hook]",
|
4
|
-
:partial => "layouts/admin/login_nav")
|
4
|
+
:partial => "spree/layouts/admin/login_nav")
|
@@ -1,6 +1,6 @@
|
|
1
|
-
Deface::Override.new(:virtual_path => "shared/_nav_bar",
|
1
|
+
Deface::Override.new(:virtual_path => "spree/shared/_nav_bar",
|
2
2
|
:name => "auth_shared_login_bar",
|
3
3
|
:insert_after => "li#search-bar",
|
4
|
-
:partial => "shared/login_bar",
|
4
|
+
:partial => "spree/shared/login_bar",
|
5
5
|
:disabled => false)
|
6
6
|
|