Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix invitations when inviting already existing user (#12712)

Damian Kaczmarczyk01a21265c11d
diff --git a/admin/app/controllers/spree/admin/base_controller.rb b/admin/app/controllers/spree/admin/base_controller.rb
index 767b444923..a019e9019f 100644
--- a/admin/app/controllers/spree/admin/base_controller.rb
+++ b/admin/app/controllers/spree/admin/base_controller.rb
@@ -36,15 +36,19 @@ module Spree
redirect_to spree.admin_forbidden_path, allow_other_host: true
else
store_location
- if defined?(spree_admin_login_path)
- redirect_to spree_admin_login_path, allow_other_host: true
- elsif respond_to?(:spree_login_path)
- redirect_to spree_login_path, allow_other_host: true
- elsif spree.respond_to?(:root_path)
- redirect_to spree.root_path, allow_other_host: true
- else
- redirect_to main_app.respond_to?(:root_path) ? main_app.root_path : '/'
- end
+ try_to_redirect_to_login_path
+ end
+ end
+
+ def try_to_redirect_to_login_path
+ if defined?(spree_admin_login_path)
+ redirect_to spree_admin_login_path, allow_other_host: true
+ elsif respond_to?(:spree_login_path)
+ redirect_to spree_login_path, allow_other_host: true
+ elsif spree.respond_to?(:root_path)
+ redirect_to spree.root_path, allow_other_host: true
+ else
+ redirect_to main_app.respond_to?(:root_path) ? main_app.root_path : '/'
end
end
diff --git a/admin/app/controllers/spree/admin/invitations_controller.rb b/admin/app/controllers/spree/admin/invitations_controller.rb
index 2fb3045eab..6c1834be0a 100644
--- a/admin/app/controllers/spree/admin/invitations_controller.rb
+++ b/admin/app/controllers/spree/admin/invitations_controller.rb
@@ -1,10 +1,10 @@
module Spree
module Admin
class InvitationsController < BaseController
- skip_before_action :authorize_admin, only: [:show]
+ skip_before_action :authorize_admin, only: [:show, :accept]
before_action :load_parent, except: [:show]
- before_action :load_invitation, only: [:accept, :destroy]
+ before_action :load_invitation, only: [:destroy]
before_action :load_roles, only: [:new, :create]
layout :choose_layout
@@ -48,28 +48,30 @@ module Spree
# GET /admin/invitations/:id?token=:token
def show
- begin
- @invitation = Spree::Invitation.pending.not_expired.find_by!(id: params[:id], token: params[:token])
- @parent = @invitation.resource
+ @invitation = Spree::Invitation.pending.not_expired.find_by!(id: params[:id], token: params[:token])
+ @parent = @invitation.resource
- if try_spree_current_user != @invitation.invitee
+ if try_spree_current_user.present?
+ unless @invitation.invitee == try_spree_current_user
raise ActiveRecord::RecordNotFound
end
- rescue ActiveRecord::RecordNotFound
- redirect_to spree.root_path, alert: Spree.t('invalid_or_expired_invitation')
- return
- end
-
- # if there's no current user, redirect to the new admin user path so they can sign up
- unless try_spree_current_user
+ elsif @invitation.invitee.present?
+ store_location
+ try_to_redirect_to_login_path
+ else
redirect_to spree.new_admin_admin_user_path(token: @invitation.token), status: :see_other
- return
end
+ rescue ActiveRecord::RecordNotFound
+ redirect_to spree.root_path, alert: Spree.t('invalid_or_expired_invitation')
+ nil
end
# PUT /admin/invitations/:id/accept
def accept
@invitation = try_spree_current_user.invitations.pending.not_expired.find(params[:id])
+
+ authorize! :accept, @invitation
+
@invitation.accept!
redirect_to spree.admin_path, notice: Spree.t('invitation_accepted')
end
diff --git a/admin/spec/controllers/spree/admin/invitations_controller_spec.rb b/admin/spec/controllers/spree/admin/invitations_controller_spec.rb
index 6359910155..a817ef29f6 100644
--- a/admin/spec/controllers/spree/admin/invitations_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/invitations_controller_spec.rb
@@ -9,6 +9,7 @@ RSpec.describe Spree::Admin::InvitationsController, type: :controller do
let(:role) { Spree::Role.find_or_create_by!(name: 'admin') }
before do
+ allow(controller).to receive(:spree_admin_login_path).and_return('/admin/login')
allow(spree).to receive(:root_path).and_return('/')
end
@@ -82,6 +83,19 @@ RSpec.describe Spree::Admin::InvitationsController, type: :controller do
post :create, params: valid_params
expect(flash[:notice]).not_to be_nil
end
+
+ context 'when the invitee already exists' do
+ let!(:invitee) { create(:admin_user, :without_admin_role, email: valid_params[:invitation][:email]) }
+
+ before do
+ post :create, params: valid_params
+ end
+
+ it 'sets the invitee' do
+ expect(assigns(:invitation).invitee).to eq(invitee)
+ expect(assigns(:invitation)).to be_persisted
+ end
+ end
end
context 'with invalid parameters' do
@@ -115,7 +129,7 @@ RSpec.describe Spree::Admin::InvitationsController, type: :controller do
let(:token) { invitation.token }
context 'when user is logged in' do
- let(:another_user) { create(:user) }
+ let(:another_user) { create(:admin_user, :without_admin_role) }
before do
allow(controller).to receive(:try_spree_current_user).and_return(another_user)
@@ -187,12 +201,25 @@ RSpec.describe Spree::Admin::InvitationsController, type: :controller do
end
end
- context 'with already accepted invitation' do
- let(:invitation) { create(:invitation, inviter: admin_user, resource: store, status: 'accepted', invitee: create(:admin_user, :without_admin_role)) }
+ context 'when invitation has invitee' do
+ let(:invitation) { create(:invitation, inviter: admin_user, resource: store, invitee: invitee, email: invitee.email) }
+ let(:invitee) { create(:admin_user, :without_admin_role) }
- it 'redirects to root path' do
- expect(response).to redirect_to(spree.root_path)
- expect(flash[:alert]).to eq(Spree.t('invalid_or_expired_invitation'))
+ it 'redirects to login path' do
+ expect(response).to redirect_to(controller.spree_admin_login_path)
+ end
+
+ it 'stores the return to path' do
+ expect(session["#{Spree.admin_user_class.model_name.singular_route_key.to_sym}_return_to"]).to eq(spree.admin_invitation_path(invitation, token: invitation.token))
+ end
+
+ context 'with already accepted invitation' do
+ let(:invitation) { create(:invitation, inviter: admin_user, resource: store, status: 'accepted', invitee: invitee, email: invitee.email) }
+
+ it 'redirects to root path' do
+ expect(response).to redirect_to(spree.root_path)
+ expect(flash[:alert]).to eq(Spree.t('invalid_or_expired_invitation'))
+ end
end
end
end
diff --git a/core/app/models/spree/invitation.rb b/core/app/models/spree/invitation.rb
index 67e92176a7..583eef75ff 100644
--- a/core/app/models/spree/invitation.rb
+++ b/core/app/models/spree/invitation.rb
@@ -51,6 +51,7 @@ module Spree
# Callbacks
#
after_initialize :set_defaults, if: :new_record?
+ before_validation :set_invitee_from_email, on: :create
after_create :send_invitation_email, unless: :skip_email
# returns the store for the invitation
@@ -135,5 +136,11 @@ module Spree
self.role_user = role_user
save!
end
+
+ def set_invitee_from_email
+ return if invitee.present?
+
+ self.invitee = Spree.admin_user_class.find_by(email: email)
+ end
end
end
diff --git a/core/spec/models/spree/invitation_spec.rb b/core/spec/models/spree/invitation_spec.rb
index 94f0bdb4e7..81cf44ec50 100644
--- a/core/spec/models/spree/invitation_spec.rb
+++ b/core/spec/models/spree/invitation_spec.rb
@@ -41,6 +41,15 @@ RSpec.describe Spree::Invitation, type: :model do
it 'sends invitation email after create' do
expect { invitation.save }.to have_enqueued_job(ActionMailer::MailDeliveryJob)
end
+
+ it 'sets invitee from email before validation' do
+ create(:admin_user, :without_admin_role, email: '[email protected]')
+
+ invitation = build(:invitation, email: '[email protected]')
+ expect(invitation.invitee).to be_nil
+ invitation.save
+ expect(invitation.invitee).to be_present
+ end
end
describe 'State Machine' do