Fix access restriction in valuation budget investments controller

Since we allow many active budgets at the same time, the
controller should now check the budget given by params.

Before this change the controller was checking the latest
published budget, ignoring the request parameter `budget_id`.
This commit is contained in:
Senén Rodero Rodríguez
2023-01-13 15:29:37 +01:00
parent 0c09fd22af
commit cdd26dd568
2 changed files with 13 additions and 2 deletions

View File

@@ -4,9 +4,9 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController
feature_flag :budgets feature_flag :budgets
before_action :load_budget
before_action :restrict_access_to_assigned_items, only: [:show, :edit, :valuate] before_action :restrict_access_to_assigned_items, only: [:show, :edit, :valuate]
before_action :restrict_access, only: [:edit, :valuate] before_action :restrict_access, only: [:edit, :valuate]
before_action :load_budget
before_action :load_investment, only: [:show, :edit, :valuate] before_action :load_investment, only: [:show, :edit, :valuate]
has_orders %w[oldest], only: [:show, :edit] has_orders %w[oldest], only: [:show, :edit]
@@ -110,7 +110,7 @@ class Valuation::BudgetInvestmentsController < Valuation::BaseController
end end
def restrict_access def restrict_access
unless current_user.administrator? || current_budget.valuating? unless current_user.administrator? || @budget.valuating?
raise CanCan::AccessDenied, I18n.t("valuation.budget_investments.not_in_valuating_phase") raise CanCan::AccessDenied, I18n.t("valuation.budget_investments.not_in_valuating_phase")
end end
end end

View File

@@ -501,6 +501,17 @@ describe "Valuation budget investments" do
expect(page).to have_content("Investments can only be valuated when Budget is in valuating phase") expect(page).to have_content("Investments can only be valuated when Budget is in valuating phase")
end end
scenario "restric access to the budget given by params when is not in valuating phase" do
budget.update!(phase: "publishing_prices")
create(:budget, :valuating)
investment = create(:budget_investment, budget: budget, valuators: [valuator])
login_as(valuator.user)
visit edit_valuation_budget_budget_investment_path(budget, investment)
expect(page).to have_content("Investments can only be valuated when Budget is in valuating phase")
end
scenario "visible to admins regardless of not being in valuating phase" do scenario "visible to admins regardless of not being in valuating phase" do
budget.update!(phase: "publishing_prices") budget.update!(phase: "publishing_prices")