solidus_auth_devise_devise_token_auth 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +20 -0
  5. data/CHANGELOG.md +173 -0
  6. data/Gemfile +36 -0
  7. data/LICENSE.md +26 -0
  8. data/README.md +98 -0
  9. data/Rakefile +21 -0
  10. data/app/mailers/spree/user_mailer.rb +15 -0
  11. data/app/models/spree/auth_configuration.rb +9 -0
  12. data/app/models/spree/user.rb +47 -0
  13. data/app/overrides/auth_admin_login_navigation_bar.rb +10 -0
  14. data/app/overrides/auth_shared_login_bar.rb +10 -0
  15. data/bin/rails +7 -0
  16. data/circle.yml +6 -0
  17. data/config/initializers/devise.rb +141 -0
  18. data/config/initializers/warden.rb +14 -0
  19. data/config/locales/de.yml +53 -0
  20. data/config/locales/en.yml +54 -0
  21. data/config/locales/es.yml +50 -0
  22. data/config/locales/fr.yml +50 -0
  23. data/config/locales/it.yml +76 -0
  24. data/config/locales/nl.yml +50 -0
  25. data/config/locales/pt-BR.yml +52 -0
  26. data/config/locales/tr.yml +50 -0
  27. data/config/locales/zh-CN.yml +54 -0
  28. data/config/routes.rb +71 -0
  29. data/db/default/users.rb +75 -0
  30. data/db/migrate/20101026184949_create_users.rb +29 -0
  31. data/db/migrate/20101026184950_rename_columns_for_devise.rb +41 -0
  32. data/db/migrate/20101214150824_convert_user_remember_field.rb +11 -0
  33. data/db/migrate/20120203010234_add_reset_password_sent_at_to_spree_users.rb +8 -0
  34. data/db/migrate/20120605211305_make_users_email_index_unique.rb +9 -0
  35. data/db/migrate/20140904000425_add_deleted_at_to_users.rb +6 -0
  36. data/db/migrate/20141002154641_add_confirmable_to_users.rb +7 -0
  37. data/db/seeds.rb +1 -0
  38. data/lib/assets/javascripts/spree/backend/solidus_auth.js +1 -0
  39. data/lib/assets/javascripts/spree/frontend/solidus_auth.js +1 -0
  40. data/lib/assets/stylesheets/spree/backend/solidus_auth.css +3 -0
  41. data/lib/assets/stylesheets/spree/frontend/solidus_auth.css +3 -0
  42. data/lib/controllers/backend/spree/admin/admin_controller_decorator.rb +11 -0
  43. data/lib/controllers/backend/spree/admin/orders/customer_details_controller_decorator.rb +15 -0
  44. data/lib/controllers/backend/spree/admin/user_passwords_controller.rb +41 -0
  45. data/lib/controllers/backend/spree/admin/user_sessions_controller.rb +43 -0
  46. data/lib/controllers/frontend/spree/checkout_controller_decorator.rb +66 -0
  47. data/lib/controllers/frontend/spree/user_confirmations_controller.rb +14 -0
  48. data/lib/controllers/frontend/spree/user_passwords_controller.rb +50 -0
  49. data/lib/controllers/frontend/spree/user_registrations_controller.rb +40 -0
  50. data/lib/controllers/frontend/spree/user_sessions_controller.rb +64 -0
  51. data/lib/controllers/frontend/spree/users_controller.rb +57 -0
  52. data/lib/generators/solidus/auth/install/install_generator.rb +25 -0
  53. data/lib/generators/solidus/auth/install/templates/config/initializers/devise.rb +1 -0
  54. data/lib/solidus/auth.rb +2 -0
  55. data/lib/solidus_auth_devise.rb +13 -0
  56. data/lib/spree/auth/devise.rb +14 -0
  57. data/lib/spree/auth/engine.rb +80 -0
  58. data/lib/spree/authentication_helpers.rb +33 -0
  59. data/lib/tasks/auth.rake +9 -0
  60. data/lib/views/backend/spree/admin/shared/_navigation_footer.html.erb +25 -0
  61. data/lib/views/backend/spree/admin/user_passwords/edit.html.erb +15 -0
  62. data/lib/views/backend/spree/admin/user_passwords/new.html.erb +17 -0
  63. data/lib/views/backend/spree/admin/user_sessions/authorization_failure.html.erb +4 -0
  64. data/lib/views/backend/spree/admin/user_sessions/new.html.erb +31 -0
  65. data/lib/views/backend/spree/layouts/admin/_login_nav.html.erb +13 -0
  66. data/lib/views/frontend/spree/checkout/registration.html.erb +22 -0
  67. data/lib/views/frontend/spree/shared/_flashes.html.erb +9 -0
  68. data/lib/views/frontend/spree/shared/_login.html.erb +18 -0
  69. data/lib/views/frontend/spree/shared/_login_bar.html.erb +2 -0
  70. data/lib/views/frontend/spree/shared/_login_bar_items.html.erb +6 -0
  71. data/lib/views/frontend/spree/shared/_user_form.html.erb +17 -0
  72. data/lib/views/frontend/spree/user_mailer/confirmation_instructions.text.erb +5 -0
  73. data/lib/views/frontend/spree/user_mailer/reset_password_instructions.text.erb +10 -0
  74. data/lib/views/frontend/spree/user_passwords/edit.html.erb +17 -0
  75. data/lib/views/frontend/spree/user_passwords/new.html.erb +17 -0
  76. data/lib/views/frontend/spree/user_registrations/new.html.erb +21 -0
  77. data/lib/views/frontend/spree/user_sessions/authorization_failure.html.erb +4 -0
  78. data/lib/views/frontend/spree/user_sessions/new.html.erb +13 -0
  79. data/lib/views/frontend/spree/users/edit.html.erb +14 -0
  80. data/lib/views/frontend/spree/users/show.html.erb +43 -0
  81. data/solidus_auth_devise.gemspec +41 -0
  82. data/spec/controllers/spree/checkout_controller_spec.rb +196 -0
  83. data/spec/controllers/spree/products_controller_spec.rb +27 -0
  84. data/spec/controllers/spree/user_passwords_controller_spec.rb +44 -0
  85. data/spec/controllers/spree/user_registrations_controller_spec.rb +96 -0
  86. data/spec/controllers/spree/user_sessions_controller_spec.rb +113 -0
  87. data/spec/controllers/spree/users_controller_spec.rb +38 -0
  88. data/spec/factories/confirmed_user.rb +7 -0
  89. data/spec/features/account_spec.rb +58 -0
  90. data/spec/features/admin/orders_spec.rb +30 -0
  91. data/spec/features/admin/password_reset_spec.rb +24 -0
  92. data/spec/features/admin/products_spec.rb +9 -0
  93. data/spec/features/admin/sign_in_spec.rb +44 -0
  94. data/spec/features/admin/sign_out_spec.rb +22 -0
  95. data/spec/features/admin_permissions_spec.rb +46 -0
  96. data/spec/features/change_email_spec.rb +26 -0
  97. data/spec/features/checkout_spec.rb +181 -0
  98. data/spec/features/confirmation_spec.rb +32 -0
  99. data/spec/features/order_spec.rb +62 -0
  100. data/spec/features/password_reset_spec.rb +24 -0
  101. data/spec/features/sign_in_spec.rb +52 -0
  102. data/spec/features/sign_out_spec.rb +25 -0
  103. data/spec/features/sign_up_spec.rb +30 -0
  104. data/spec/mailers/user_mailer_spec.rb +46 -0
  105. data/spec/models/order_spec.rb +26 -0
  106. data/spec/models/user_spec.rb +63 -0
  107. data/spec/spec_helper.rb +17 -0
  108. data/spec/support/ability.rb +15 -0
  109. data/spec/support/authentication_helpers.rb +14 -0
  110. data/spec/support/confirm_helpers.rb +13 -0
  111. data/spec/support/email.rb +5 -0
  112. data/spec/support/preferences.rb +5 -0
  113. data/spec/support/spree.rb +10 -0
  114. metadata +446 -0
@@ -0,0 +1,8 @@
1
+ class AddResetPasswordSentAtToSpreeUsers < SolidusSupport::Migration[4.2]
2
+ def change
3
+ Spree::User.reset_column_information
4
+ unless Spree::User.column_names.include?("reset_password_sent_at")
5
+ add_column :spree_users, :reset_password_sent_at, :datetime
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ class MakeUsersEmailIndexUnique < SolidusSupport::Migration[4.2]
2
+ def up
3
+ add_index "spree_users", ["email"], :name => "email_idx_unique", :unique => true
4
+ end
5
+
6
+ def down
7
+ remove_index "spree_users", :name => "email_idx_unique"
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ class AddDeletedAtToUsers < SolidusSupport::Migration[4.2]
2
+ def change
3
+ add_column :spree_users, :deleted_at, :datetime
4
+ add_index :spree_users, :deleted_at
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ class AddConfirmableToUsers < SolidusSupport::Migration[4.2]
2
+ def change
3
+ add_column :spree_users, :confirmation_token, :string
4
+ add_column :spree_users, :confirmed_at, :datetime
5
+ add_column :spree_users, :confirmation_sent_at, :datetime
6
+ end
7
+ end
data/db/seeds.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'default/users.rb'
@@ -0,0 +1 @@
1
+ //= require spree/backend
@@ -0,0 +1 @@
1
+ //= require spree/frontend
@@ -0,0 +1,3 @@
1
+ /*
2
+ *= require spree/backend
3
+ */
@@ -0,0 +1,3 @@
1
+ /*
2
+ *= require spree/frontend
3
+ */
@@ -0,0 +1,11 @@
1
+ Spree::Admin::BaseController.class_eval do
2
+ protected
3
+
4
+ def model_class
5
+ const_name = controller_name.classify
6
+ if Spree.const_defined?(const_name, false)
7
+ return "Spree::#{const_name}".constantize
8
+ end
9
+ nil
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ Spree::Admin::Orders::CustomerDetailsController.class_eval do
2
+ before_action :check_authorization
3
+
4
+ private
5
+ def check_authorization
6
+ load_order
7
+ session[:access_token] ||= params[:token]
8
+
9
+ resource = @order
10
+ action = params[:action].to_sym
11
+ action = :edit if action == :show # show route renders :edit for this controller
12
+
13
+ authorize! action, resource, session[:access_token]
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ class Spree::Admin::UserPasswordsController < Devise::PasswordsController
2
+ helper 'spree/base'
3
+
4
+ include Spree::Core::ControllerHelpers::Auth
5
+ include Spree::Core::ControllerHelpers::Common
6
+ include Spree::Core::ControllerHelpers::Store
7
+
8
+ helper 'spree/admin/navigation'
9
+ layout 'spree/layouts/admin'
10
+
11
+ # Overridden due to bug in Devise.
12
+ # respond_with resource, location: new_session_path(resource_name)
13
+ # is generating bad url /session/new.user
14
+ #
15
+ # overridden to:
16
+ # respond_with resource, location: spree.login_path
17
+ #
18
+ def create
19
+ self.resource = resource_class.send_reset_password_instructions(params[resource_name])
20
+
21
+ if resource.errors.empty?
22
+ set_flash_message(:notice, :send_instructions) if is_navigational_format?
23
+ respond_with resource, location: spree.admin_login_path
24
+ else
25
+ respond_with_navigational(resource) { render :new }
26
+ end
27
+ end
28
+
29
+ # Devise::PasswordsController allows for blank passwords.
30
+ # Silly Devise::PasswordsController!
31
+ # Fixes spree/spree#2190.
32
+ def update
33
+ if params[:spree_user][:password].blank?
34
+ set_flash_message(:error, :cannot_be_blank)
35
+ render :edit
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,43 @@
1
+ class Spree::Admin::UserSessionsController < Devise::SessionsController
2
+ helper 'spree/base'
3
+
4
+ include Spree::Core::ControllerHelpers::Auth
5
+ include Spree::Core::ControllerHelpers::Common
6
+ include Spree::Core::ControllerHelpers::Store
7
+
8
+ helper 'spree/admin/navigation'
9
+ layout 'spree/layouts/admin'
10
+
11
+ def create
12
+ authenticate_spree_user!
13
+
14
+ if spree_user_signed_in?
15
+ respond_to do |format|
16
+ format.html {
17
+ flash[:success] = Spree.t(:logged_in_succesfully)
18
+ redirect_back_or_default(after_sign_in_path_for(spree_current_user))
19
+ }
20
+ format.js {
21
+ user = resource.record
22
+ render json: {ship_address: user.ship_address, bill_address: user.bill_address}.to_json
23
+ }
24
+ end
25
+ else
26
+ flash.now[:error] = t('devise.failure.invalid')
27
+ render :new
28
+ end
29
+ end
30
+
31
+ def authorization_failure
32
+ end
33
+
34
+ private
35
+ def accurate_title
36
+ Spree.t(:login)
37
+ end
38
+
39
+ def redirect_back_or_default(default)
40
+ redirect_to(session["spree_user_return_to"] || default)
41
+ session["spree_user_return_to"] = nil
42
+ end
43
+ end
@@ -0,0 +1,66 @@
1
+ Spree::CheckoutController.class_eval do
2
+ prepend_before_action :check_registration,
3
+ except: [:registration, :update_registration]
4
+ prepend_before_action :check_authorization
5
+
6
+ # This action builds some associations on the order, ex. addresses, which we
7
+ # don't to build or save here.
8
+ skip_before_action :setup_for_current_state, only: [:registration, :update_registration]
9
+
10
+ def registration
11
+ @user = Spree::User.new
12
+ end
13
+
14
+ def update_registration
15
+ if params[:order][:email] =~ Devise.email_regexp && current_order.update_attributes(email: params[:order][:email])
16
+ redirect_to spree.checkout_path
17
+ else
18
+ flash[:registration_error] = t(:email_is_invalid, scope: [:errors, :messages])
19
+ @user = Spree::User.new
20
+ render 'registration'
21
+ end
22
+ end
23
+
24
+ private
25
+ def order_params
26
+ params.
27
+ fetch(:order, {}).
28
+ permit(:email)
29
+ end
30
+
31
+ def skip_state_validation?
32
+ %w(registration update_registration).include?(params[:action])
33
+ end
34
+
35
+ def check_authorization
36
+ authorize!(:edit, current_order, cookies.signed[:guest_token])
37
+ end
38
+
39
+ # Introduces a registration step whenever the +registration_step+ preference is true.
40
+ def check_registration
41
+ return unless registration_required?
42
+ store_location
43
+ redirect_to spree.checkout_registration_path
44
+ end
45
+
46
+ def registration_required?
47
+ Spree::Auth::Config[:registration_step] &&
48
+ !already_registered?
49
+ end
50
+
51
+ def already_registered?
52
+ spree_current_user || guest_authenticated?
53
+ end
54
+
55
+ def guest_authenticated?
56
+ current_order.try!(:email).present? &&
57
+ Spree::Config[:allow_guest_checkout]
58
+ end
59
+
60
+ # Overrides the equivalent method defined in Spree::Core. This variation of the method will ensure that users
61
+ # are redirected to the tokenized order url unless authenticated as a registered user.
62
+ def completion_route
63
+ return spree.order_path(@order) if spree_current_user
64
+ spree.token_order_path(@order, @order.guest_token)
65
+ end
66
+ end
@@ -0,0 +1,14 @@
1
+ class Spree::UserConfirmationsController < Devise::ConfirmationsController
2
+ helper 'spree/base', 'spree/store'
3
+
4
+ include Spree::Core::ControllerHelpers::Auth
5
+ include Spree::Core::ControllerHelpers::Common
6
+ include Spree::Core::ControllerHelpers::Order
7
+ include Spree::Core::ControllerHelpers::Store
8
+
9
+ protected
10
+
11
+ def after_confirmation_path_for(resource_name, resource)
12
+ signed_in?(resource_name) ? spree.signed_in_root_path(resource) : spree.login_path
13
+ end
14
+ end
@@ -0,0 +1,50 @@
1
+ class Spree::UserPasswordsController < Devise::PasswordsController
2
+ helper 'spree/base', 'spree/store'
3
+
4
+ include Spree::Core::ControllerHelpers::Auth
5
+ include Spree::Core::ControllerHelpers::Common
6
+ include Spree::Core::ControllerHelpers::Order
7
+ include Spree::Core::ControllerHelpers::Store
8
+
9
+ # Overridden due to bug in Devise.
10
+ # respond_with resource, location: new_session_path(resource_name)
11
+ # is generating bad url /session/new.user
12
+ #
13
+ # overridden to:
14
+ # respond_with resource, location: spree.login_path
15
+ #
16
+ def create
17
+ self.resource = resource_class.send_reset_password_instructions(params[resource_name])
18
+
19
+ if resource.errors.empty?
20
+ set_flash_message(:notice, :send_instructions) if is_navigational_format?
21
+ respond_with resource, location: spree.login_path
22
+ else
23
+ respond_with_navigational(resource) { render :new }
24
+ end
25
+ end
26
+
27
+ # Devise::PasswordsController allows for blank passwords.
28
+ # Silly Devise::PasswordsController!
29
+ # Fixes spree/spree#2190.
30
+ def update
31
+ if params[:spree_user][:password].blank?
32
+ self.resource = resource_class.new
33
+ resource.reset_password_token = params[:spree_user][:reset_password_token]
34
+ set_flash_message(:error, :cannot_be_blank)
35
+ render :edit
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ protected
42
+
43
+ def translation_scope
44
+ 'devise.user_passwords'
45
+ end
46
+
47
+ def new_session_path(resource_name)
48
+ spree.send("new_#{resource_name}_session_path")
49
+ end
50
+ end
@@ -0,0 +1,40 @@
1
+ class Spree::UserRegistrationsController < Devise::RegistrationsController
2
+ helper 'spree/base', 'spree/store'
3
+
4
+ include Spree::Core::ControllerHelpers::Auth
5
+ include Spree::Core::ControllerHelpers::Common
6
+ include Spree::Core::ControllerHelpers::Order
7
+ include Spree::Core::ControllerHelpers::Store
8
+
9
+ before_action :check_permissions, only: [:edit, :update]
10
+ skip_before_action :require_no_authentication
11
+
12
+ def create
13
+ build_resource(spree_user_params)
14
+ if resource.save
15
+ set_flash_message(:notice, :signed_up)
16
+ sign_in(:spree_user, resource)
17
+ session[:spree_user_signup] = true
18
+ respond_with resource, location: after_sign_up_path_for(resource)
19
+ else
20
+ clean_up_passwords(resource)
21
+ respond_with(resource) do |format|
22
+ format.html { render :new }
23
+ end
24
+ end
25
+ end
26
+
27
+ protected
28
+ def translation_scope
29
+ 'devise.user_registrations'
30
+ end
31
+
32
+ def check_permissions
33
+ authorize!(:create, resource)
34
+ end
35
+
36
+ private
37
+ def spree_user_params
38
+ params.require(:spree_user).permit(Spree::PermittedAttributes.user_attributes | [:email])
39
+ end
40
+ end
@@ -0,0 +1,64 @@
1
+ class Spree::UserSessionsController < Devise::SessionsController
2
+ helper 'spree/base', 'spree/store'
3
+
4
+ include Spree::Core::ControllerHelpers::Auth
5
+ include Spree::Core::ControllerHelpers::Common
6
+ include Spree::Core::ControllerHelpers::Order
7
+ include Spree::Core::ControllerHelpers::Store
8
+
9
+ # This is included in ControllerHelpers::Order. We just want to call
10
+ # it after someone has successfully logged in.
11
+ after_action :set_current_order, only: :create
12
+
13
+ def create
14
+ authenticate_spree_user!
15
+
16
+ if spree_user_signed_in?
17
+ respond_to do |format|
18
+ format.html do
19
+ flash[:success] = Spree.t(:logged_in_succesfully)
20
+ redirect_back_or_default(after_sign_in_path_for(spree_current_user))
21
+ end
22
+ format.js { render success_json }
23
+ end
24
+ else
25
+ respond_to do |format|
26
+ format.html do
27
+ flash.now[:error] = t('devise.failure.invalid')
28
+ render :new
29
+ end
30
+ format.js do
31
+ render json: { error: t('devise.failure.invalid') },
32
+ status: :unprocessable_entity
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ protected
39
+
40
+ def translation_scope
41
+ 'devise.user_sessions'
42
+ end
43
+
44
+ private
45
+
46
+ def accurate_title
47
+ Spree.t(:login)
48
+ end
49
+
50
+ def redirect_back_or_default(default)
51
+ redirect_to(session["spree_user_return_to"] || default)
52
+ session["spree_user_return_to"] = nil
53
+ end
54
+
55
+ def success_json
56
+ {
57
+ json: {
58
+ user: spree_current_user,
59
+ ship_address: spree_current_user.ship_address,
60
+ bill_address: spree_current_user.bill_address
61
+ }.to_json
62
+ }
63
+ end
64
+ end
@@ -0,0 +1,57 @@
1
+ class Spree::UsersController < Spree::StoreController
2
+ skip_before_action :set_current_order, only: :show, raise: false
3
+ prepend_before_action :load_object, only: [:show, :edit, :update]
4
+ prepend_before_action :authorize_actions, only: :new
5
+
6
+ include Spree::Core::ControllerHelpers
7
+
8
+ def show
9
+ @orders = @user.orders.complete.order('completed_at desc')
10
+ end
11
+
12
+ def create
13
+ @user = Spree::User.new(user_params)
14
+ if @user.save
15
+
16
+ if current_order
17
+ session[:guest_token] = nil
18
+ end
19
+
20
+ redirect_back_or_default(root_url)
21
+ else
22
+ render :new
23
+ end
24
+ end
25
+
26
+ def update
27
+ if @user.update_attributes(user_params)
28
+ if params[:user][:password].present?
29
+ # this logic needed b/c devise wants to log us out after password changes
30
+ unless Spree::Auth::Config[:signout_after_password_change]
31
+ bypass_sign_in(@user)
32
+ end
33
+ end
34
+ redirect_to spree.account_url, notice: Spree.t(:account_updated)
35
+ else
36
+ render :edit
37
+ end
38
+ end
39
+
40
+ private
41
+ def user_params
42
+ params.require(:user).permit(Spree::PermittedAttributes.user_attributes | [:email])
43
+ end
44
+
45
+ def load_object
46
+ @user ||= spree_current_user
47
+ authorize! params[:action].to_sym, @user
48
+ end
49
+
50
+ def authorize_actions
51
+ authorize! params[:action].to_sym, Spree::User.new
52
+ end
53
+
54
+ def accurate_title
55
+ Spree.t(:my_account)
56
+ end
57
+ end