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
Remove old allowed_to methods
Klaus Zanders8bb37e1b7cea
diff --git a/app/models/users/permission_checks.rb b/app/models/users/permission_checks.rbindex bb8b4e0e2be..b65050da09e 100644--- a/app/models/users/permission_checks.rb+++ b/app/models/users/permission_checks.rb@@ -150,28 +150,8 @@ module Users::PermissionChecksendend- # Old allowed_to? interface. Marked as deprecated, should be removed at some point ... Guessing 14.0?- def allowed_to?(action, context, global: false)- OpenProject::Deprecation.deprecate_method(User, :allowed_to?)- user_allowed_service.call(action, context, global:)- end-- def allowed_to_in_project?(action, project)- OpenProject::Deprecation.replaced(:allowed_to_in_project?, :allowed_in_project?, caller)- allowed_to?(action, project)- end-- def allowed_to_globally?(action)- OpenProject::Deprecation.replaced(:allowed_to_globally?, :allowed_globally?, caller)- allowed_to?(action, nil, global: true)- end-private- def user_allowed_service- @user_allowed_service ||= ::Authorization::UserAllowedService.new(self, role_cache: project_role_cache)- end-def user_permissible_service@user_permissible_service ||= ::Authorization::UserPermissibleService.new(self)enddiff --git a/app/services/authorization/user_allowed_service.rb b/app/services/authorization/user_allowed_service.rbdeleted file mode 100644index 4da4874db7b..00000000000--- a/app/services/authorization/user_allowed_service.rb+++ /dev/null@@ -1,178 +0,0 @@-#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 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.-#++--class Authorization::UserAllowedService- attr_accessor :user-- def initialize(user, role_cache: Users::ProjectRoleCache.new(user))- self.user = user- self.project_role_cache = role_cache- end-- # Return true if the user is allowed to do the specified action on a specific context- # Action can be:- # * a parameter-like Hash (eg. { controller: '/projects', action: 'edit' })- # * a permission Symbol (eg. :edit_project)- # Context can be:- # * a project : returns true if user is allowed to do the specified action on this project- # * a group of projects : returns true if user is allowed on every project- # * an entity that a user can become a member of specifically (listed in Member::ALLOWED_ENTITIES) :- # * returns true if user is allowed to do the specified action on the given item or- # * returns ture if user is allowed to do the specified action on the project the entity belongs to- # * nil with +global+ set to +true+ : check if user has at least one role allowed for this action,- # or falls back to Non Member / Anonymous permissions depending if the user is logged- def call(action, context, global: false)- if supported_context?(context, global:)- allowed_to?(action, context, global:)- else- false- end- end-- def preload_projects_allowed_to(action)- project_authorization_cache.cache(action)- end-- private-- attr_accessor :project_role_cache-- def allowed_to?(action, context, global: false)- action = normalize_action(action)-- if context.nil? && global- allowed_to_globally?(action)- elsif context.is_a? Project- allowed_to_in_project?(action, context)- elsif supported_entity?(context)- allowed_to_in_entity?(action, context)- elsif context.respond_to?(:to_a)- allowed_to_in_all_projects?(action, context)- else- false- end- end-- def allowed_to_in_entity?(action, entity)- # Inactive users are never authorized- return false unless authorizable_user?-- # Short circuit: When the user is already allowed to execute the action baed- # on the project, there's no need to do a check on the entity- return true if entity.respond_to?(:project) && allowed_to_in_project?(action, entity.project)-- # Admin users are authorized for anything else- # unless the permission is explicitly flagged not to be granted to admins.- return true if granted_to_admin?(action)-- has_authorized_role?(action, entity)- end-- def allowed_to_in_project?(action, project)- return false if project.nil?-- if project_authorization_cache.cached?(action)- return project_authorization_cache.allowed?(action, project)- end-- # No action allowed on archived projects- return false unless project.active? || project.being_archived?- # No action allowed on disabled modules- return false unless project.allows_to?(action)- # Inactive users are never authorized- return false unless authorizable_user?- # Admin users are authorized for anything else- # unless the permission is explicitly flagged not to be granted to admins.- return true if granted_to_admin?(action)-- has_authorized_role?(action, project)- end-- # Authorize if user is authorized on every element of the array- def allowed_to_in_all_projects?(action, projects)- projects.present? && Array(projects).all? do |project|- allowed_to?(action, project)- end- end-- # Is the user allowed to do the specified action on any project?- # See allowed_to? for the action parameter description.- def allowed_to_globally?(action)- # Inactive users are never authorized- return false unless authorizable_user?- # Admin users are always authorized- return true if granted_to_admin?(action)-- has_authorized_role?(action)- end-- ##- # Only users that are not locked may be granted actions- # with the exception of a temporary-granted system user- def authorizable_user?- !user.locked? || user.is_a?(SystemUser)- end-- # Admin users are granted every permission unless the- # permission explicitly disables it.- def granted_to_admin?(action)- user.admin? && OpenProject::AccessControl.grant_to_admin?(action)- end-- def has_authorized_role?(action, context = nil)- project_role_cache- .fetch(context)- .any? do |role|- role.allowed_to?(action)- end- end-- def project_authorization_cache- @project_authorization_cache ||= Users::ProjectAuthorizationCache.new(user)- end-- def normalize_action(action)- if action.is_a?(Hash) && action[:controller] && action[:controller].to_s.starts_with?("/")- action = action.dup- action[:controller] = action[:controller][1..]- end-- action- end-- def supported_context?(context, global:)- (context.nil? && global) ||- context.is_a?(Project) ||- supported_entity?(context) ||- (!context.nil? && context.respond_to?(:to_a))- end-- def supported_entity?(entity)- Member.can_be_member_of?(entity)- end-enddiff --git a/spec/services/authorization/user_allowed_service_spec.rb b/spec/services/authorization/user_allowed_service_spec.rbdeleted file mode 100644index 850b2af7ae5..00000000000--- a/spec/services/authorization/user_allowed_service_spec.rb+++ /dev/null@@ -1,298 +0,0 @@-#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 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"--# TODO: Fix tests here--RSpec.describe Authorization::UserAllowedService do- let(:user) { build_stubbed(:user) }- let(:instance) { described_class.new(user) }- let(:action) { :an_action }- let(:action_hash) { { controller: "/controller", action: "action" } }- let(:project) { build_stubbed(:project) }- let(:other_project) { build_stubbed(:project) }- let(:role) { build_stubbed(:project_role) }- let(:user_roles_in_project) do- array = [role]- allow(array)- .to receive(:eager_load)- .and_return(array)-- array- end- let(:role_grants_action) { true }- let(:project_allows_to) { true }-- subject { instance.call(action, context) }-- describe "#initialize" do- it "has the user" do- expect(instance.user).to eql user- end- end-- shared_examples_for "allowed to checked" do- before do- Array(context).each do |project|- project.active = true-- allow(project).to receive(:allows_to?).with(action).and_return(project_allows_to)- allow(Authorization).to receive(:roles).with(user, project).and_return(user_roles_in_project)- end-- allow(role).to receive(:allowed_to?).with(action).and_return(role_grants_action)- end-- context "with the user having a granting role" do- it "is true" do- expect(subject).to be_truthy- end-- it "does not call the db twice for a project" do- Array(context).each do |project|- allow(Authorization).to receive(:roles).with(user, project).and_return(user_roles_in_project)- end-- subject- subject-- Array(context).each do |project|- expect(Authorization)- .to have_received(:roles)- .once- .with(user, project)- end- end-- context "but the user not being active" do- before do- user.lock- end-- it "returns false", :aggregate_failures do- expect(instance.call(action, nil, global: true)).not_to be_truthy- end- end- end-- context "with the user having a nongranting role" do- let(:role_grants_action) { false }-- it "is false" do- expect(subject).to be_falsey- end- end-- context 'with the user being admin- with the user not having a granting role' do- let(:user_roles_in_project) { [] }-- before do- user.admin = true- end-- it "is true" do- expect(subject).to be_truthy- end- end-- context "with the project not being active" do- before do- Array(context).each do |project|- project.active = false- project.clear_changes_information- end- end-- it "is false" do- expect(subject).to be_falsey- end-- it "is false even if the user is admin" do- user.admin = true-- expect(subject).to be_falsey- end- end-- context "with the project being archived" do- before do- Array(context).each do |project|- project.active = false- end- end-- it "is true" do- expect(subject).to be_truthy- end- end-- context "with the project not having the action enabled" do- let(:project_allows_to) { false }-- it "is false" do- expect(subject).to be_falsey- end-- it "is false even if the user is admin" do- user.admin = true-- expect(subject).to be_falsey- end- end-- context "with having precached the results" do- before do- auth_cache = double("auth_cache")-- allow(Users::ProjectAuthorizationCache)- .to receive(:new)- .and_return(auth_cache)-- allow(auth_cache)- .to receive(:cache)- .with(action)-- allow(auth_cache)- .to receive(:cached?)- .with(action)- .and_return(true)-- Array(context).each do |project|- allow(auth_cache)- .to receive(:allowed?)- .with(action, project)- .and_return(true)- end-- instance.preload_projects_allowed_to(action)- end-- it "is true" do- expect(subject).to be_truthy- end-- it "does not call the db" do- subject-- expect(Authorization)- .not_to have_received(:roles)- end- end- end-- describe "#call" do- context "for a project" do- let(:context) { project }-- it_behaves_like "allowed to checked"- end-- context "for an array of projects" do- let(:context) { [project, other_project] }-- it_behaves_like "allowed to checked"-- it "is false" do- expect(instance.call(action, [])).to be_falsey- end-- context "with one project not allowing an action" do- before do- allow(project)- .to receive(:allows_to?)- .with(action)- .and_return(false)- end-- it "is false" do- expect(instance.call(action, [project, other_project])).to be_falsey- end- end- end-- context "for a relation of projects" do- let(:context) { double("relation", class: ActiveRecord::Relation, to_a: [project]) }-- it_behaves_like "allowed to checked"- end-- context "for anything else" do- let(:context) { nil }-- it "is false" do- expect(subject).to be_falsey- end- end-- context "for a global check" do- context "with the user being admin" do- before do- user.admin = true- end-- it "is true" do- expect(instance.call(action, nil, global: true)).to be_truthy- end- end-- context "with the user having a granting role" do- before do- allow(Authorization).to receive(:roles).with(user, nil).and_return(user_roles_in_project)- allow(role).to receive(:allowed_to?).with(action).and_return(true)- end-- context "but the user not being active" do- before do- user.lock- end-- it "is unsuccessful", :aggregate_failures do- expect(instance.call(action, nil, global: true)).not_to be_truthy- end- end-- it "is successful", :aggregate_failures do- expect(instance.call(action, nil, global: true)).to be_truthy- end- end-- context "with the user not having a granting role" do- before do- allow(Authorization).to receive(:roles).with(user, nil).and_return(user_roles_in_project)- allow(role).to receive(:allowed_to?).with(action).and_return(false)- end-- it "is false" do- expect(instance.call(action, nil, global: true)).to be_falsey- end- end- end- end-end