project_feature.rb 4.68 KB
Newer Older
1 2
# frozen_string_literal: true

Felipe's avatar
Felipe committed
3 4 5 6 7 8 9 10 11 12 13 14 15
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
16
  # Public:   enabled for everyone (only allowed for pages)
Felipe's avatar
Felipe committed
17 18
  #

19
  # Permission levels
Felipe's avatar
Felipe committed
20 21 22
  DISABLED = 0
  PRIVATE  = 10
  ENABLED  = 20
23
  PUBLIC   = 30
Felipe's avatar
Felipe committed
24

25
  FEATURES = %i(issues merge_requests wiki snippets builds repository pages).freeze
26
  PRIVATE_FEATURES_MIN_ACCESS_LEVEL = { merge_requests: Gitlab::Access::REPORTER }.freeze
Felipe's avatar
Felipe committed
27

28 29
  class << self
    def access_level_attribute(feature)
30
      feature = ensure_feature!(feature)
31 32 33

      "#{feature}_access_level".to_sym
    end
34 35 36 37 38 39 40

    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
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

    def required_minimum_access_level(feature)
      feature = ensure_feature!(feature)

      PRIVATE_FEATURES_MIN_ACCESS_LEVEL.fetch(feature, Gitlab::Access::GUEST)
    end

    private

    def ensure_feature!(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
    end
56 57
  end

58 59 60 61
  # 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) }
Felipe's avatar
Felipe committed
62

63 64
  validates :project, presence: true

65
  validate :repository_children_level
66
  validate :allowed_access_levels
67

68 69 70 71 72
  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
73
  default_value_for :repository_access_level,     value: ENABLED, allows_nil: false
74

Felipe's avatar
Felipe committed
75
  def feature_available?(feature, user)
76 77 78
    # 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)

79
    get_permission(user, feature)
80 81 82
  end

  def access_level(feature)
83
    public_send(ProjectFeature.access_level_attribute(feature)) # rubocop:disable GitlabSecurity/PublicSend
Felipe's avatar
Felipe committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97
  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

98 99 100 101
  def issues_enabled?
    issues_access_level > DISABLED
  end

102 103 104 105 106 107 108 109 110 111
  def pages_enabled?
    pages_access_level > DISABLED
  end

  def public_pages?
    return true unless Gitlab.config.pages.access_control

    pages_access_level == PUBLIC || pages_access_level == ENABLED && project.public?
  end

Felipe's avatar
Felipe committed
112 113
  private

114 115 116 117
  # Validates builds and merge requests access level
  # which cannot be higher than repository access level
  def repository_children_level
    validator = lambda do |field|
118
      level = public_send(field) || ProjectFeature::ENABLED # rubocop:disable GitlabSecurity/PublicSend
119 120 121 122 123 124 125
      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

126 127 128 129 130 131 132 133 134 135 136
  # Validates access level for other than pages cannot be PUBLIC
  def allowed_access_levels
    validator = lambda do |field|
      level = public_send(field) || ProjectFeature::ENABLED # rubocop:disable GitlabSecurity/PublicSend
      not_allowed = level > ProjectFeature::ENABLED
      self.errors.add(field, "cannot have public visibility level") if not_allowed
    end

    (FEATURES - %i(pages)).each {|f| validator.call("#{f}_access_level")}
  end

137 138
  def get_permission(user, feature)
    case access_level(feature)
Felipe's avatar
Felipe committed
139 140 141
    when DISABLED
      false
    when PRIVATE
142
      team_access?(user, feature)
Felipe's avatar
Felipe committed
143 144
    when ENABLED
      true
145 146
    when PUBLIC
      true
Felipe's avatar
Felipe committed
147 148 149 150
    else
      true
    end
  end
151 152 153 154 155 156 157

  def team_access?(user, feature)
    return unless user
    return true if user.full_private_access?

    project.team.member?(user, ProjectFeature.required_minimum_access_level(feature))
  end
Felipe's avatar
Felipe committed
158
end