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
Move the UnknownPermissionError into the permission_for method
Klaus Zanders9306d3521af5
diff --git a/app/models/users/permission_checks.rb b/app/models/users/permission_checks.rbindex 87c0806fb5e..65b1b6afd9c 100644--- a/app/models/users/permission_checks.rb+++ b/app/models/users/permission_checks.rb@@ -98,7 +98,7 @@ module Users::PermissionChecks# Helper method to be used in places where we just throw anything into the permission check and don't know what# context it should be checked on. Things like menu item checks, controller action checks, generic services, etc.def allowed_based_on_permission_context?(permission, project: nil, entity: nil) # rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize- permissions = Authorization.permissions_for(permission)+ permissions = Authorization.permissions_for(permission, raise_on_unknown: true)permissions.any? do |perm|if perm.global?diff --git a/app/services/authorization.rb b/app/services/authorization.rbindex d94af695dd4..d9d50570fc4 100644--- a/app/services/authorization.rb+++ b/app/services/authorization.rb@@ -59,25 +59,21 @@ module Authorization# Possible arguments# - Symbol permission names (e.g. :view_work_packages)# - Hash with :controller and :action (e.g. { controller: 'work_packages', action: 'show' })- def permissions_for(action)+ #+ # Exceptions+ # - When there is no permission matching the +action+ parameter, either an empty array is returned+ # or an +UnknownPermissionError+ is raised (depending on the +raise_on_unknown+ parameter).+ # . Additionally a debugger message is logged.+ # If the permission is disabled, it will not raise an error or log debug output.+ def permissions_for(action, raise_on_unknown: false) # rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSizereturn [action] if action.is_a?(OpenProject::AccessControl::Permission)return action if action.is_a?(Array) && (action.empty? || action.all?(OpenProject::AccessControl::Permission))- if action.is_a?(Hash)- OpenProject::AccessControl.allow_actions(action)- else- [OpenProject::AccessControl.permission(action)].compact- end- end-- # Returns a set of normalized permissions filtered for a given context- # - When there is no permission matching the +permission+ parameter, either an empty array is returned- # or an +UnknownPermissionError+ is raised (depending on the raise_on_unknown parameter).- # If the permission is disabled, it will never raise an error.- # - When there are no permissions available for the given context (based on +permissible_on+- # attribute of the permission), an +IllegalPermissionContextError+ is raised- def contextual_permissions(action, context, raise_on_unknown: false)- perms = permissions_for(action)+ perms = if action.is_a?(Hash)+ OpenProject::AccessControl.allow_actions(action)+ else+ [OpenProject::AccessControl.permission(action)].compact+ endif perms.blank?if !OpenProject::AccessControl.disabled_permission?(action)@@ -85,9 +81,18 @@ module Authorizationraise UnknownPermissionError.new(action) if raise_on_unknownend- return []+ []end+ perms+ end++ # Returns a set of normalized permissions filtered for a given context+ # - When there are no permissions available for the given context (based on +permissible_on++ # attribute of the permission), an +IllegalPermissionContextError+ is raised+ def contextual_permissions(action, context, raise_on_unknown: false)+ perms = permissions_for(action, raise_on_unknown:)+context_perms = perms.select { |p| p.permissible_on?(context) }raise IllegalPermissionContextError.new(action, perms, context) if context_perms.blank?diff --git a/spec/services/authorization_spec.rb b/spec/services/authorization_spec.rbindex 63ff07d9c9b..9d1f9181d5f 100644--- a/spec/services/authorization_spec.rb+++ b/spec/services/authorization_spec.rb@@ -83,7 +83,9 @@ RSpec.describe Authorization doenddescribe '.permissions_for' do- subject { described_class.permissions_for(action) }+ let(:raise_on_unknown) { false }++ subject { described_class.permissions_for(action, raise_on_unknown:) }context 'when called with a Permission object' dolet(:action) { OpenProject::AccessControl.permission(:view_work_packages) }@@ -136,39 +138,29 @@ RSpec.describe Authorization doendend- context 'when action is a permission name that does not exist' do- let(:action) { :this_permission_does_not_exist }-- it 'returns the Permission object wrapped in an array' do- expect(subject).to be_empty+ context 'when there is a permission but it is disabled' do+ let(:permission_object) { OpenProject::AccessControl.permission(:manage_user) }+ let(:action) { permission_object.name }++ around do |example|+ permission_object.disable!+ OpenProject::AccessControl.clear_caches+ example.run+ ensure+ permission_object.enable!+ OpenProject::AccessControl.clear_cachesend- end- end- describe '.contextual_permissions' do- subject { described_class.contextual_permissions(action, context, raise_on_unknown:) }-- let(:raise_on_unknown) { false }- let(:context) { nil }-- let(:global_permission) { OpenProject::AccessControl.permission(:manage_user) }- let(:project_permission) { OpenProject::AccessControl.permission(:manage_members) }- let(:project_and_work_package_permission) { OpenProject::AccessControl.permission(:view_work_packages) }-- let(:returned_permissions) do- [- global_permission,- project_permission,- project_and_work_package_permission- ]- end-- before do- allow(described_class).to receive(:permissions_for).and_return(returned_permissions)+ it 'returns an empty array and does not warn or raise' do+ expect(Rails.logger).not_to receive(:debug)+ expect do+ expect(subject).to be_empty+ end.not_to raise_error+ endendcontext 'when there is no permission' do- let(:returned_permissions) { [] }+ let(:action) { :this_permission_does_not_exist }context 'and raise_on_unknown is false' dolet(:raise_on_unknown) { false }@@ -199,77 +191,79 @@ RSpec.describe Authorization doendend- context 'with global context' do- let(:context) { :global }+ describe '.contextual_permissions' do+ subject { described_class.contextual_permissions(action, context, raise_on_unknown:) }- context 'when a global permission is part of the returned permissions' do- it 'returns only the global permission' do- expect(subject).to eq([global_permission])- end+ let(:raise_on_unknown) { false }+ let(:context) { nil }- context 'with a disabled permission' do- let(:action) { global_permission.name }- let(:returned_permissions) { [] }-- around do |example|- global_permission.disable!- OpenProject::AccessControl.clear_caches- example.run- ensure- global_permission.enable!- OpenProject::AccessControl.clear_caches- end+ let(:global_permission) { OpenProject::AccessControl.permission(:manage_user) }+ let(:project_permission) { OpenProject::AccessControl.permission(:manage_members) }+ let(:project_and_work_package_permission) { OpenProject::AccessControl.permission(:view_work_packages) }- it 'returns an empty array and does not warn or raise' do- expect(Rails.logger).not_to receive(:debug)- expect do- expect(subject).to be_empty- end.not_to raise_error+ let(:returned_permissions) do+ [+ global_permission,+ project_permission,+ project_and_work_package_permission+ ]+ end++ before do+ allow(described_class).to receive(:permissions_for).and_return(returned_permissions)+ end++ context 'with global context' do+ let(:context) { :global }++ context 'when a global permission is part of the returned permissions' do+ it 'returns only the global permission' do+ expect(subject).to eq([global_permission])endend- end- context 'when no global permission is part of the returned permissions' do- let(:returned_permissions) { [project_permission, project_and_work_package_permission] }+ context 'when no global permission is part of the returned permissions' do+ let(:returned_permissions) { [project_permission, project_and_work_package_permission] }- it 'raises an IllegalPermissionContextError' do- expect { subject }.to raise_error(Authorization::IllegalPermissionContextError)+ it 'raises an IllegalPermissionContextError' do+ expect { subject }.to raise_error(Authorization::IllegalPermissionContextError)+ endendend- end- context 'with project context' do- let(:context) { :project }+ context 'with project context' do+ let(:context) { :project }- context 'when a project permission is part of the returned permissions' do- it 'returns only the project permissions' do- expect(subject).to eq([project_permission, project_and_work_package_permission])+ context 'when a project permission is part of the returned permissions' do+ it 'returns only the project permissions' do+ expect(subject).to eq([project_permission, project_and_work_package_permission])+ endend- end- context 'when no project permission is part of the returned permissions' do- let(:returned_permissions) { [global_permission] }+ context 'when no project permission is part of the returned permissions' do+ let(:returned_permissions) { [global_permission] }- it 'raises an IllegalPermissionContextError' do- expect { subject }.to raise_error(Authorization::IllegalPermissionContextError)+ it 'raises an IllegalPermissionContextError' do+ expect { subject }.to raise_error(Authorization::IllegalPermissionContextError)+ endendend- end- context 'with work package context' do- let(:context) { :work_package }+ context 'with work package context' do+ let(:context) { :work_package }- context 'when a work package permission is part of the returned permissions' do- it 'returns only the work package permission' do- expect(subject).to eq([project_and_work_package_permission])+ context 'when a work package permission is part of the returned permissions' do+ it 'returns only the work package permission' do+ expect(subject).to eq([project_and_work_package_permission])+ endend- end- context 'when no work package permission is part of the returned permissions' do- let(:returned_permissions) { [global_permission, project_permission] }+ context 'when no work package permission is part of the returned permissions' do+ let(:returned_permissions) { [global_permission, project_permission] }- it 'raises an IllegalPermissionContextError' do- expect { subject }.to raise_error(Authorization::IllegalPermissionContextError)+ it 'raises an IllegalPermissionContextError' do+ expect { subject }.to raise_error(Authorization::IllegalPermissionContextError)+ endendendend