-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3007 from ericsaupe/wallet-payment-source-protection
Verify ownership of payment_source when creating WalletPaymentSource
- Loading branch information
Showing
6 changed files
with
68 additions
and
24 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
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,19 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
class Spree::WalletPaymentSource < ActiveRecord::Base | ||
belongs_to :user, class_name: Spree::UserClassHandle.new, foreign_key: 'user_id', inverse_of: :wallet_payment_sources | ||
belongs_to :payment_source, polymorphic: true, inverse_of: :wallet_payment_sources | ||
module Spree | ||
class WalletPaymentSource < Spree::Base | ||
belongs_to :user, class_name: Spree::UserClassHandle.new, foreign_key: 'user_id', inverse_of: :wallet_payment_sources | ||
belongs_to :payment_source, polymorphic: true, inverse_of: :wallet_payment_sources | ||
|
||
validates_presence_of :user | ||
validates_presence_of :payment_source | ||
validates_presence_of :user | ||
validates_presence_of :payment_source | ||
validates :user_id, uniqueness: { | ||
scope: [:payment_source_type, :payment_source_id], | ||
message: :payment_source_already_exists | ||
} | ||
|
||
validate :check_for_payment_source_class | ||
validate :check_for_payment_source_class | ||
validate :validate_payment_source_ownership | ||
|
||
private | ||
private | ||
|
||
def check_for_payment_source_class | ||
if !payment_source.is_a?(Spree::PaymentSource) | ||
errors.add(:payment_source, :has_to_be_payment_source_class) | ||
def check_for_payment_source_class | ||
if !payment_source.is_a?(Spree::PaymentSource) | ||
errors.add(:payment_source, :has_to_be_payment_source_class) | ||
end | ||
end | ||
|
||
def validate_payment_source_ownership | ||
return unless payment_source.present? | ||
|
||
if payment_source.respond_to?(:user_id) && | ||
payment_source.user_id != user_id | ||
errors.add(:payment_source, :not_owned_by_user) | ||
end | ||
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 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