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

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

214 lines
7 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class ProjectFeature < ApplicationRecord
2020-06-23 00:09:42 +05:30
include Featurable
2021-10-27 15:23:28 +05:30
extend Gitlab::ConfigHelper
2022-06-21 17:19:12 +05:30
extend ::Gitlab::Utils::Override
2016-09-29 09:46:39 +05:30
2021-04-29 21:17:54 +05:30
# When updating this array, make sure to update rubocop/cop/gitlab/feature_available_usage.rb as well.
2021-04-17 20:07:23 +05:30
FEATURES = %i[
issues
forking
merge_requests
wiki
snippets
builds
repository
pages
metrics_dashboard
analytics
2022-10-11 01:57:18 +05:30
monitor
2021-04-17 20:07:23 +05:30
operations
security_and_compliance
container_registry
2022-07-23 23:45:48 +05:30
package_registry
2022-08-27 11:52:29 +05:30
environments
feature_flags
releases
2023-01-13 00:05:48 +05:30
infrastructure
2021-04-17 20:07:23 +05:30
].freeze
2021-04-29 21:17:54 +05:30
EXPORTABLE_FEATURES = (FEATURES - [:security_and_compliance, :pages]).freeze
2020-06-23 00:09:42 +05:30
set_available_features(FEATURES)
2021-09-04 01:27:46 +05:30
PRIVATE_FEATURES_MIN_ACCESS_LEVEL = {
merge_requests: Gitlab::Access::REPORTER,
metrics_dashboard: Gitlab::Access::REPORTER,
2022-07-23 23:45:48 +05:30
container_registry: Gitlab::Access::REPORTER,
2023-05-08 21:46:49 +05:30
package_registry: Gitlab::Access::REPORTER,
environments: Gitlab::Access::REPORTER
2021-09-04 01:27:46 +05:30
}.freeze
2019-12-04 20:38:33 +05:30
PRIVATE_FEATURES_MIN_ACCESS_LEVEL_FOR_PRIVATE_PROJECT = { repository: Gitlab::Access::REPORTER }.freeze
2016-09-29 09:46:39 +05:30
2016-11-24 13:41:30 +05:30
class << self
2019-02-15 15:39:39 +05:30
def required_minimum_access_level(feature)
feature = ensure_feature!(feature)
PRIVATE_FEATURES_MIN_ACCESS_LEVEL.fetch(feature, Gitlab::Access::GUEST)
end
2019-12-04 20:38:33 +05:30
# Guest users can perform certain features on public and internal projects, but not private projects.
def required_minimum_access_level_for_private_project(feature)
feature = ensure_feature!(feature)
PRIVATE_FEATURES_MIN_ACCESS_LEVEL_FOR_PRIVATE_PROJECT.fetch(feature) do
required_minimum_access_level(feature)
end
end
2016-11-24 13:41:30 +05:30
end
2021-10-27 15:23:28 +05:30
belongs_to :project
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
validates :project, presence: true
2016-11-03 12:29:30 +05:30
validate :repository_children_level
2016-09-29 09:46:39 +05:30
2023-04-23 21:23:45 +05:30
attribute :builds_access_level, default: ENABLED
attribute :issues_access_level, default: ENABLED
attribute :forking_access_level, default: ENABLED
attribute :merge_requests_access_level, default: ENABLED
attribute :snippets_access_level, default: ENABLED
attribute :wiki_access_level, default: ENABLED
attribute :repository_access_level, default: ENABLED
attribute :analytics_access_level, default: ENABLED
attribute :metrics_dashboard_access_level, default: PRIVATE
attribute :operations_access_level, default: ENABLED
attribute :security_and_compliance_access_level, default: PRIVATE
attribute :monitor_access_level, default: ENABLED
attribute :infrastructure_access_level, default: ENABLED
attribute :feature_flags_access_level, default: ENABLED
attribute :environments_access_level, default: ENABLED
attribute :package_registry_access_level, default: -> do
2022-07-23 23:45:48 +05:30
if ::Gitlab.config.packages.enabled
ENABLED
else
DISABLED
end
end
2023-04-23 21:23:45 +05:30
attribute :container_registry_access_level, default: -> do
2021-10-27 15:23:28 +05:30
if gitlab_config_features.container_registry
ENABLED
else
DISABLED
end
end
2023-04-23 21:23:45 +05:30
after_initialize :set_pages_access_level, if: :new_record?
after_initialize :set_default_values, unless: :new_record?
2022-01-26 12:08:38 +05:30
# "enabled" here means "not disabled". It includes private features!
scope :with_feature_enabled, ->(feature) {
feature_access_level_attribute = arel_table[access_level_attribute(feature)]
enabled_feature = feature_access_level_attribute.gt(DISABLED).or(feature_access_level_attribute.eq(nil))
where(enabled_feature)
}
# Picks a feature where the level is exactly that given.
scope :with_feature_access_level, ->(feature, level) {
feature_access_level_attribute = access_level_attribute(feature)
where(project_features: { feature_access_level_attribute => level })
}
# project features may be "disabled", "internal", "enabled" or "public". If "internal",
# they are only available to team members. This scope returns features where
# the feature is either public, enabled, or internal with permission for the user.
# Note: this scope doesn't enforce that the user has access to the projects, it just checks
# that the user has access to the feature. It's important to use this scope with others
# that checks project authorizations first (e.g. `filter_by_feature_visibility`).
#
2022-05-03 16:02:30 +05:30
# This method uses an optimized version of `with_feature_access_level` for
2022-01-26 12:08:38 +05:30
# logged in users to more efficiently get private projects with the given
# feature.
def self.with_feature_available_for_user(feature, user)
visible = [ENABLED, PUBLIC]
if user&.can_read_all_resources?
with_feature_enabled(feature)
elsif user
min_access_level = required_minimum_access_level(feature)
column = quoted_access_level_column(feature)
where("#{column} IS NULL OR #{column} IN (:public_visible) OR (#{column} = :private_visible AND EXISTS (:authorizations))",
{
public_visible: visible,
private_visible: PRIVATE,
authorizations: user.authorizations_for_projects(min_access_level: min_access_level, related_project_column: 'project_features.project_id')
})
else
# This has to be added to include features whose value is nil in the db
visible << nil
with_feature_access_level(feature, visible)
end
end
2018-12-05 23:21:45 +05:30
def public_pages?
return true unless Gitlab.config.pages.access_control
2018-11-18 11:00:15 +05:30
2020-03-13 15:44:24 +05:30
return false if ::Gitlab::Pages.access_control_is_forced?
2018-12-05 23:21:45 +05:30
pages_access_level == PUBLIC || pages_access_level == ENABLED && project.public?
2018-11-18 11:00:15 +05:30
end
2019-12-04 20:38:33 +05:30
def private_pages?
!public_pages?
end
2022-07-23 23:45:48 +05:30
def package_registry_access_level=(value)
super(value).tap do
project.packages_enabled = self.package_registry_access_level != DISABLED if project
end
end
2023-05-27 22:25:52 +05:30
def public_packages?
return false unless Gitlab.config.packages.enabled
package_registry_access_level == PUBLIC || project.public?
end
2018-12-05 23:21:45 +05:30
private
2023-04-23 21:23:45 +05:30
def set_pages_access_level
self.pages_access_level ||= if ::Gitlab::Pages.access_control_is_forced?
PRIVATE
else
self.project&.public? ? ENABLED : PRIVATE
end
end
def set_default_values
self.class.column_names.each do |column_name|
next unless has_attribute?(column_name)
next unless read_attribute(column_name).nil?
write_attribute(column_name, self.class.column_defaults[column_name])
end
end
2016-11-03 12:29:30 +05:30
# Validates builds and merge requests access level
# which cannot be higher than repository access level
def repository_children_level
validator = lambda do |field|
2020-06-23 00:09:42 +05:30
level = public_send(field) || ENABLED # rubocop:disable GitlabSecurity/PublicSend
2016-11-03 12:29:30 +05:30
not_allowed = level > repository_access_level
self.errors.add(field, "cannot have higher visibility level than repository access level") if not_allowed
end
%i(merge_requests_access_level builds_access_level).each(&validator)
end
2022-06-21 17:19:12 +05:30
def feature_validation_exclusion
2022-07-23 23:45:48 +05:30
%i(pages package_registry)
2016-09-29 09:46:39 +05:30
end
2019-03-13 22:55:13 +05:30
2022-06-21 17:19:12 +05:30
override :resource_member?
def resource_member?(user, feature)
2019-03-13 22:55:13 +05:30
project.team.member?(user, ProjectFeature.required_minimum_access_level(feature))
end
2016-09-29 09:46:39 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
ProjectFeature.prepend_mod_with('ProjectFeature')