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
use mix of handwritten and arel
ulferts3102c0d7e45a
diff --git a/app/models/projects/scopes/allowed_to.rb b/app/models/projects/scopes/allowed_to.rbindex 081684d5fcf..134ff86faa9 100644--- a/app/models/projects/scopes/allowed_to.rb+++ b/app/models/projects/scopes/allowed_to.rb@@ -31,79 +31,71 @@ module Projects::Scopesextend ActiveSupport::Concernclass_methods do- # Returns a ActiveRecord::Relation to find all projects for which+ # Returns an ActiveRecord::Relation to find all projects for which# +user+ has the given +permission+def allowed_to(user, permission)- permissions = if permission.is_a?(Hash)- OpenProject::AccessControl.allow_actions(permission)- else- [OpenProject::AccessControl.permission(permission)].compact- end+ permissions = allowed_to_permissions(permission)if user.admin?- Project- .joins(:enabled_modules)- .where(active: true)- .where(enabled_modules: { name: permissions.map(&:project_module).compact.uniq })+ Project.where(id: allowed_to_admin_relation(permissions))elsif user.anonymous?- Project.find_by_sql(allowed_to_non_member_sql(user, permissions))+ Project.where(id: allowed_to_non_member_relation(user, permissions))else- Project.find_by_sql("#{allowed_to_member_sql(user, permissions)} UNION #{allowed_to_non_member_sql(user, permissions)}")+ Project.where(Project.arel_table[:id].in(allowed_to_member_relation(user, permissions).arel.union(+ allowed_to_non_member_relation(user, permissions).arel+ )))endendprivate- def allowed_to_non_member_sql(user, permissions)- sql = <<~SQL.squish- SELECT "projects".*- FROM "projects"- #{allowed_to_enabled_module_join(permissions)}- INNER JOIN "roles"+ def allowed_to_admin_relation(permissions)+ Project+ .joins(allowed_to_enabled_module_join(permissions))+ .where(active: true)+ end++ def allowed_to_non_member_relation(user, permissions)+ Project+ .joins(allowed_to_enabled_module_join(permissions))+ .joins(<<~SQL.squish+ INNER JOIN "roles"ON "roles"."builtin" IN (#{Role::BUILTIN_ANONYMOUS}, #{Role::BUILTIN_NON_MEMBER})AND "projects"."active" = TRUEAND "projects"."public" = TRUEAND NOT EXISTS (SELECT 1FROM membersWHERE members.project_id = projects.id- AND members.user_id = :user_id+ AND members.user_id = #{user.id}AND members.entity_type IS NULLAND members.entity_id IS NULLLIMIT 1)- #{allowed_to_role_permission_join(permissions)}- SQL-- OpenProject::SqlSanitization.sanitize(sql,- user_id: user.id,- permission: permissions.map(&:name))+ SQL+ )+ .joins(allowed_to_role_permission_join(permissions))+ .select(:id)end- def allowed_to_member_sql(user, permissions)- sql = <<~SQL.squish- SELECT "projects".*- FROM "projects"- #{allowed_to_enabled_module_join(permissions)}- JOIN "members"+ def allowed_to_member_relation(user, permissions)+ Project+ .joins(allowed_to_enabled_module_join(permissions))+ .joins(<<~SQL.squish+ JOIN "members"ON "projects"."id" = "members"."project_id"- AND "members"."user_id" = :user_id+ AND "members"."user_id" = #{user.id}AND "members"."entity_type" IS NULLAND "members"."entity_id" IS NULLAND "projects"."active" = TRUE- JOIN "member_roles"- ON "members"."id" = "member_roles"."member_id"- JOIN "roles"- ON "projects"."active" = TRUE- AND ("roles"."id" = "member_roles"."role_id")- #{allowed_to_role_permission_join(permissions)}- SQL-- OpenProject::SqlSanitization.sanitize(sql,- user_id: user.id,- permission: permissions.map(&:name))+ SQL+ )+ .joins('JOIN "member_roles" ON "members"."id" = "member_roles"."member_id"')+ .joins('JOIN "roles" ON member_roles.role_id = roles.id')+ .joins(allowed_to_role_permission_join(permissions))+ .select(:id)enddef allowed_to_enabled_module_join(permissions)- project_module = permissions.map(&:project_module).compact.uniq+ project_module = permissions.filter_map(&:project_module).uniqif project_module.any?sql = <<~SQL.squish@@ -122,9 +114,15 @@ module Projects::Scopesreturn if permissions.all?(&:public?)condition = permissions.map do |permission|- <<~SQL.squish- (enabled_modules.name = '#{permission.project_module}' AND "role_permissions"."permission" = '#{permission.name}')- SQL+ if permission.public?+ <<~SQL.squish+ "role_permissions"."permission" = '#{permission.name}'+ SQL+ else+ <<~SQL.squish+ (enabled_modules.name = '#{permission.project_module}' AND "role_permissions"."permission" = '#{permission.name}')+ SQL+ endend.join(' OR ')<<~SQL.squish@@ -133,6 +131,14 @@ module Projects::ScopesAND (#{condition})SQLend++ def allowed_to_permissions(permission)+ if permission.is_a?(Hash)+ OpenProject::AccessControl.allow_actions(permission)+ else+ [OpenProject::AccessControl.permission(permission)].compact+ end+ endendendenddiff --git a/spec/models/projects/allowed_to_scope_spec.rb b/spec/models/projects/allowed_to_scope_spec.rbindex 84a1783cc05..68b0ce94c08 100644--- a/spec/models/projects/allowed_to_scope_spec.rb+++ b/spec/models/projects/allowed_to_scope_spec.rb@@ -72,13 +72,13 @@ RSpec.describe Project, 'allowed to' doshared_examples_for 'includes the project' doit 'includes the project' do- expect(Project.allowed_to(user, action)).to match_array [project]+ expect(described_class.allowed_to(user, action)).to contain_exactly(project)endendshared_examples_for 'is empty' doit 'is empty' do- expect(Project.allowed_to(user, action)).to be_empty+ expect(described_class.allowed_to(user, action)).to be_emptyendend@@ -88,8 +88,8 @@ RSpec.describe Project, 'allowed to' douser.save!end- context 'w/ the user being member- w/ the role having the permission' do+ context 'with the user being member+ with the role having the permission' dobefore donon_member_role.save!member.save!@@ -98,9 +98,9 @@ RSpec.describe Project, 'allowed to' doit_behaves_like 'includes the project'end- context 'w/ the user being member- w/ the role having the permission- w/o the project being active' do+ context 'with the user being member+ with the role having the permission+ without the project being active' dolet(:project_status) { false }before do@@ -110,8 +110,8 @@ RSpec.describe Project, 'allowed to' doit_behaves_like 'is empty'end- context 'w/ the user being member- w/o the role having the permission' do+ context 'with the user being member+ without the role having the permission' dolet(:permissions) { [] }before do@@ -121,14 +121,14 @@ RSpec.describe Project, 'allowed to' doit_behaves_like 'is empty'end- context 'w/o the user being member- w/ the role having the permission' do+ context 'without the user being member+ with the role having the permission' doit_behaves_like 'is empty'end- context 'w/ the user being member- w/ the role having the permission- w/o the associated module being active' do+ context 'with the user being member+ with the role having the permission+ without the associated module being active' dobefore domember.save!project.enabled_modules = []@@ -137,45 +137,45 @@ RSpec.describe Project, 'allowed to' doit_behaves_like 'is empty'end- context 'w/ the user being member- w/ the permission being public' do+ context 'with the user being member+ with the permission being public' dobefore domember.save!endit 'includes the project' do- expect(Project.allowed_to(user, public_action)).to match_array [project]+ expect(described_class.allowed_to(user, public_action)).to contain_exactly(project)endend- context 'w/ the user being member- w/ the permission being public- w/o the associated module being active' do+ context 'with the user being member+ with the permission being public+ without the associated module being active' dobefore domember.save!project.enabled_modules = []endit 'is empty' do- expect(Project.allowed_to(user, public_action)).to be_empty+ expect(described_class.allowed_to(user, public_action)).to be_emptyendend- context 'w/ the user being member- w/ the permission being public and not module bound- w/ no module bing active' do+ context 'with the user being member+ with the permission being public and not module bound+ with no module bing active' dobefore domember.save!project.enabled_modules = []endit 'includes the project' do- expect(Project.allowed_to(user, public_non_module_action)).to match_array [project]+ expect(described_class.allowed_to(user, public_non_module_action)).to contain_exactly(project)endendend- shared_examples_for 'w/ an admin user' do+ shared_examples_for 'with an admin user' dolet(:user) { admin }before do@@ -183,26 +183,32 @@ RSpec.describe Project, 'allowed to' douser.save!end- context 'w/o the user being a member' do+ context 'without the user being a member' doit_behaves_like 'includes the project'end- context 'w/o the project being active' do+ context 'without the project being active' dolet(:project_status) { false }it_behaves_like 'is empty'end- context 'w/o the project being active- w/ the permission being public' do+ context 'without the project being active+ with the permission being public' dolet(:project_status) { false }it 'is empty' do- expect(Project.allowed_to(user, public_action)).to be_empty+ expect(described_class.allowed_to(user, public_action)).to be_emptyendend- context 'w/o the project module being active' do+ context 'with the project being active with the permission being public' do+ it 'includes the project' do+ expect(described_class.allowed_to(user, public_action)).to contain_exactly(project)+ end+ end++ context 'without the project module being active' dobefore doproject.enabled_modules = []end@@ -211,99 +217,99 @@ RSpec.describe Project, 'allowed to' doendend- context 'w/ the project being private' do+ context 'with the project being private' dolet(:project) { private_project }it_behaves_like 'member based allowed to check'- it_behaves_like 'w/ an admin user'+ it_behaves_like 'with an admin user'- context 'w/ the user not being logged in' do+ context 'with the user not being logged in' dobefore doproject.save!anonymous.save!anonymous_role.save!end- context 'w/ the anonymous role having the permission' do+ context 'with the anonymous role having the permission' doit 'is empty' do- expect(Project.allowed_to(anonymous, action)).to be_empty+ expect(described_class.allowed_to(anonymous, action)).to be_emptyendend- context 'w/ the permission being public' do+ context 'with the permission being public' doit 'is empty' do- expect(Project.allowed_to(anonymous, public_action)).to be_empty+ expect(described_class.allowed_to(anonymous, public_action)).to be_emptyendendendend- context 'w/ the project being public' do+ context 'with the project being public' dolet(:project) { public_project }it_behaves_like 'member based allowed to check'- it_behaves_like 'w/ an admin user'+ it_behaves_like 'with an admin user'- context 'w/ the user not being logged in' do+ context 'with the user not being logged in' dobefore doproject.save!anonymous.save!anonymous_role.save!end- context 'w/ the anonymous role having the permission' do+ context 'with the anonymous role having the permission' doit 'includes the project' do- expect(Project.allowed_to(anonymous, action)).to match_array [project]+ expect(described_class.allowed_to(anonymous, action)).to contain_exactly(project)endend- context 'w/ the anonymous role having the permission- w/o the project being active' do+ context 'with the anonymous role having the permission+ without the project being active' dolet(:project_status) { false }it 'is empty' do- expect(Project.allowed_to(anonymous, action)).to be_empty+ expect(described_class.allowed_to(anonymous, action)).to be_emptyendend- context 'w/o the anonymous role having the permission' do+ context 'without the anonymous role having the permission' dolet(:anonymous_permissions) { [] }it 'is empty' do- expect(Project.allowed_to(anonymous, action)).to be_empty+ expect(described_class.allowed_to(anonymous, action)).to be_emptyendend- context 'w/ the permission being public' do+ context 'with the permission being public' doit 'includes the project' do- expect(Project.allowed_to(anonymous, public_action)).to match_array [project]+ expect(described_class.allowed_to(anonymous, public_action)).to contain_exactly(project)endend- context 'w/ the permission being public- w/ the associated module not being active' do+ context 'with the permission being public+ with the associated module not being active' dobefore doproject.enabled_modules = []endit 'is empty' do- expect(Project.allowed_to(anonymous, public_action)).to be_empty+ expect(described_class.allowed_to(anonymous, public_action)).to be_emptyendend- context 'w/ the permission being public and not module bound- w/ no module being active' do+ context 'with the permission being public and not module bound+ with no module being active' dobefore doproject.enabled_modules = []endit 'includes the project' do- expect(Project.allowed_to(anonymous, public_non_module_action)).to match_array [project]+ expect(described_class.allowed_to(anonymous, public_non_module_action)).to contain_exactly(project)endendend- context 'w/ the user being member' do+ context 'with the user being member' dobefore doproject.save!user.save!@@ -311,8 +317,8 @@ RSpec.describe Project, 'allowed to' domember.save!end- context 'w/ the role not having the permission- w/ non member having the permission' do+ context 'with the role not having the permission+ with non member having the permission' dolet(:permissions) { [] }let(:non_member_permissions) { [action] }@@ -320,64 +326,64 @@ RSpec.describe Project, 'allowed to' doendend- context 'w/o the user being member' do+ context 'without the user being member' dobefore doproject.save!user.save!non_member_role.save!end- context 'w/ the non member role having the permission' do+ context 'with the non member role having the permission' doit_behaves_like 'includes the project'end- context 'w/ the non member role having the permission- w/o the project being active' do+ context 'with the non member role having the permission+ without the project being active' dolet(:project_status) { false }it_behaves_like 'is empty'end- context 'w/ the permission being public and not module bound- w/o the project being active' do+ context 'with the permission being public and not module bound+ without the project being active' dolet(:project_status) { false }it 'is empty' do- expect(Project.allowed_to(user, public_non_module_action)).to be_empty+ expect(described_class.allowed_to(user, public_non_module_action)).to be_emptyendend- context 'w/o the non member role having the permission' do+ context 'without the non member role having the permission' dolet(:non_member_permissions) { [] }it_behaves_like 'is empty'end- context 'w/ the permission being public' do+ context 'with the permission being public' doit 'includes the project' do- expect(Project.allowed_to(user, public_action)).to match_array [project]+ expect(described_class.allowed_to(user, public_action)).to contain_exactly(project)endend- context 'w/ the permission being public- w/ the module not being active' do+ context 'with the permission being public+ with the module not being active' dobefore doproject.enabled_modules = []endit 'is empty' do- expect(Project.allowed_to(user, public_action)).to be_empty+ expect(described_class.allowed_to(user, public_action)).to be_emptyendend- context 'w/ the permission being public and not module bound- w/ no module active' do+ context 'with the permission being public and not module bound+ with no module active' dobefore doproject.enabled_modules = []endit 'includes the project' do- expect(Project.allowed_to(user, public_non_module_action)).to match_array [project]+ expect(described_class.allowed_to(user, public_non_module_action)).to contain_exactly(project)endendend