2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-01 22:31:21 +00:00
|
|
|
class ProjectFeature < ActiveRecord::Base
|
|
|
|
# == Project features permissions
|
|
|
|
#
|
|
|
|
# Grants access level to project tools
|
|
|
|
#
|
|
|
|
# Tools can be enabled only for users, everyone or disabled
|
|
|
|
# Access control is made only for non private projects
|
|
|
|
#
|
|
|
|
# levels:
|
|
|
|
#
|
|
|
|
# Disabled: not enabled for anyone
|
|
|
|
# Private: enabled only for team members
|
|
|
|
# Enabled: enabled for everyone able to access the project
|
|
|
|
#
|
|
|
|
|
2016-09-16 19:15:39 +00:00
|
|
|
# Permission levels
|
2016-08-01 22:31:21 +00:00
|
|
|
DISABLED = 0
|
|
|
|
PRIVATE = 10
|
|
|
|
ENABLED = 20
|
|
|
|
|
2017-02-21 23:32:18 +00:00
|
|
|
FEATURES = %i(issues merge_requests wiki snippets builds repository).freeze
|
2016-08-01 22:31:21 +00:00
|
|
|
|
2016-10-26 17:34:06 +00:00
|
|
|
class << self
|
|
|
|
def access_level_attribute(feature)
|
|
|
|
feature = feature.model_name.plural.to_sym if feature.respond_to?(:model_name)
|
|
|
|
raise ArgumentError, "invalid project feature: #{feature}" unless FEATURES.include?(feature)
|
|
|
|
|
|
|
|
"#{feature}_access_level".to_sym
|
|
|
|
end
|
2017-06-14 12:31:20 +00:00
|
|
|
|
|
|
|
def quoted_access_level_column(feature)
|
|
|
|
attribute = connection.quote_column_name(access_level_attribute(feature))
|
|
|
|
table = connection.quote_table_name(table_name)
|
|
|
|
|
|
|
|
"#{table}.#{attribute}"
|
|
|
|
end
|
2016-10-26 17:34:06 +00:00
|
|
|
end
|
|
|
|
|
2016-10-05 03:53:15 +00:00
|
|
|
# Default scopes force us to unscope here since a service may need to check
|
|
|
|
# permissions for a project in pending_delete
|
|
|
|
# http://stackoverflow.com/questions/1540645/how-to-disable-default-scope-for-a-belongs-to
|
|
|
|
belongs_to :project, -> { unscope(where: :pending_delete) }
|
2016-08-01 22:31:21 +00:00
|
|
|
|
2017-09-12 21:07:11 +00:00
|
|
|
validates :project, presence: true
|
|
|
|
|
2016-09-16 19:15:39 +00:00
|
|
|
validate :repository_children_level
|
|
|
|
|
2016-09-21 10:10:27 +00:00
|
|
|
default_value_for :builds_access_level, value: ENABLED, allows_nil: false
|
|
|
|
default_value_for :issues_access_level, value: ENABLED, allows_nil: false
|
|
|
|
default_value_for :merge_requests_access_level, value: ENABLED, allows_nil: false
|
|
|
|
default_value_for :snippets_access_level, value: ENABLED, allows_nil: false
|
|
|
|
default_value_for :wiki_access_level, value: ENABLED, allows_nil: false
|
2016-09-16 19:15:39 +00:00
|
|
|
default_value_for :repository_access_level, value: ENABLED, allows_nil: false
|
2016-09-21 10:10:27 +00:00
|
|
|
|
2016-08-01 22:31:21 +00:00
|
|
|
def feature_available?(feature, user)
|
2018-09-21 20:43:24 +00:00
|
|
|
# This feature might not be behind a feature flag at all, so default to true
|
|
|
|
return false unless ::Feature.enabled?(feature, user, default_enabled: true)
|
|
|
|
|
2017-04-06 21:06:42 +00:00
|
|
|
get_permission(user, access_level(feature))
|
|
|
|
end
|
|
|
|
|
|
|
|
def access_level(feature)
|
2017-08-04 02:20:34 +00:00
|
|
|
public_send(ProjectFeature.access_level_attribute(feature)) # rubocop:disable GitlabSecurity/PublicSend
|
2016-08-01 22:31:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def builds_enabled?
|
|
|
|
builds_access_level > DISABLED
|
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_enabled?
|
|
|
|
wiki_access_level > DISABLED
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_requests_enabled?
|
|
|
|
merge_requests_access_level > DISABLED
|
|
|
|
end
|
|
|
|
|
2016-11-12 10:17:24 +00:00
|
|
|
def issues_enabled?
|
|
|
|
issues_access_level > DISABLED
|
|
|
|
end
|
|
|
|
|
2016-08-01 22:31:21 +00:00
|
|
|
private
|
|
|
|
|
2016-09-16 19:15:39 +00:00
|
|
|
# Validates builds and merge requests access level
|
|
|
|
# which cannot be higher than repository access level
|
|
|
|
def repository_children_level
|
|
|
|
validator = lambda do |field|
|
2017-08-04 02:20:34 +00:00
|
|
|
level = public_send(field) || ProjectFeature::ENABLED # rubocop:disable GitlabSecurity/PublicSend
|
2016-09-16 19:15:39 +00:00
|
|
|
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
|
|
|
|
|
2016-08-01 22:31:21 +00:00
|
|
|
def get_permission(user, level)
|
|
|
|
case level
|
|
|
|
when DISABLED
|
|
|
|
false
|
|
|
|
when PRIVATE
|
2017-06-22 06:35:49 +00:00
|
|
|
user && (project.team.member?(user) || user.full_private_access?)
|
2016-08-01 22:31:21 +00:00
|
|
|
when ENABLED
|
|
|
|
true
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|