Skip to content

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

use mix of handwritten and arel

ulferts3102c0d7e45a
diff --git a/app/models/projects/scopes/allowed_to.rb b/app/models/projects/scopes/allowed_to.rb
index 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::Scopes
extend ActiveSupport::Concern
class_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
+ )))
end
end
private
- 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" = TRUE
AND "projects"."public" = TRUE
AND NOT EXISTS (SELECT 1
FROM members
WHERE members.project_id = projects.id
- AND members.user_id = :user_id
+ AND members.user_id = #{user.id}
AND members.entity_type IS NULL
AND members.entity_id IS NULL
LIMIT 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 NULL
AND "members"."entity_id" IS NULL
AND "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)
end
def allowed_to_enabled_module_join(permissions)
- project_module = permissions.map(&:project_module).compact.uniq
+ project_module = permissions.filter_map(&:project_module).uniq
if project_module.any?
sql = <<~SQL.squish
@@ -122,9 +114,15 @@ module Projects::Scopes
return 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
+ end
end.join(' OR ')
<<~SQL.squish
@@ -133,6 +131,14 @@ module Projects::Scopes
AND (#{condition})
SQL
end
+
+ def allowed_to_permissions(permission)
+ if permission.is_a?(Hash)
+ OpenProject::AccessControl.allow_actions(permission)
+ else
+ [OpenProject::AccessControl.permission(permission)].compact
+ end
+ end
end
end
end
diff --git a/spec/models/projects/allowed_to_scope_spec.rb b/spec/models/projects/allowed_to_scope_spec.rb
index 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' do
shared_examples_for 'includes the project' do
it '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)
end
end
shared_examples_for 'is empty' do
it 'is empty' do
- expect(Project.allowed_to(user, action)).to be_empty
+ expect(described_class.allowed_to(user, action)).to be_empty
end
end
@@ -88,8 +88,8 @@ RSpec.describe Project, 'allowed to' do
user.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' do
before do
non_member_role.save!
member.save!
@@ -98,9 +98,9 @@ RSpec.describe Project, 'allowed to' do
it_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' do
let(:project_status) { false }
before do
@@ -110,8 +110,8 @@ RSpec.describe Project, 'allowed to' do
it_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' do
let(:permissions) { [] }
before do
@@ -121,14 +121,14 @@ RSpec.describe Project, 'allowed to' do
it_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' do
it_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' do
before do
member.save!
project.enabled_modules = []
@@ -137,45 +137,45 @@ RSpec.describe Project, 'allowed to' do
it_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' do
before do
member.save!
end
it '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)
end
end
- 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' do
before do
member.save!
project.enabled_modules = []
end
it 'is empty' do
- expect(Project.allowed_to(user, public_action)).to be_empty
+ expect(described_class.allowed_to(user, public_action)).to be_empty
end
end
- 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' do
before do
member.save!
project.enabled_modules = []
end
it '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)
end
end
end
- shared_examples_for 'w/ an admin user' do
+ shared_examples_for 'with an admin user' do
let(:user) { admin }
before do
@@ -183,26 +183,32 @@ RSpec.describe Project, 'allowed to' do
user.save!
end
- context 'w/o the user being a member' do
+ context 'without the user being a member' do
it_behaves_like 'includes the project'
end
- context 'w/o the project being active' do
+ context 'without the project being active' do
let(: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' do
let(: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_empty
end
end
- 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' do
before do
project.enabled_modules = []
end
@@ -211,99 +217,99 @@ RSpec.describe Project, 'allowed to' do
end
end
- context 'w/ the project being private' do
+ context 'with the project being private' do
let(: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' do
before do
project.save!
anonymous.save!
anonymous_role.save!
end
- context 'w/ the anonymous role having the permission' do
+ context 'with the anonymous role having the permission' do
it 'is empty' do
- expect(Project.allowed_to(anonymous, action)).to be_empty
+ expect(described_class.allowed_to(anonymous, action)).to be_empty
end
end
- context 'w/ the permission being public' do
+ context 'with the permission being public' do
it 'is empty' do
- expect(Project.allowed_to(anonymous, public_action)).to be_empty
+ expect(described_class.allowed_to(anonymous, public_action)).to be_empty
end
end
end
end
- context 'w/ the project being public' do
+ context 'with the project being public' do
let(: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' do
before do
project.save!
anonymous.save!
anonymous_role.save!
end
- context 'w/ the anonymous role having the permission' do
+ context 'with the anonymous role having the permission' do
it '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)
end
end
- 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' do
let(: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_empty
end
end
- context 'w/o the anonymous role having the permission' do
+ context 'without the anonymous role having the permission' do
let(:anonymous_permissions) { [] }
it 'is empty' do
- expect(Project.allowed_to(anonymous, action)).to be_empty
+ expect(described_class.allowed_to(anonymous, action)).to be_empty
end
end
- context 'w/ the permission being public' do
+ context 'with the permission being public' do
it '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)
end
end
- 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' do
before do
project.enabled_modules = []
end
it 'is empty' do
- expect(Project.allowed_to(anonymous, public_action)).to be_empty
+ expect(described_class.allowed_to(anonymous, public_action)).to be_empty
end
end
- 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' do
before do
project.enabled_modules = []
end
it '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)
end
end
end
- context 'w/ the user being member' do
+ context 'with the user being member' do
before do
project.save!
user.save!
@@ -311,8 +317,8 @@ RSpec.describe Project, 'allowed to' do
member.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' do
let(:permissions) { [] }
let(:non_member_permissions) { [action] }
@@ -320,64 +326,64 @@ RSpec.describe Project, 'allowed to' do
end
end
- context 'w/o the user being member' do
+ context 'without the user being member' do
before do
project.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' do
it_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' do
let(: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' do
let(: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_empty
end
end
- context 'w/o the non member role having the permission' do
+ context 'without the non member role having the permission' do
let(:non_member_permissions) { [] }
it_behaves_like 'is empty'
end
- context 'w/ the permission being public' do
+ context 'with the permission being public' do
it '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)
end
end
- 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' do
before do
project.enabled_modules = []
end
it 'is empty' do
- expect(Project.allowed_to(user, public_action)).to be_empty
+ expect(described_class.allowed_to(user, public_action)).to be_empty
end
end
- 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' do
before do
project.enabled_modules = []
end
it '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)
end
end
end