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
Extract method to request all permissions on a given context for a user
Klaus Zanders460435977158
diff --git a/app/models/users/permission_checks.rb b/app/models/users/permission_checks.rbindex 78009d1359f..1afdea76c47 100644--- a/app/models/users/permission_checks.rb+++ b/app/models/users/permission_checks.rb@@ -85,6 +85,14 @@ module Users::PermissionChecksroles_for_project(project).any?(&:member?)end+ def all_permissions_for(context)+ Authorization+ .roles(self, context)+ .includes(:role_permissions)+ .pluck(:permission)+ .map(&:to_sym)+ end+# Old allowed_to? interface. Marked as deprecated, should be removed at some point ... Guessing 14.0?def allowed_to?(action, context, global: false)diff --git a/app/services/authorization/user_permissible_service.rb b/app/services/authorization/user_permissible_service.rbindex e3ab4fa45ca..1607f554d4c 100644--- a/app/services/authorization/user_permissible_service.rb+++ b/app/services/authorization/user_permissible_service.rb@@ -47,8 +47,7 @@ module Authorizationenddef allowed_in_any_entity?(permission, entity_class, in_project: nil)- perms = contextual_permissions(permission,- context_name(entity_class))+ perms = contextual_permissions(permission, context_name(entity_class))return true if admin_and_all_granted_to_admin?(perms)# entity_class.allowed_to will also check whether the user has the permission via a membership in the project.@@ -65,11 +64,7 @@ module Authorizationdef cached_permissions(context)@cached_permissions ||= Hash.new do |hash, context_key|- hash[context_key] = Authorization- .roles(user, context_key)- .includes(:role_permissions)- .pluck(:permission)- .map(&:to_sym)+ hash[context_key] = user.all_permissions_for(context_key)end@cached_permissions[context]diff --git a/spec/models/users/permission_checks_spec.rb b/spec/models/users/permission_checks_spec.rbnew file mode 100644index 00000000000..7d63b1f40a1--- /dev/null+++ b/spec/models/users/permission_checks_spec.rb@@ -0,0 +1,84 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2023 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 User, "permission check methods" do+ subject { build_stubbed(:user) }++ let(:project) { build_stubbed(:project) }++ describe '.allowed' do+ it 'calls the Authorization.users method' do+ expect(Authorization).to receive(:users).with(:view_work_packages, project)+ described_class.allowed(:view_work_packages, project)+ end+ end++ it { is_expected.to delegate_method(:allowed_globally?).to(:user_permissible_service) }+ it { is_expected.to delegate_method(:allowed_in_project?).to(:user_permissible_service) }+ it { is_expected.to delegate_method(:allowed_in_any_project?).to(:user_permissible_service) }+ it { is_expected.to delegate_method(:allowed_in_entity?).to(:user_permissible_service) }+ it { is_expected.to delegate_method(:allowed_in_any_entity?).to(:user_permissible_service) }++ Member::ALLOWED_ENTITIES.each do |entity_model_name|+ context "for #{entity_model_name}" do+ it "defines allowed_in_#{entity_model_name.underscore}?" do+ expect(subject).to respond_to("allowed_in_#{entity_model_name.underscore}?")+ end++ it "defines allowed_in_any_#{entity_model_name.underscore}?" do+ expect(subject).to respond_to("allowed_in_any_#{entity_model_name.underscore}?")+ end+ end+ end++ describe '#all_permissions_for' do+ let(:project) { create(:project) }++ subject do+ create(:user, global_permissions: [:create_user],+ member_with_permissions: { project => %i[view_work_packages edit_work_packages] })+ end++ it 'returns all permissions given on the project' do+ expect(subject.all_permissions_for(project)).to match_array(%i[view_work_packages edit_work_packages])+ end++ it 'returns all global permissions' do+ skip 'Current implementation of the Authorization.roles query returns ALL permissions the user has, not only global ones. ' \+ 'We should change this in the fututre, thats why this test is already in here.'++ expect(subject.all_permissions_for(nil)).to match_array(%i[create_user])+ end++ it 'returns all permissions the user has (with project and global permissions)' do+ expect(subject.all_permissions_for(nil)).to match_array(%i[create_user view_work_packages edit_work_packages])+ end+ end+end