Skip to content

Commit

Permalink
prevent users from different organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed May 24, 2024
1 parent a32bf0a commit 4c26b66
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/models/decidim/action_delegator/participant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Participant < ApplicationRecord
validates :email, uniqueness: { scope: :setting }, if: -> { email.present? }
validates :phone, uniqueness: { scope: :setting }, if: -> { phone.present? }

validate :user_belongs_to_organization

# sets the decidim user if found
before_save :set_decidim_user

Expand All @@ -36,7 +38,7 @@ def user
end

def user_from_metadata
@user_from_metadata ||= if setting.email_required?
@user_from_metadata ||= if setting&.email_required?
Decidim::User.find_by(email: email, organization: setting.organization)
else
Decidim::Authorization.find_by(unique_id: uniq_ids)&.user
Expand Down Expand Up @@ -96,6 +98,13 @@ def voted?
def set_decidim_user
self.decidim_user = user_from_metadata if decidim_user.blank?
end

def user_belongs_to_organization
return unless decidim_user && setting && setting.consultation
return if decidim_user.organization == organization

errors.add(:decidim_user, :invalid)
end
end
end
end
6 changes: 6 additions & 0 deletions spec/models/decidim/action_delegator/participant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ module ActionDelegator
it { is_expected.to be_valid }
it { is_expected.to belong_to(:setting) }

context "when user belongs to a different organization" do
let(:decidim_user) { create(:user) }

it { is_expected.not_to be_valid }
end

it "belong_to a ponderation" do
expect(subject.ponderation).to eq(ponderation)
end
Expand Down

0 comments on commit 4c26b66

Please sign in to comment.