provide allowed_project_ids_for and visible_condition_for for entities

This commit is contained in:
Alexander Meindl 2022-06-14 19:13:17 +02:00
parent 6a199ce07d
commit 3877769c7a
2 changed files with 62 additions and 0 deletions

View File

@ -45,6 +45,21 @@ module Additionals
end
module InstanceMethods
def visible_condition_for(entity, **options)
@visible_condition_for ||= Hash.new do |memo, (inner_entity, inner_options)|
memo[[inner_entity, inner_options]] = inner_entity.visible_condition self, **inner_options
end
@visible_condition_for[[entity, options]]
end
def allowed_project_ids_for(permission, **options)
@allowed_project_ids_for ||= Hash.new do |memo, (inner_perm, inner_options)|
ids = Project.where(Project.allowed_to_condition(self, inner_perm, **inner_options)).ids
memo[[inner_perm, inner_options]] = ids
end
@allowed_project_ids_for[[permission, options]]
end
def can_be_admin?
@can_be_admin ||= AdditionalsPlugin.active_sudo? ? (admin || sudoer) : admin
end

View File

@ -65,4 +65,51 @@ class UserTest < Additionals::TestCase
assert_not User.where(sudoer: false, admin: false).first.can_be_admin?
end
def test_allowed_project_ids_for
user = users :users_002
assert_sorted_equal Project.where(Project.allowed_to_condition(user, :view_issues)).ids,
user.allowed_project_ids_for(:view_issues)
# test cache
assert_sorted_equal Project.where(Project.allowed_to_condition(user, :view_issues)).ids,
user.allowed_project_ids_for(:view_issues)
end
def test_allowed_project_ids_for_with_options
user = users :users_002
options = { skip_pre_condition: true, project: nil }
assert_sorted_equal Project.where(Project.allowed_to_condition(user, :view_issues, **options)).ids,
user.allowed_project_ids_for(:view_issues, **options)
# test cache
assert_sorted_equal Project.where(Project.allowed_to_condition(user, :view_issues, **options)).ids,
user.allowed_project_ids_for(:view_issues, **options)
end
def test_visible_condition_for
user = users :users_002
assert_equal Issue.visible_condition(user),
user.visible_condition_for(Issue)
# test cache
assert_equal Issue.visible_condition(user),
user.visible_condition_for(Issue)
end
def test_visible_condition_for_with_skip_pre_condition
user = users :users_002
options = { skip_pre_condition: true, project: projects(:projects_001) }
assert_equal Issue.visible_condition(user, **options),
user.visible_condition_for(Issue, **options)
# test cache
assert_equal Issue.visible_condition(user, **options),
user.visible_condition_for(Issue, **options)
end
end