Read about our upcoming Code of Conduct on this issue

project_feature.rb 3.15 KB
Newer Older
Felipe Artur's avatar
Felipe Artur committed
1
2
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
  # Permission levels
Felipe Artur's avatar
Felipe Artur committed
17
18
19
20
  DISABLED = 0
  PRIVATE  = 10
  ENABLED  = 20

Douwe Maan's avatar
Douwe Maan committed
21
  FEATURES = %i(issues merge_requests wiki snippets builds repository).freeze
Felipe Artur's avatar
Felipe Artur committed
22

23
24
25
26
27
28
29
  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
30
31
32
33
34
35
36

    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
37
38
  end

39
40
41
42
  # 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 Artur's avatar
Felipe Artur committed
43

44
45
  validates :project, presence: true

46
47
  validate :repository_children_level

48
49
50
51
52
  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
53
  default_value_for :repository_access_level,     value: ENABLED, allows_nil: false
54

Felipe Artur's avatar
Felipe Artur committed
55
  def feature_available?(feature, user)
56
57
58
59
    get_permission(user, access_level(feature))
  end

  def access_level(feature)
60
    public_send(ProjectFeature.access_level_attribute(feature)) # rubocop:disable GitlabSecurity/PublicSend
Felipe Artur's avatar
Felipe Artur committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  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

75
76
77
78
  def issues_enabled?
    issues_access_level > DISABLED
  end

Felipe Artur's avatar
Felipe Artur committed
79
80
  private

81
82
83
84
  # Validates builds and merge requests access level
  # which cannot be higher than repository access level
  def repository_children_level
    validator = lambda do |field|
85
      level = public_send(field) || ProjectFeature::ENABLED # rubocop:disable GitlabSecurity/PublicSend
86
87
88
89
90
91
92
      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

Felipe Artur's avatar
Felipe Artur committed
93
94
95
96
97
  def get_permission(user, level)
    case level
    when DISABLED
      false
    when PRIVATE
98
      user && (project.team.member?(user) || user.full_private_access?)
Felipe Artur's avatar
Felipe Artur committed
99
100
101
102
103
104
105
    when ENABLED
      true
    else
      true
    end
  end
end