2018-08-03 17:22:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-04 01:59:37 +00:00
|
|
|
module ProtectedRefAccess
|
|
|
|
extend ActiveSupport::Concern
|
2017-11-24 12:43:02 +00:00
|
|
|
HUMAN_ACCESS_LEVELS = {
|
2019-08-31 19:57:00 +00:00
|
|
|
Gitlab::Access::MAINTAINER => "Maintainers",
|
|
|
|
Gitlab::Access::DEVELOPER => "Developers + Maintainers",
|
|
|
|
Gitlab::Access::NO_ACCESS => "No one"
|
2017-11-24 12:43:02 +00:00
|
|
|
}.freeze
|
|
|
|
|
2018-08-28 16:30:38 +00:00
|
|
|
class_methods do
|
|
|
|
def allowed_access_levels
|
|
|
|
[
|
|
|
|
Gitlab::Access::MAINTAINER,
|
|
|
|
Gitlab::Access::DEVELOPER,
|
|
|
|
Gitlab::Access::NO_ACCESS
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-04 01:59:37 +00:00
|
|
|
included do
|
2018-07-11 14:36:08 +00:00
|
|
|
scope :master, -> { maintainer } # @deprecated
|
|
|
|
scope :maintainer, -> { where(access_level: Gitlab::Access::MAINTAINER) }
|
2017-04-04 01:59:37 +00:00
|
|
|
scope :developer, -> { where(access_level: Gitlab::Access::DEVELOPER) }
|
2018-06-14 13:47:49 +00:00
|
|
|
scope :by_user, -> (user) { where(user_id: user ) }
|
|
|
|
scope :by_group, -> (group) { where(group_id: group ) }
|
|
|
|
scope :for_role, -> { where(user_id: nil, group_id: nil) }
|
|
|
|
scope :for_user, -> { where.not(user_id: nil) }
|
|
|
|
scope :for_group, -> { where.not(group_id: nil) }
|
2017-11-24 12:41:36 +00:00
|
|
|
|
|
|
|
validates :access_level, presence: true, if: :role?, inclusion: {
|
2018-08-28 16:30:38 +00:00
|
|
|
in: self.allowed_access_levels
|
2017-11-24 12:41:36 +00:00
|
|
|
}
|
2017-04-04 01:59:37 +00:00
|
|
|
end
|
2017-03-15 22:29:07 +00:00
|
|
|
|
2017-04-04 01:59:37 +00:00
|
|
|
def humanize
|
2017-11-24 12:43:02 +00:00
|
|
|
HUMAN_ACCESS_LEVELS[self.access_level]
|
2017-04-04 01:59:37 +00:00
|
|
|
end
|
2017-03-15 22:29:07 +00:00
|
|
|
|
2017-11-24 12:41:36 +00:00
|
|
|
# CE access levels are always role-based,
|
|
|
|
# where as EE allows groups and users too
|
|
|
|
def role?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-04-04 01:59:37 +00:00
|
|
|
def check_access(user)
|
2017-04-07 00:14:10 +00:00
|
|
|
return true if user.admin?
|
2017-03-15 22:29:07 +00:00
|
|
|
|
2017-12-11 14:21:06 +00:00
|
|
|
user.can?(:push_code, project) &&
|
|
|
|
project.team.max_member_access(user.id) >= access_level
|
2017-04-04 01:59:37 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
ProtectedRefAccess.include_if_ee('EE::ProtectedRefAccess::Scopes') # rubocop: disable Cop/InjectEnterpriseEditionModule
|
|
|
|
ProtectedRefAccess.prepend_if_ee('EE::ProtectedRefAccess') # rubocop: disable Cop/InjectEnterpriseEditionModule
|
|
|
|
|
|
|
|
# When using `prepend` (or `include` for that matter), the `ClassMethods`
|
|
|
|
# constants are not merged. This means that `class_methods` in
|
|
|
|
# `EE::ProtectedRefAccess` would be ignored.
|
|
|
|
#
|
|
|
|
# To work around this, we prepend the `ClassMethods` constant manually.
|
|
|
|
ProtectedRefAccess::ClassMethods.prepend_if_ee('EE::ProtectedRefAccess::ClassMethods')
|