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
Add short circuits into the user permissible service when asking any project and permissions are granted to non-member or anonymous
Klaus Zandersc1059a0ca864
diff --git a/app/services/authorization/user_permissible_service.rb b/app/services/authorization/user_permissible_service.rbindex 94bf483299c..5cf7ef9b25d 100644--- a/app/services/authorization/user_permissible_service.rb+++ b/app/services/authorization/user_permissible_service.rb@@ -30,7 +30,12 @@ module Authorizationperms = contextual_permissions(permission, :project)return true if admin_and_all_granted_to_admin?(perms)- Project.allowed_to(user, perms).exists?+ # If no projects matching the scope exists, it could still be that the user has access to the+ # permission either via the anonymous or non-member role, especially when there are no projects+ # in the database. So if this returns false, we check non-member and anonymous permissions as well.+ Project.allowed_to(user, perms).exists? ||+ non_member_permissions.intersect?(perms.map { |perm| perm.name.to_sym }) ||+ anonymous_permissions.intersect?(perms.map { |perm| perm.name.to_sym })enddef allowed_in_entity?(permission, entities_to_check, entity_class)@@ -47,23 +52,22 @@ module Authorizationendend- def allowed_in_any_entity?(permission, entity_class, in_project: nil)+ def allowed_in_any_entity?(permission, entity_class, in_project: nil) # rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSizeperms = contextual_permissions(permission, context_name(entity_class))return true if admin_and_all_granted_to_admin?(perms)- # Short circuit, when the user is permitted in the project. This is for the edge case- # when the user has permissions in the project, but not work package exists yet, the other- # query below would return false in those cases.- return true if in_project && allowed_in_single_project?(perms, in_project)- return true if !in_project && allowed_in_any_project?(perms)-# entity_class.allowed_to will also check whether the user has the permission via a membership in the project.allowed_scope = entity_class.allowed_to(user, perms)if in_projectallowed_scope.exists?(project: in_project)else- allowed_scope.exists?+ # If no entities matching the scope exists, it could still be that the user has access to the+ # permission either via the anonymous or non-member role, especially when there are no projects+ # or entities in the database. So if this returns false, we check non-member and anonymous permissions as well.+ allowed_scope.exists? ||+ non_member_permissions.intersect?(perms.map { |perm| perm.name.to_sym }) ||+ (user.anonymous? && anonymous_permissions.intersect?(perms.map { |perm| perm.name.to_sym }))endend@@ -127,5 +131,13 @@ module Authorizationdef context_name(entity_class)entity_class.model_name.element.to_symend++ def non_member_permissions+ @non_member_permissions ||= ProjectRole.non_member.permissions+ end++ def anonymous_permissions+ @anonymous_permissions ||= ProjectRole.anonymous.permissions+ endendenddiff --git a/spec/services/authorization/user_permissible_service_spec.rb b/spec/services/authorization/user_permissible_service_spec.rbindex ea6ba6318bf..c4170a86304 100644--- a/spec/services/authorization/user_permissible_service_spec.rb+++ b/spec/services/authorization/user_permissible_service_spec.rb@@ -5,8 +5,8 @@ RSpec.describe Authorization::UserPermissibleService doshared_let(:anonymous_user) { create(:anonymous) }shared_let(:project) { create(:project) }shared_let(:work_package) { create(:work_package, project:) }- shared_let(:non_member_role) { create(:non_member, permissions: [:view_work_packages]) }- shared_let(:anonymous_role) { create(:anonymous_role, permissions: [:view_work_packages]) }+ shared_let(:non_member_role) { create(:non_member, permissions: [:view_news]) }+ shared_let(:anonymous_role) { create(:anonymous_role, permissions: [:view_meetings]) }let(:queried_user) { user }@@ -140,6 +140,27 @@ RSpec.describe Authorization::UserPermissibleService docontext 'and the user is not a member of any work package or project' doit { is_expected.not_to be_allowed_in_project(permission, project) }++ context 'and the project is public' do+ before { project.update(public: true) }++ context 'when requesting a permission that is not granted to the non-member role' do+ it { is_expected.not_to be_allowed_in_project(permission, project) }+ end++ context 'when requesting a permission that is granted to the non-member role' do+ let(:permission) { :view_news }++ it { is_expected.to be_allowed_in_project(permission, project) }+ end++ context 'when an anonymous user is requesting a permission that is granted to the anonymous role' do+ let(:queried_user) { anonymous_user }+ let(:permission) { :view_meetings }++ it { is_expected.to be_allowed_in_project(permission, project) }+ end+ endendcontext 'and the user is a member of a project' do@@ -182,12 +203,33 @@ RSpec.describe Authorization::UserPermissibleService docontext 'and the project is public' dobefore { project.update_column(:public, true) }- it { is_expected.to be_allowed_in_any_project(permission) }+ context 'and a permission is requested that is not granted to the non-member role' do+ it { is_expected.not_to be_allowed_in_any_project(permission) }+ end++ context 'and a permission is requested that is granted to the non-member role' do+ let(:permission) { :view_news }++ it { is_expected.to be_allowed_in_any_project(permission) }++ context 'and no project exists' do+ before { Project.destroy_all }++ it { is_expected.to be_allowed_in_any_project(permission) }+ end+ endcontext 'and the user is the anonymous user' dolet(:queried_user) { anonymous_user }+ let(:permission) { :view_meetings }it { is_expected.to be_allowed_in_any_project(permission) }++ context 'and no project exists' do+ before { Project.destroy_all }++ it { is_expected.to be_allowed_in_any_project(permission) }+ endendcontext 'and the project is archived' do@@ -220,7 +262,15 @@ RSpec.describe Authorization::UserPermissibleService docontext 'and the project is public' dobefore { project.update_column(:public, true) }- it { is_expected.to be_allowed_in_any_project(permission) }+ context 'and a permission is requested that is not granted to the non-member role' do+ it { is_expected.not_to be_allowed_in_any_project(permission) }+ end++ context 'and a permission is requested that is granted to the non-member role' do+ let(:permission) { :view_news }++ it { is_expected.to be_allowed_in_any_project(permission) }+ endcontext 'and the project is archived' dobefore { project.update_column(:active, false) }