roadmap icon indicating copy to clipboard operation
roadmap copied to clipboard

Why does method `Org#org_admin_plans` include plans with same `org_id`?

Open nicolasfranck opened this issue 2 years ago • 1 comments

Please complete the following fields as applicable:

What version of the DMPRoadmap code are you running? (e.g. v2.2.0)

4.1.0

Behaviour:

I see that method Org#org_admin_plans not only lists plans with owners and coowners of that same organization, but also plans that happen to have the attribute org_id set to that organization:

combined_plan_ids = (native_plan_ids + affiliated_plan_ids).flatten.uniq

where native_plan_ids is equal to SELECT * FROM plans where org_id = ?

In PlansController#create the plan.org_id is often set to organization of the logged in user. So in that case the organization is equal to the organization of the owner. No problem here

But on this line

if plan_params[:org].present? && plan_params[:org][:id].present?
        attrs = plan_params[:org]
        attrs[:org_id] = attrs[:id]
        @plan.org = org_from_params(params_in: attrs, allow_create: false)

.. plan.org_id is set to the organization of the selected primary research organization, as I understand.

I see that this method is used on these pages:

  • /org_admin/plans
  • /api/v0/plans

Is this intended? I would expect that the org admin would only receive a list of plans that he/she is affiliated with.

Access policy is different though, and correct in my opinion

nicolasfranck avatar Sep 29 '23 09:09 nicolasfranck

I think this issue relates to #3345.

  # app/models/plan.rb
  # Retrieves any plan organisationally or publicly visible for a given org id
  scope :organisationally_or_publicly_visible, lambda { |user|
    plan_ids = user.org.org_admin_plans.where(complete: true).pluck(:id).uniq
    includes(:template, roles: :user)
      .where(id: plan_ids, visibility: [
               visibilities[:organisationally_visible],
               visibilities[:publicly_visible]
             ])
      .where(
        'NOT EXISTS (SELECT 1 FROM roles WHERE plan_id = plans.id AND user_id = ?)',
        user.id
      )
  }

The above scope is used to populate the "Organizational plans" section on the /plans page. However, I think that unwanted plans may be listed here due to the inclusion of native_plan_ids.

aaronskiba avatar Apr 23 '24 19:04 aaronskiba