2017-04-03 10:17:24 -04:00
|
|
|
module ProtectedRef
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
belongs_to :project
|
2017-04-05 13:59:46 -04:00
|
|
|
|
2017-04-03 10:17:24 -04:00
|
|
|
validates :name, presence: true
|
|
|
|
validates :project, presence: true
|
|
|
|
|
2017-04-03 14:48:00 -04:00
|
|
|
delegate :matching, :matches?, :wildcard?, to: :ref_matcher
|
|
|
|
|
2017-04-03 22:50:15 -04:00
|
|
|
def self.protected_ref_accessible_to?(ref, user, action:)
|
2017-04-03 17:04:37 -04:00
|
|
|
access_levels_for_ref(ref, action: action).any? do |access_level|
|
2017-04-03 10:17:24 -04:00
|
|
|
access_level.check_access(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-03 20:39:34 -04:00
|
|
|
def self.developers_can?(action, ref)
|
|
|
|
access_levels_for_ref(ref, action: action).any? do |access_level|
|
|
|
|
access_level.access_level == Gitlab::Access::DEVELOPER
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-03 22:50:15 -04:00
|
|
|
def self.access_levels_for_ref(ref, action:)
|
2017-04-03 17:04:37 -04:00
|
|
|
self.matching(ref).map(&:"#{action}_access_levels").flatten
|
2017-04-03 10:17:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.matching(ref_name, protected_refs: nil)
|
|
|
|
ProtectedRefMatcher.matching(self, ref_name, protected_refs: protected_refs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit
|
|
|
|
project.commit(self.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ref_matcher
|
|
|
|
@ref_matcher ||= ProtectedRefMatcher.new(self)
|
|
|
|
end
|
|
|
|
end
|