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

delete AuthorizationService class to reduce indirections

Christophe Bliardd4e3bfa424bd
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 5606ab455e1..08a6b729b12 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -228,7 +228,7 @@ class ApplicationController < ActionController::Base
# Authorize the user for the requested action
def authorize(ctrl = params[:controller], action = params[:action], global = false)
context = @project || @projects
- is_authorized = AuthorizationService.new({ controller: ctrl, action: action }, context: context, global: global).call
+ is_authorized = User.current.allowed_to?({ controller: ctrl, action: }, context, global:)
unless is_authorized
if @project&.archived?
diff --git a/app/models/user.rb b/app/models/user.rb
index 5772aa38e48..50bdf4b80ac 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -164,7 +164,7 @@ class User < Principal
def reload(*args)
@name = nil
@projects_by_role = nil
- @authorization_service = nil
+ @user_allowed_service = nil
@project_role_cache = nil
super
@@ -519,7 +519,7 @@ class User < Principal
end
def allowed_to?(action, context, global: false)
- authorization_service.call(action, context, global:).result
+ user_allowed_service.call(action, context, global:).result
end
def allowed_to_in_project?(action, project)
@@ -530,7 +530,7 @@ class User < Principal
allowed_to?(action, nil, global: true)
end
- delegate :preload_projects_allowed_to, to: :authorization_service
+ delegate :preload_projects_allowed_to, to: :user_allowed_service
def reported_work_package_count
WorkPackage.on_active_project.with_author(self).visible.count
@@ -648,8 +648,8 @@ class User < Principal
[skip_suffix_check, regexp]
end
- def authorization_service
- @authorization_service ||= ::Authorization::UserAllowedService.new(self, role_cache: project_role_cache)
+ def user_allowed_service
+ @user_allowed_service ||= ::Authorization::UserAllowedService.new(self, role_cache: project_role_cache)
end
def project_role_cache
diff --git a/app/services/authorization_service.rb b/app/services/authorization_service.rb
deleted file mode 100644
index 3e016c642ea..00000000000
--- a/app/services/authorization_service.rb
+++ /dev/null
@@ -1,49 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2022 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.
-#++
-
-# project, projects, global, user = nil
-
-class AuthorizationService
- # @params
- # ctrl - controller
- # action - action
- # @named params
- # context - single project or array of projects - default nil
- # global - global - default false
- # user - user - default current user
- def initialize(permission, context: nil, global: false, user: User.current)
- @permission = permission
- @context = context
- @global = global
- @user = user
- end
-
- def call
- @user.allowed_to?(@permission, @context, global: @global)
- end
-end
diff --git a/lib/api/root_api.rb b/lib/api/root_api.rb
index 6a87e1706e9..19dcd837ba2 100644
--- a/lib/api/root_api.rb
+++ b/lib/api/root_api.rb
@@ -136,11 +136,7 @@ module API
#
# @raise [API::Errors::Unauthorized] when permission is not met
def authorize(permission, context: nil, global: false, user: current_user, &block)
- auth_service = AuthorizationService.new(permission,
- context:,
- global:,
- user:)
-
+ auth_service = -> { user.allowed_to?(permission, context, global:) }
authorize_by_with_raise auth_service, &block
end