OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Allow permission-based authorization
Oliver Güntherb3b4fac1e032
diff --git a/app/controllers/concerns/accounts/authorization.rb b/app/controllers/concerns/accounts/authorization.rbindex 3d4227a43de..a45f130a49d 100644--- a/app/controllers/concerns/accounts/authorization.rb+++ b/app/controllers/concerns/accounts/authorization.rb@@ -85,7 +85,7 @@ module Accounts::Authorization# Action can be:# * a parameter-like Hash (eg. { controller: '/projects', action: 'edit' })# * a permission Symbol (eg. :edit_project)- def do_authorize(action, global: false) # rubocop:disable Metrics/PerceivedComplexity+ def do_authorize(action, global: false)is_authorized = if globalUser.current.allowed_based_on_permission_context?(action)else@@ -196,5 +196,27 @@ module Accounts::Authorizationgenerally_allowed: authorization_ensured[:generally_allowed],controller: self }end++ # Authorize on the given permission+ def authorize_with_permission(permission, global: false, **args)+ authorization_checked_by_default_action(**args.slice(:only, :except))++ before_action(**args) do+ do_authorize(permission, global:)+ end+ end++ # Find a project based on params[:project_id]+ # and authorize on a given permission+ def load_and_authorize_with_permission_in_optional_project(permission, **args)+ authorization_checked_by_default_action(**args.slice(:only, :except))++ before_action(**args) do+ @project = Project.find(params[:project_id]) if params[:project_id].present?+ do_authorize(permission, global: params[:project_id].blank?)+ rescue ActiveRecord::RecordNotFound+ render_404+ end+ endendenddiff --git a/spec/controllers/concerns/authorization_spec.rb b/spec/controllers/concerns/authorization_spec.rbindex a0ffc7cb622..dcf42dde898 100644--- a/spec/controllers/concerns/authorization_spec.rb+++ b/spec/controllers/concerns/authorization_spec.rb@@ -54,18 +54,18 @@ RSpec.describe ApplicationController, "enforcement of authorization" do # rubococurrent_user { user }- shared_examples "succeeds" do+ shared_examples "succeeds" do |action_name = :index|it "succeeds" do- get :index+ get action_nameexpect(response).to have_http_status :okendend- shared_examples "is prevented" do+ shared_examples "is prevented" do |action_name = :index|it "fails with a RuntimeError" do- expect { get :index }+ expect { get action_name }.to raise_error RuntimeErrorendend@@ -108,6 +108,123 @@ RSpec.describe ApplicationController, "enforcement of authorization" do # rubocoit_behaves_like "succeeds"end+ context "with authorization checked with authorize_with_permission" do+ controller do+ include Accounts::Authorization+ include controller_setup++ authorize_with_permission :view_project++ def do_authorize(_permission, global: false)+ true+ end+ end++ it_behaves_like "succeeds"++ it "calls the authorization method" do+ allow(controller).to receive(:do_authorize)++ get :index++ expect(controller).to have_received(:do_authorize).with(:view_project, global: false)+ end+ end++ context "with authorization checked with authorize_with_permission on a single action" do+ controller do+ include Accounts::Authorization+ include controller_setup++ authorize_with_permission :view_project, only: %i[test]++ def test+ render plain: "OK"+ end++ def do_authorize(_permission, global: true)+ true+ end+ end++ before do+ @routes.draw do # rubocop:disable RSpec/InstanceVariable+ get "/anonymous/index"+ get "/anonymous/test"+ end+ end++ it "allows calling test" do+ allow(controller).to receive(:do_authorize)++ get :test++ expect(controller).to have_received(:do_authorize).with(:view_project, global: false)+ end++ it_behaves_like "is prevented", :index+ end++ context "with authorization checked with load_and_authorize_with_permission_in_optional_project" do+ controller do+ include Accounts::Authorization+ include controller_setup++ load_and_authorize_with_permission_in_optional_project :view_project++ def do_authorize(_permission, global: true)+ true+ end+ end++ it "loads the project if present" do+ allow(controller).to receive(:do_authorize)+ allow(Project).to receive(:find)++ get :index, params: { project_id: "1" }++ expect(Project).to have_received(:find).with("1")+ expect(controller).to have_received(:do_authorize).with(:view_project, global: false)+ end++ it "uses the global call if not present" do+ allow(controller).to receive(:do_authorize)+ allow(Project).to receive(:find)++ get :index++ expect(Project).not_to have_received(:find)+ expect(controller).to have_received(:do_authorize).with(:view_project, global: true)+ end+ end++ context "with authorization checked on single action with load_and_authorize_with_permission_in_optional_project" do+ controller do+ include Accounts::Authorization+ include controller_setup++ load_and_authorize_with_permission_in_optional_project :view_project, only: %i[test]++ def test+ render plain: "OK"+ end++ def do_authorize(_permission, global: true)+ true+ end+ end++ before do+ @routes.draw do # rubocop:disable RSpec/InstanceVariable+ get "/anonymous/index"+ get "/anonymous/test"+ end+ end++ it_behaves_like "succeeds", :test+ it_behaves_like "is prevented", :index+ end+context "with authorization checked with load_and_authorize_in_optional_project" docontroller dobefore_action :load_and_authorize_in_optional_project