debian-mirror-gitlab/app/models/ability.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

161 lines
5.7 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class Ability
class << self
# Given a list of users and a project this method returns the users that can
# read the given project.
def users_that_can_read_project(users, project)
2017-09-10 17:25:29 +05:30
DeclarativePolicy.subject_scope do
users.select { |u| allowed?(u, :read_project, project) }
end
end
2018-10-15 14:42:47 +05:30
# Given a list of users and a group this method returns the users that can
# read the given group.
def users_that_can_read_group(users, group)
DeclarativePolicy.subject_scope do
users.select { |u| allowed?(u, :read_group, group) }
end
end
2017-08-17 22:00:37 +05:30
# Given a list of users and a snippet this method returns the users that can
# read the given snippet.
def users_that_can_read_personal_snippet(users, snippet)
2017-09-10 17:25:29 +05:30
DeclarativePolicy.subject_scope do
2020-03-13 15:44:24 +05:30
users.select { |u| allowed?(u, :read_snippet, snippet) }
2017-08-17 22:00:37 +05:30
end
end
2022-08-13 15:12:31 +05:30
# A list of users that can read confidential notes in a project
def users_that_can_read_internal_notes(users, note_parent)
DeclarativePolicy.subject_scope do
2023-04-23 21:23:45 +05:30
users.select { |u| allowed?(u, :read_internal_note, note_parent) }
2022-08-13 15:12:31 +05:30
end
end
2016-09-13 17:45:13 +05:30
# Returns an Array of Issues that can be read by the given user.
#
# issues - The issues to reduce down to those readable by the user.
# user - The User for which to check the issues
2018-03-27 19:54:05 +05:30
# filters - A hash of abilities and filters to apply if the user lacks this
# ability
def issues_readable_by_user(issues, user = nil, filters: {})
issues = apply_filters_if_needed(issues, user, filters)
2017-09-10 17:25:29 +05:30
DeclarativePolicy.user_scope do
issues.select { |issue| issue.visible_to_user?(user) }
end
2016-09-13 17:45:13 +05:30
end
2018-03-27 19:54:05 +05:30
# Returns an Array of MergeRequests that can be read by the given user.
#
2020-04-08 14:13:33 +05:30
# merge_requests - MRs out of which to collect MRs readable by the user.
2018-03-27 19:54:05 +05:30
# user - The User for which to check the merge_requests
# filters - A hash of abilities and filters to apply if the user lacks this
# ability
def merge_requests_readable_by_user(merge_requests, user = nil, filters: {})
merge_requests = apply_filters_if_needed(merge_requests, user, filters)
DeclarativePolicy.user_scope do
merge_requests.select { |mr| allowed?(user, :read_merge_request, mr) }
end
end
2021-09-30 23:02:18 +05:30
def feature_flags_readable_by_user(feature_flags, user = nil, filters: {})
feature_flags = apply_filters_if_needed(feature_flags, user, filters)
DeclarativePolicy.user_scope do
feature_flags.select { |flag| allowed?(user, :read_feature_flag, flag) }
end
end
2021-09-04 01:27:46 +05:30
def allowed?(user, ability, subject = :global, opts = {})
2017-09-10 17:25:29 +05:30
if subject.is_a?(Hash)
2021-04-29 21:17:54 +05:30
opts = subject
subject = :global
2017-09-10 17:25:29 +05:30
end
2015-11-26 14:37:03 +05:30
2017-09-10 17:25:29 +05:30
policy = policy_for(user, subject)
2016-08-24 12:49:21 +05:30
2017-09-10 17:25:29 +05:30
case opts[:scope]
when :user
2021-09-04 01:27:46 +05:30
DeclarativePolicy.user_scope { policy.allowed?(ability) }
2017-09-10 17:25:29 +05:30
when :subject
2021-09-04 01:27:46 +05:30
DeclarativePolicy.subject_scope { policy.allowed?(ability) }
2017-09-10 17:25:29 +05:30
else
2021-09-04 01:27:46 +05:30
policy.allowed?(ability)
2017-09-10 17:25:29 +05:30
end
2021-09-04 01:27:46 +05:30
ensure
# TODO: replace with runner invalidation:
# See: https://gitlab.com/gitlab-org/declarative-policy/-/merge_requests/24
# See: https://gitlab.com/gitlab-org/declarative-policy/-/merge_requests/25
forget_runner_result(policy.runner(ability)) if policy && ability_forgetting?
2015-04-26 12:48:37 +05:30
end
2015-09-11 14:41:01 +05:30
2017-09-10 17:25:29 +05:30
def policy_for(user, subject = :global)
2021-09-04 01:27:46 +05:30
DeclarativePolicy.policy_for(user, subject, cache: ::Gitlab::SafeRequestStore.storage)
end
# This method is something of a band-aid over the problem. The problem is
# that some conditions may not be re-entrant, if facts change.
# (`BasePolicy#admin?` is a known offender, due to the effects of
# `admin_mode`)
#
# To deal with this we need to clear two elements of state: the offending
# conditions (selected by 'pattern') and the cached ability checks (cached
# on the `policy#runner(ability)`).
#
# Clearing the conditions (see `forget_all_but`) is fairly robust, provided
# the pattern is not _under_-selective. Clearing the runners is harder,
# since there is not good way to know which abilities any given condition
# may affect. The approach taken here (see `forget_runner_result`) is to
# discard all runner results generated during a `forgetting` block. This may
# be _under_-selective if a runner prior to this block cached a state value
# that might now be invalid.
#
# TODO: add some kind of reverse-dependency mapping in DeclarativePolicy
# See: https://gitlab.com/gitlab-org/declarative-policy/-/issues/14
def forgetting(pattern, &block)
was_forgetting = ability_forgetting?
::Gitlab::SafeRequestStore[:ability_forgetting] = true
keys_before = ::Gitlab::SafeRequestStore.storage.keys
yield
ensure
::Gitlab::SafeRequestStore[:ability_forgetting] = was_forgetting
forget_all_but(keys_before, matching: pattern)
2016-08-24 12:49:21 +05:30
end
2018-03-27 19:54:05 +05:30
private
2021-09-04 01:27:46 +05:30
def ability_forgetting?
::Gitlab::SafeRequestStore[:ability_forgetting]
end
def forget_all_but(keys_before, matching:)
keys_after = ::Gitlab::SafeRequestStore.storage.keys
added_keys = keys_after - keys_before
added_keys.each do |key|
if key.is_a?(String) && key.start_with?('/dp') && key =~ matching
::Gitlab::SafeRequestStore.delete(key)
end
end
end
def forget_runner_result(runner)
# TODO: add support in DP for this
# See: https://gitlab.com/gitlab-org/declarative-policy/-/issues/15
runner.instance_variable_set(:@state, nil)
end
2018-03-27 19:54:05 +05:30
def apply_filters_if_needed(elements, user, filters)
filters.each do |ability, filter|
elements = filter.call(elements) unless allowed?(user, ability)
end
elements
end
2014-09-02 18:07:02 +05:30
end
end