Skip to content

Commit 4ac02d2

Browse files
author
John Pinto
committed
Issue #3561 - Fix for bug "Discrepancy in Org Admin view of the
Organisation Plans and the related CSV Download". Changes: - where(Role.creator_condition) condition added to Plans retrieved in the Org model's org_admin_plans method. This ensures that Plans returned are only active plans. Note: if an owner or co-owner de-activates a plan the Role of the user is set to active: false, so any plan with creator with role (having active: false) would mean the Plan is removed. - To avoid duplication we removed .where(Role.creator_condition) in org_admin method in app/controllers/paginable/plans_controller.rb from line plans = plans.joins(:template, roles: [user::org]).where(Role.creator_condition). - Updated RSpec tests for Org method org_admin_plans() in spec/models/org_spec.rb. - Update CHANGELOG.
1 parent bfbb518 commit 4ac02d2

File tree

5 files changed

+93
-29
lines changed

5 files changed

+93
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changelog
22

3+
- Fix for a discrepancy in Org Admin view of the Organisation Plans and the related CSV Download. [#3561] (https://github.com/DMPRoadmap/roadmap/issues/3561)
4+
35
## v5.0.1
46
- Updated seeds.rb file for identifier_schemes to include context value and removed logo_url and idenitifier_prefix for Shibboleth (as it was causing issues with SSO). [#3525](https://github.com/DMPRoadmap/roadmap/pull/3525)
57
- Adjustments to style of select tags and plan download layout [#3509](https://github.com/DMPRoadmap/roadmap/pull/3509)

app/controllers/paginable/plans_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def org_admin
4646
@super_admin = current_user.can_super_admin?
4747
@clicked_through = params[:click_through].present?
4848
plans = @super_admin ? Plan.all : current_user.org.org_admin_plans
49-
plans = plans.joins(:template, roles: [user: :org]).where(Role.creator_condition)
49+
plans = plans.joins(:template, roles: [user: :org])
5050

5151
paginable_renderise(
5252
partial: 'org_admin',

app/models/org.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,13 @@ def org_admin_plans
292292
if Rails.configuration.x.plans.org_admins_read_all
293293
Plan.includes(:template, :phases, :roles, :users).where(id: combined_plan_ids)
294294
.where(roles: { active: true })
295+
.where(Role.creator_condition)
295296
else
296297
Plan.includes(:template, :phases, :roles, :users).where(id: combined_plan_ids)
297298
.where.not(visibility: Plan.visibilities[:privately_visible])
298299
.where.not(visibility: Plan.visibilities[:is_test])
299300
.where(roles: { active: true })
301+
.where(Role.creator_condition)
300302
end
301303
end
302304
# rubocop:enable Metrics/AbcSize

spec/factories/plans.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,26 @@
6666
obj.roles << create(:role, :creator, user: create(:user, org: create(:org)))
6767
end
6868
end
69+
trait :administrator do
70+
after(:create) do |obj|
71+
obj.roles << create(:role, :administrator, user: create(:user, org: create(:org)))
72+
end
73+
end
74+
trait :editor do
75+
after(:create) do |obj|
76+
obj.roles << create(:role, :editor, user: create(:user, org: create(:org)))
77+
end
78+
end
6979
trait :commenter do
7080
after(:create) do |obj|
7181
obj.roles << create(:role, :commenter, user: create(:user, org: create(:org)))
7282
end
7383
end
84+
trait :reviewer do
85+
after(:create) do |obj|
86+
obj.roles << create(:role, :reviewer, user: create(:user, org: create(:org)))
87+
end
88+
end
7489
trait :organisationally_visible do
7590
after(:create) do |plan|
7691
plan.update(visibility: Plan.visibilities[:organisationally_visible])

spec/models/org_spec.rb

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -344,79 +344,124 @@
344344

345345
describe '#org_admin_plans' do
346346
Rails.configuration.x.plans.org_admins_read_all = true
347-
let!(:org) { create(:org) }
348-
let!(:plan) { create(:plan, org: org, visibility: 'publicly_visible') }
349-
let!(:user) { create(:user, org: org) }
347+
# Two Orgs
348+
let!(:org1) { create(:org) }
349+
let!(:org2) { create(:org) }
350+
351+
# Plans for org1
352+
let!(:org1plan1) { create(:plan, :creator, :organisationally_visible, org: org1) }
353+
let!(:org1plan2) { create(:plan, :creator, :privately_visible, org: org1) }
354+
let!(:org1plan3) { create(:plan, :creator, :publicly_visible, org: org1) }
355+
let!(:org1plan4) { create(:plan, :creator, :organisationally_visible, org: org1) }
350356

351-
subject { org.org_admin_plans }
357+
# Plans for org2
358+
let!(:org2plan1) { create(:plan, :creator, :organisationally_visible, org: org2) }
359+
360+
subject { org1.org_admin_plans }
352361

353362
context 'when user belongs to Org and plan owner with role :creator' do
354363
before do
355-
create(:role, :creator, user: user, plan: plan)
356-
plan.add_user!(user.id, :creator)
364+
Rails.configuration.x.plans.org_admins_read_all = true
357365
end
366+
it { is_expected.to include(org1plan1, org1plan2, org1plan3, org1plan4) }
367+
it { is_expected.not_to include(org2plan1) }
368+
end
358369

359-
it { is_expected.to include(plan) }
370+
context 'when user belongs to Org and a plan removed by creator assuming there are no coowners' do
371+
before do
372+
Rails.configuration.x.plans.org_admins_read_all = true
373+
org1plan4.roles.map { |r| r.update(active: false) if r.user_id == org1plan4.owner.id }
374+
end
375+
376+
it { is_expected.to include(org1plan1, org1plan2, org1plan3) }
377+
it { is_expected.not_to include(org1plan4) }
360378
end
361379

362-
context 'when user belongs to Org and plan user with role :administrator' do
380+
context 'when user belongs to Org and a plan removed by creator, but cowner still active.' do
363381
before do
364-
plan.add_user!(user.id, :administrator)
382+
Rails.configuration.x.plans.org_admins_read_all = true
383+
coowner = create(:user, org: org1)
384+
org1plan4.add_user!(coowner.id, :coowner)
385+
owner_id = org1plan4.owner.id
386+
org1plan4.roles.map { |r| r.update(active: false) if r.user_id == owner_id }
387+
end
388+
389+
it { is_expected.to include(org1plan1, org1plan2, org1plan3) }
390+
it { is_expected.not_to include(org1plan4) }
391+
end
392+
393+
context 'when user belongs to Org, plan user with role :administrator, but plan creator from a different Org' do
394+
before do
395+
Rails.configuration.x.plans.org_admins_read_all = true
396+
# Creator belongs to different org
397+
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
398+
@plan.add_user!(create(:user, org: org1).id, :administrator)
365399
end
366400

367401
it {
368-
is_expected.to include(plan)
402+
is_expected.to include(org1plan1, org1plan2, org1plan3, org1plan4, @plan)
369403
}
370404
end
371405

372406
context 'user belongs to Org and plan user with role :editor, but not :creator and :admin' do
373407
before do
374-
plan.add_user!(user.id, :editor)
408+
Rails.configuration.x.plans.org_admins_read_all = true
409+
# Creator and admin belongs to different orgs
410+
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
411+
@plan.add_user!(create(:org).id, :administrator)
412+
# Editor belongs to org1
413+
@plan.add_user!(create(:user, org: org1).id, :editor)
375414
end
376415

377-
it { is_expected.to include(plan) }
416+
it { is_expected.not_to include(@plan) }
378417
end
379418

380419
context 'user belongs to Org and plan user with role :commenter, but not :creator and :admin' do
381420
before do
382-
plan.add_user!(user.id, :commenter)
421+
Rails.configuration.x.plans.org_admins_read_all = true
422+
# Creator and admin belongs to different orgs
423+
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
424+
@plan.add_user!(create(:org).id, :administrator)
425+
# Commenter belongs to org1
426+
@plan.add_user!(create(:user, org: org1).id, :commentor)
383427
end
384428

385-
it { is_expected.to include(plan) }
429+
it { is_expected.not_to include(@plan) }
386430
end
387431

388432
context 'user belongs to Org and plan user with role :reviewer, but not :creator and :admin' do
389433
before do
390-
plan.add_user!(user.id, :reviewer)
434+
Rails.configuration.x.plans.org_admins_read_all = true
435+
# Creator and admin belongs to different orgs
436+
@plan = create(:plan, :creator, :organisationally_visible, org: create(:org))
437+
@plan.add_user!(create(:org).id, :administrator)
438+
# Reviewer belongs to org1
439+
@plan.add_user!(create(:user, org: org1).id, :reviewer)
391440
end
392441

393-
it { is_expected.to include(plan) }
442+
it { is_expected.not_to include(@plan) }
394443
end
395444

396445
context 'read_all is false, visibility private and user org_admin' do
397446
before do
398447
Rails.configuration.x.plans.org_admins_read_all = false
399-
@perm = build(:perm)
400-
@perm.name = 'grant_permissions'
401-
user.perms << @perm
402-
plan.add_user!(user.id, :reviewer)
403-
plan.privately_visible!
448+
@user = create(:user, :org_admin, org: org1)
449+
@plan = create(:plan, :creator, :privately_visible, org: org1)
450+
@plan.add_user!(@user.id, :reviewer)
404451
end
405452

406-
it { is_expected.not_to include(plan) }
453+
it { is_expected.not_to include(@plan) }
407454
end
408455

409456
context 'read_all is false, visibility public and user org_admin' do
410457
before do
411458
Rails.configuration.x.plans.org_admins_read_all = false
412-
@perm = build(:perm)
413-
@perm.name = 'grant_permissions'
414-
user.perms << @perm
415-
plan.add_user!(user.id, :reviewer)
416-
plan.publicly_visible!
459+
@user = create(:user, :org_admin, org: org1)
460+
@plan = create(:plan, :creator, :publicly_visible, org: org1)
461+
@plan.add_user!(@user.id, :reviewer)
417462
end
418463

419-
it { is_expected.to include(plan) }
464+
it { is_expected.to include(@plan) }
420465
end
421466
end
422467

0 commit comments

Comments
 (0)