2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module ProtectedRefAccess
|
|
|
|
extend ActiveSupport::Concern
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
class_methods do
|
2023-07-09 08:55:56 +05:30
|
|
|
def human_access_levels
|
|
|
|
{
|
|
|
|
Gitlab::Access::DEVELOPER => 'Developers + Maintainers',
|
|
|
|
Gitlab::Access::MAINTAINER => 'Maintainers',
|
|
|
|
Gitlab::Access::ADMIN => 'Instance admins',
|
|
|
|
Gitlab::Access::NO_ACCESS => 'No one'
|
|
|
|
}.slice(*allowed_access_levels)
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def allowed_access_levels
|
2023-07-09 08:55:56 +05:30
|
|
|
levels = [
|
2018-12-05 23:21:45 +05:30
|
|
|
Gitlab::Access::DEVELOPER,
|
2023-07-09 08:55:56 +05:30
|
|
|
Gitlab::Access::MAINTAINER,
|
|
|
|
Gitlab::Access::ADMIN,
|
2018-12-05 23:21:45 +05:30
|
|
|
Gitlab::Access::NO_ACCESS
|
|
|
|
]
|
2023-07-09 08:55:56 +05:30
|
|
|
|
|
|
|
return levels unless Gitlab.com?
|
|
|
|
|
|
|
|
levels.excluding(Gitlab::Access::ADMIN)
|
|
|
|
end
|
|
|
|
|
|
|
|
def humanize(access_level)
|
|
|
|
human_access_levels[access_level]
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
included do
|
2018-11-18 11:00:15 +05:30
|
|
|
scope :maintainer, -> { where(access_level: Gitlab::Access::MAINTAINER) }
|
2017-08-17 22:00:37 +05:30
|
|
|
scope :developer, -> { where(access_level: Gitlab::Access::DEVELOPER) }
|
2018-11-18 11:00:15 +05:30
|
|
|
scope :for_role, -> { where(user_id: nil, group_id: nil) }
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
validates :access_level, presence: true, if: :role?, inclusion: { in: allowed_access_levels }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def humanize
|
2023-07-09 08:55:56 +05:30
|
|
|
self.class.humanize(access_level)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def type
|
|
|
|
:role
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def role?
|
2021-01-29 00:20:46 +05:30
|
|
|
type == :role
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
def check_access(current_user)
|
|
|
|
return false if current_user.nil? || no_access?
|
|
|
|
return current_user.admin? if admin_access?
|
|
|
|
|
|
|
|
yield if block_given?
|
|
|
|
|
|
|
|
user_can_access?(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def admin_access?
|
|
|
|
role? && access_level == ::Gitlab::Access::ADMIN
|
|
|
|
end
|
|
|
|
|
|
|
|
def no_access?
|
|
|
|
role? && access_level == Gitlab::Access::NO_ACCESS
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
def user_can_access?(current_user)
|
|
|
|
current_user.can?(:push_code, project) &&
|
|
|
|
project.team.max_member_access(current_user.id) >= access_level
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
ProtectedRefAccess.include_mod_with('ProtectedRefAccess::Scopes')
|
|
|
|
ProtectedRefAccess.prepend_mod_with('ProtectedRefAccess')
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
# 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.
|
2021-06-08 01:23:25 +05:30
|
|
|
ProtectedRefAccess::ClassMethods.prepend_mod_with('ProtectedRefAccess::ClassMethods')
|