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

Add EnterpriseGuard to make it easier to add before_actions

Oliver Günther8c5d182d7bb7
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 6210f661029..c5e84856c4c 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -50,6 +50,7 @@ class ApplicationController < ActionController::Base
include Accounts::CurrentUser
include Accounts::UserLogin
include Accounts::Authorization
+ include Accounts::EnterpriseGuard
include ::OpenProject::Authentication::SessionExpiry
include AdditionalUrlHelpers
include OpenProjectErrorHelper
diff --git a/app/controllers/concerns/accounts/enterprise_guard.rb b/app/controllers/concerns/accounts/enterprise_guard.rb
new file mode 100644
index 00000000000..e7f4af7e8f9
--- /dev/null
+++ b/app/controllers/concerns/accounts/enterprise_guard.rb
@@ -0,0 +1,66 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+##
+# Intended to be used by the ApplicationController to provide authorization helpers
+module Accounts::EnterpriseGuard
+ extend ActiveSupport::Concern
+
+ class_methods do
+ ##
+ # Adds a before_action check to test enterprise status of the feature
+ # @param feature_key [String] the name of the enterprise feature to check
+ #
+ # If a block is passed, it will be executed if the feature is not available.
+ def guard_enterprise_feature(feature_key, **action_args, &)
+ before_action(**action_args) do
+ perform_enterprise_feature_guard(feature_key, &)
+ end
+ end
+ end
+
+ private
+
+ ##
+ # Checks if the current action is covered by any authorization method.
+ # @param feature_key [String] the name of the enterprise feature to check
+ # If a block is passed, it will be executed if the feature is not available.
+ def perform_enterprise_feature_guard(feature_key, &)
+ return if EnterpriseToken.allows_to?(feature_key)
+
+ plan = OpenProject::Token.lowest_plan_for(feature_key)
+ if block_given?
+ flash[:error] = I18n.t("error_enterprise_plan_needed", plan:)
+ instance_eval(&)
+ else
+ render_403 message: I18n.t("error_enterprise_plan_needed", plan:)
+ end
+ end
+end
diff --git a/app/controllers/custom_actions_controller.rb b/app/controllers/custom_actions_controller.rb
index e94f1266519..a74b257c1d4 100644
--- a/app/controllers/custom_actions_controller.rb
+++ b/app/controllers/custom_actions_controller.rb
@@ -28,7 +28,10 @@
class CustomActionsController < ApplicationController
before_action :require_admin
- before_action :require_enterprise_token, except: %i[index]
+
+ guard_enterprise_feature(:custom_actions, only: %i[new create edit update]) do
+ redirect_to action: :index
+ end
self._model_object = CustomAction
before_action :find_model_object, only: %i(edit update destroy)
@@ -81,12 +84,6 @@ class CustomActionsController < ApplicationController
}
end
- def require_enterprise_token
- return if EnterpriseToken.allows_to?(:custom_actions)
-
- render_403
- end
-
# If no action/condition is set in the view, the
# actions/conditions already existing on a custom action should be removed.
# But because it is not feasible to have an empty and hidden hash object in a form
diff --git a/app/views/custom_actions/index.html.erb b/app/views/custom_actions/index.html.erb
index 856c888f218..5f2b58cfaa5 100644
--- a/app/views/custom_actions/index.html.erb
+++ b/app/views/custom_actions/index.html.erb
@@ -14,16 +14,17 @@
<%=
render(Primer::OpenProject::SubHeader.new) do |subheader|
- subheader.with_action_button(
- scheme: :primary,
- leading_icon: :plus,
- label: t("custom_actions.new"),
- test_selector: "op-admin-custom-actions--button-new",
- tag: :a,
- disabled: !available,
- href: new_custom_action_path
- ) do
- CustomAction.model_name.human
+ if available
+ subheader.with_action_button(
+ scheme: :primary,
+ leading_icon: :plus,
+ label: t("custom_actions.new"),
+ test_selector: "op-admin-custom-actions--button-new",
+ tag: :a,
+ href: new_custom_action_path
+ ) do
+ CustomAction.model_name.human
+ end
end
end
%>
@@ -36,4 +37,6 @@
)
%>
-<%= render(::CustomActions::TableComponent.new(rows: @custom_actions)) %>
+<% if available %>
+ <%= render(::CustomActions::TableComponent.new(rows: @custom_actions)) %>
+<% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index d844073cfa6..cccd43847db 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -2201,6 +2201,7 @@ en:
error_color_could_not_be_saved: "Color could not be saved"
error_cookie_missing: "The OpenProject cookie is missing. Please ensure that cookies are enabled, as this application will not properly function without."
error_custom_option_not_found: "Option does not exist."
+ error_enterprise_plan_needed: "You need the %{plan} enterprise plan to perform this action."
error_enterprise_activation_user_limit: "Your account could not be activated (user limit reached). Please contact your administrator to gain access."
error_enterprise_token_invalid_domain: "The Enterprise edition is not active. Your Enterprise token's domain (%{actual}) does not match the system's host name (%{expected})."
error_failed_to_delete_entry: "Failed to delete this entry."
diff --git a/spec/controllers/concerns/accounts/enterprise_guard_spec.rb b/spec/controllers/concerns/accounts/enterprise_guard_spec.rb
new file mode 100644
index 00000000000..cc453121883
--- /dev/null
+++ b/spec/controllers/concerns/accounts/enterprise_guard_spec.rb
@@ -0,0 +1,342 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+require "spec_helper"
+
+RSpec.describe ApplicationController, "enforcement of enterprise feature guards" do # rubocop:disable RSpec/SpecFilePathFormat
+ shared_let(:user) { create(:user) }
+ let(:feature_key) { "some_enterprise_feature" }
+ let(:plan) { "enterprise" }
+
+ controller_setup = Module.new do
+ extend ActiveSupport::Concern
+
+ included do
+ # These calls prevent the RuntimeError about authorization checks being required
+ # They tell the controller that authorization has been handled separately
+ # In real usage, the enterprise guard itself would be the authorization check
+ authorization_checked! :index
+ authorization_checked! :alternative_action
+
+ def index
+ render plain: "OK"
+ end
+
+ def alternative_action
+ render plain: "OK"
+ end
+
+ private
+
+ def other_before_action; end
+ end
+ end
+
+ current_user { user }
+
+ shared_examples "succeeds" do |action_name = :index|
+ it "succeeds" do
+ get action_name
+
+ expect(response)
+ .to have_http_status :ok
+ end
+ end
+
+ shared_examples "is blocked" do |action_name = :index|
+ it "returns 403 forbidden" do
+ get action_name
+
+ expect(response)
+ .to have_http_status :forbidden
+ end
+ end
+
+ shared_examples "redirects" do |action_name = :index|
+ it "redirects" do
+ get action_name
+
+ expect(response)
+ .to have_http_status :redirect
+ end
+ end
+
+ before do
+ allow(EnterpriseToken).to receive(:allows_to?).with(feature_key).and_return(false)
+ allow(OpenProject::Token).to receive(:lowest_plan_for).with(feature_key).and_return(plan)
+ end
+
+ context "with feature check using guard_enterprise_feature" do
+ controller do
+ include Accounts::EnterpriseGuard
+ include controller_setup
+
+ guard_enterprise_feature "some_enterprise_feature"
+ end
+
+ before do
+ @routes.draw do # rubocop:disable RSpec/InstanceVariable
+ get "/anonymous/index"
+ get "/anonymous/alternative_action"
+ end
+ end
+
+ it_behaves_like "is blocked"
+
+ context "when enterprise token allows the feature" do
+ before do
+ allow(EnterpriseToken).to receive(:allows_to?).with(feature_key).and_return(true)
+ end
+
+ it_behaves_like "succeeds"
+ end
+ end
+
+ context "with feature check using guard_enterprise_feature on a single action" do
+ controller do
+ include Accounts::EnterpriseGuard
+ include controller_setup
+
+ guard_enterprise_feature "some_enterprise_feature", only: %i[index]
+
+ # Mark alternative_action as having authorization checked separately
+ # since it's not guarded by enterprise_feature
+ authorization_checked! :alternative_action
+ end
+
+ before do
+ @routes.draw do # rubocop:disable RSpec/InstanceVariable
+ get "/anonymous/index"
+ get "/anonymous/alternative_action"
+ end
+ end
+
+ it_behaves_like "is blocked"
+
+ it "allows other actions" do
+ get :alternative_action
+
+ expect(response)
+ .to have_http_status :ok
+ end
+ end
+
+ context "with feature check using guard_enterprise_feature except for an action" do
+ controller do
+ include Accounts::EnterpriseGuard
+ include controller_setup
+
+ guard_enterprise_feature "some_enterprise_feature", except: %i[alternative_action]
+
+ # Mark alternative_action as having authorization checked separately
+ # since it's not guarded by enterprise_feature
+ authorization_checked! :alternative_action
+ end
+
+ before do
+ @routes.draw do # rubocop:disable RSpec/InstanceVariable
+ get "/anonymous/index"
+ get "/anonymous/alternative_action"
+ end
+ end
+
+ it_behaves_like "is blocked"
+
+ it "allows the excepted action" do
+ get :alternative_action
+
+ expect(response)
+ .to have_http_status :ok
+ end
+ end
+
+ context "with feature check using guard_enterprise_feature with a custom block" do
+ controller do
+ include Accounts::EnterpriseGuard
+ include controller_setup
+
+ guard_enterprise_feature "some_enterprise_feature" do
+ redirect_to "/custom/path"
+ end
+
+ # No need for explicit authorization_checked! calls here since guard_enterprise_feature
+ # should implicitly mark actions as authorized
+ end
+
+ before do
+ @routes.draw do # rubocop:disable RSpec/InstanceVariable
+ get "/anonymous/index"
+ get "/anonymous/alternative_action"
+ end
+ end
+
+ it_behaves_like "redirects"
+
+ it "sets a flash error message" do
+ get :index
+
+ expect(flash[:error]).to be_present
+ expect(flash[:error]).to include(plan)
+ end
+ end
+
+ context "with feature check in the superclass" do
+ controller(described_class) do
+ include Accounts::EnterpriseGuard
+ guard_enterprise_feature "some_enterprise_feature"
+ end
+
+ controller(controller_class) do
+ include controller_setup
+ end
+
+ before do
+ @routes.draw do # rubocop:disable RSpec/InstanceVariable
+ get "/anonymous/index"
+ get "/anonymous/alternative_action"
+ end
+ end
+
+ it_behaves_like "is blocked"
+
+ context "when enterprise token allows the feature" do
+ before do
+ allow(EnterpriseToken).to receive(:allows_to?).with(feature_key).and_return(true)
+ end
+
+ it_behaves_like "succeeds"
+ end
+ end
+
+ context "with feature check via prepend_before_action" do
+ controller do
+ include Accounts::EnterpriseGuard
+ include controller_setup
+
+ prepend_before_action { perform_enterprise_feature_guard("some_enterprise_feature") }
+
+ # Explicitly mark actions as having authorization checked
+ authorization_checked! :index
+ authorization_checked! :alternative_action
+ end
+
+ before do
+ @routes.draw do # rubocop:disable RSpec/InstanceVariable
+ get "/anonymous/index"
+ get "/anonymous/alternative_action"
+ end
+ end
+
+ it_behaves_like "is blocked"
+
+ context "when enterprise token allows the feature" do
+ before do
+ allow(EnterpriseToken).to receive(:allows_to?).with(feature_key).and_return(true)
+ end
+
+ it_behaves_like "succeeds"
+ end
+ end
+
+ context "with feature check via append_before_action" do
+ controller do
+ include Accounts::EnterpriseGuard
+ include controller_setup
+
+ append_before_action { perform_enterprise_feature_guard("some_enterprise_feature") }
+
+ # Explicitly mark actions as having authorization checked
+ authorization_checked! :index
+ authorization_checked! :alternative_action
+ end
+
+ before do
+ @routes.draw do # rubocop:disable RSpec/InstanceVariable
+ get "/anonymous/index"
+ get "/anonymous/alternative_action"
+ end
+ end
+
+ it_behaves_like "is blocked"
+
+ context "when enterprise token allows the feature" do
+ before do
+ allow(EnterpriseToken).to receive(:allows_to?).with(feature_key).and_return(true)
+ end
+
+ it_behaves_like "succeeds"
+ end
+ end
+
+ context "with multiple feature checks" do
+ controller do
+ include Accounts::EnterpriseGuard
+ include controller_setup
+
+ guard_enterprise_feature "some_enterprise_feature", only: %i[index]
+ guard_enterprise_feature "another_enterprise_feature", only: %i[alternative_action]
+ end
+
+ before do
+ allow(EnterpriseToken).to receive(:allows_to?).with("another_enterprise_feature").and_return(false)
+ allow(OpenProject::Token).to receive(:lowest_plan_for).with("another_enterprise_feature").and_return("premium")
+
+ @routes.draw do # rubocop:disable RSpec/InstanceVariable
+ get "/anonymous/index"
+ get "/anonymous/alternative_action"
+ end
+ end
+
+ it_behaves_like "is blocked"
+
+ it "blocks the alternative action with its own feature key" do
+ get :alternative_action
+
+ expect(response)
+ .to have_http_status :forbidden
+ end
+
+ context "when enterprise token allows both features" do
+ before do
+ allow(EnterpriseToken).to receive(:allows_to?).with(feature_key).and_return(true)
+ allow(EnterpriseToken).to receive(:allows_to?).with("another_enterprise_feature").and_return(true)
+ end
+
+ it_behaves_like "succeeds"
+
+ it "allows the alternative action" do
+ get :alternative_action
+
+ expect(response)
+ .to have_http_status :ok
+ end
+ end
+ end
+end