Skip to content

Commit

Permalink
Ensure friendship takes precendence over other permissions.
Browse files Browse the repository at this point in the history
If a profile has marked another profile as "friendly", then they want
to befriend that profile, and we should prioritize that over any other
permission constraints they've placed such as confirmed emails, etc.
With the exception of myself - where friends are explicitly excluded.
  • Loading branch information
ChaelCodes committed Jun 6, 2024
1 parent 636abdf commit 89ace3c
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
5 changes: 4 additions & 1 deletion app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class Profile < ApplicationRecord

scope :with_authenticated, -> { where(visibility: %i[everyone authenticated]) }
scope :nonblocked, ->(profile) { where.not(id: Friendship.blocks(profile).select(:buddy_id)) }
scope :befriended, ->(profile) { where(id: Friendship.friends_of(profile).select(:buddy_id), visibility: :friends) }
scope :befriended, lambda { |profile|
where(id: Friendship.friends_of(profile).select(:buddy_id))
.where.not(visibility: :myself)
}

# Relationships
belongs_to :user
Expand Down
7 changes: 4 additions & 3 deletions app/policies/profile_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def index?
# Whether the user can view the Profile's Handle, Bio, and Avatar.
def show?
return true if mine? || admin? || profile.visible_to_everyone?
confirmed_user?
confirmed_user? || profile.friends_with?(current_profile)
end

# This method controls whether a user can view a profile's details.
Expand All @@ -29,8 +29,9 @@ def show?
# * Myself - only the user can view - NOT EVEN EXISTING FRIENDS!
def show_details?
return true if mine? || admin? || profile.visible_to_everyone?
return confirmed_user? if profile.visible_to_authenticated?
profile.friends_with? current_profile if profile.visible_to_friends?
return false if profile.visible_to_myself?
return true if profile.friends_with? current_profile
confirmed_user? if profile.visible_to_authenticated?
end

def create?
Expand Down
31 changes: 31 additions & 0 deletions spec/models/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,35 @@
it { is_expected.to be_truthy }
end
end

describe "#befriended" do
subject { described_class.befriended(profile) }

context "with 'authenticated' visbility friend" do
let(:authenticated_profile) { create :profile, :authenticated }
let!(:friendship) { create :friendship, buddy: authenticated_profile, friend: profile, status: :accepted }

it "does include" do
expect(subject).to include authenticated_profile
end
end

context "with 'friends' visbility friend" do
let(:friends_profile) { create :profile, :friends }
let!(:friendship) { create :friendship, buddy: friends_profile, friend: profile, status: :accepted }

it "does include" do
expect(subject).to include friends_profile
end
end

context "with 'myself' visibility friend" do
let(:myself_profile) { create :profile, :myself }
let!(:friendship) { create :friendship, buddy: myself_profile, friend: profile, status: :accepted }

it "does not include" do
expect(subject).not_to include myself_profile
end
end
end
end
74 changes: 74 additions & 0 deletions spec/policies/profile_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,67 @@
end
end

permissions :show? do
context "with viewing everyone profile" do
let(:profile) { build :profile, visibility: :everyone }

context "with no user" do
it { expect(described_class).to permit(nil, profile) }
end

context "with unconfirmed user" do
let(:user) { create :user, :unconfirmed }

it { expect(described_class).to permit(user, profile) }
end

context "with confirmed user" do
let(:user) { create :user }

it { expect(described_class).to permit(user, profile) }
end

context "with admin" do
let(:user) { create :user, :admin }

it { expect(described_class).to permit(user, profile) }
end
end

context "when viewing authenticated profile" do
let(:profile) { build :profile, visibility: :authenticated }

context "with no user" do
it { expect(described_class).not_to permit(nil, profile) }
end

context "with unconfirmed user" do
let(:user) { create :user, :unconfirmed }

it { expect(described_class).not_to permit(user, profile) }

context "when friends" do
let!(:user_profile) { create :profile, user: }
let!(:friendship) { create :friendship, buddy: profile, friend: user.profile, status: :accepted }

it { expect(described_class).to permit(user, profile) }
end
end

context "with confirmed user" do
let(:user) { create :user }

it { expect(described_class).to permit(user, profile) }
end

context "with admin" do
let(:user) { create :user, :admin }

it { expect(described_class).to permit(user, profile) }
end
end
end

permissions :show_details? do
context "with viewing everyone profile" do
let(:profile) { build :profile, visibility: :everyone }
Expand Down Expand Up @@ -61,6 +122,13 @@
let(:user) { create :user, :unconfirmed }

it { expect(described_class).not_to permit(user, profile) }

context "when friends" do
let!(:user_profile) { create :profile, user: }
let!(:friendship) { create :friendship, buddy: profile, friend: user.profile, status: :accepted }

it { expect(described_class).to permit(user, profile) }
end
end

context "with confirmed user" do
Expand Down Expand Up @@ -193,6 +261,12 @@
it { is_expected.to match_array %w[everyone myself] }
end

context "when authenticated profile is friendly with you" do
let!(:friendship) { create :friendship, buddy: authenticated, friend: current_profile, status: :accepted }

it { is_expected.to match_array %w[everyone authenticated myself] }
end

context "when you are blocked" do
let!(:blocked_by_friends) { create :friendship, buddy: friends, friend: current_profile, status: :blocked }
let!(:blocked_by_everyone) { create :friendship, buddy: everyone, friend: current_profile, status: :blocked }
Expand Down

0 comments on commit 89ace3c

Please sign in to comment.