Skip to content

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

extend functionality to prepend_before_action and append_before_action

ulferts7ba1c9a52242
diff --git a/app/controllers/concerns/accounts/authorization.rb b/app/controllers/concerns/accounts/authorization.rb
index 7c7ffddabb4..e4d12dcb353 100644
--- a/app/controllers/concerns/accounts/authorization.rb
+++ b/app/controllers/concerns/accounts/authorization.rb
@@ -124,14 +124,30 @@ module Accounts::Authorization
class_methods do
# Overriding before_action of rails to check if any authorization method is by now defined.
def before_action(*names, &)
- if METHODS_ENFORCING_AUTHORIZATION.intersect?(names)
- no_authorization_required!(only: names.last.is_a?(Hash) ? Array(names.last[:only]) : [],
- except: names.last.is_a?(Hash) ? Array(names.last[:except]) : [])
- end
+ set_authorization_checked_if_covered(*names)
+
+ super
+ end
+
+ def prepend_before_action(*names, &)
+ set_authorization_checked_if_covered(*names)
+
+ super
+ end
+
+ def append_before_action(*names, &)
+ set_authorization_checked_if_covered(*names)
super
end
+ def set_authorization_checked_if_covered(*names)
+ return unless METHODS_ENFORCING_AUTHORIZATION.intersect?(names)
+
+ authorization_checked!(only: names.last.is_a?(Hash) ? Array(names.last[:only]) : [],
+ except: names.last.is_a?(Hash) ? Array(names.last[:except]) : [])
+ end
+
def no_authorization_required!(only: [], except: [])
only = Array(only)
except = Array(except)
diff --git a/spec/controllers/concerns/authorization_spec.rb b/spec/controllers/concerns/authorization_spec.rb
index 97ce57187a9..61c110c90f4 100644
--- a/spec/controllers/concerns/authorization_spec.rb
+++ b/spec/controllers/concerns/authorization_spec.rb
@@ -120,6 +120,26 @@ RSpec.describe ApplicationController, "enforcement of authorization" do # ruboco
it_behaves_like "succeeds"
end
+ context "with authorization checked via prepend_before_action" do
+ controller do
+ prepend_before_action :authorize
+
+ include controller_setup
+ end
+
+ it_behaves_like "succeeds"
+ end
+
+ context "with authorization checked via append_before_action" do
+ controller do
+ append_before_action :authorize
+
+ include controller_setup
+ end
+
+ it_behaves_like "succeeds"
+ end
+
context "with another before action specified" do
controller do
before_action :other_before_action