Skip to content

Commit 84eb63f

Browse files
authored
modified the plan.readable_by? method and added wider test coverage (#2416)
1 parent bcea4fe commit 84eb63f

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

app/models/plan.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -344,19 +344,17 @@ def editable_by?(user_id)
344344
#
345345
# Returns Boolean
346346
def readable_by?(user_id)
347+
return true if commentable_by?(user_id)
347348
current_user = User.find(user_id)
348-
if current_user.present?
349-
# If the user is a super admin and the config allows for supers to view plans
350-
if current_user.can_super_admin? &&
351-
Branding.fetch(:service_configuration, :plans, :super_admins_read_all)
352-
true
353-
# If the user is an org admin and the config allows for org admins to view plans
354-
elsif current_user.can_org_admin? &&
355-
Branding.fetch(:service_configuration, :plans, :org_admins_read_all)
356-
owner_and_coowners.map(&:org_id).include?(current_user.org_id)
357-
else
358-
commentable_by?(user_id)
359-
end
349+
return false unless current_user.present?
350+
# If the user is a super admin and the config allows for supers to view plans
351+
if current_user.can_super_admin? &&
352+
Branding.fetch(:service_configuration, :plans, :super_admins_read_all)
353+
true
354+
# If the user is an org admin and the config allows for org admins to view plans
355+
elsif current_user.can_org_admin? &&
356+
Branding.fetch(:service_configuration, :plans, :org_admins_read_all)
357+
owner_and_coowners.map(&:org_id).include?(current_user.org_id)
360358
else
361359
false
362360
end
@@ -388,6 +386,7 @@ def administerable_by?(user_id)
388386
def reviewable_by?(user_id)
389387
reviewer = User.find(user_id)
390388
feedback_requested? &&
389+
reviewer.present? &&
391390
reviewer.org_id == owner.org_id &&
392391
reviewer.can_review_plans?
393392
end

spec/models/plan_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,36 @@
856856
end
857857
end
858858

859+
context "explicit sharing does not conflict with admin-viewing" do
860+
861+
it "super admins" do
862+
Branding.expects(:fetch)
863+
.with(:service_configuration, :plans, :super_admins_read_all)
864+
.at_most_once
865+
.returns(false)
866+
867+
user.perms << create(:perm, name: "add_organisations")
868+
role = subject.roles.commenter.first
869+
role.user_id = user.id
870+
role.save!
871+
872+
expect(subject.readable_by?(user.id)).to eql(true)
873+
end
874+
875+
it "org admins" do
876+
Branding.expects(:fetch)
877+
.with(:service_configuration, :plans, :org_admins_read_all)
878+
.at_most_once
879+
.returns(false)
880+
881+
user.perms << create(:perm, name: "modify_guidance")
882+
role = subject.roles.commenter.first
883+
role.user_id = user.id
884+
role.save!
885+
886+
expect(subject.readable_by?(user.id)).to eql(true)
887+
end
888+
end
859889
end
860890

861891
describe "#commentable_by?" do

0 commit comments

Comments
 (0)