2020-07-16 15:09:38 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class NamespaceSetting < ApplicationRecord
|
2021-04-07 18:09:45 +00:00
|
|
|
include CascadingNamespaceSettingAttribute
|
|
|
|
|
|
|
|
cascading_attr :delayed_project_removal
|
|
|
|
|
2020-07-16 15:09:38 +00:00
|
|
|
belongs_to :namespace, inverse_of: :namespace_settings
|
|
|
|
|
2020-10-09 00:08:41 +00:00
|
|
|
validate :default_branch_name_content
|
2020-10-13 15:08:53 +00:00
|
|
|
validate :allow_mfa_for_group
|
2021-04-06 03:09:02 +00:00
|
|
|
validate :allow_resource_access_token_creation_for_group
|
2020-10-09 00:08:41 +00:00
|
|
|
|
2021-08-10 12:11:00 +00:00
|
|
|
before_save :set_prevent_sharing_groups_outside_hierarchy, if: -> { user_cap_enabled? }
|
|
|
|
after_save :disable_project_sharing!, if: -> { user_cap_enabled? }
|
|
|
|
|
2020-11-04 15:08:41 +00:00
|
|
|
before_validation :normalize_default_branch_name
|
|
|
|
|
2021-04-07 18:09:45 +00:00
|
|
|
NAMESPACE_SETTINGS_PARAMS = [:default_branch_name, :delayed_project_removal,
|
2021-06-15 00:10:11 +00:00
|
|
|
:lock_delayed_project_removal, :resource_access_token_creation_allowed,
|
2021-07-14 21:09:44 +00:00
|
|
|
:prevent_sharing_groups_outside_hierarchy, :new_user_signups_cap].freeze
|
2020-10-09 00:08:41 +00:00
|
|
|
|
2020-07-16 15:09:38 +00:00
|
|
|
self.primary_key = :namespace_id
|
2020-10-09 00:08:41 +00:00
|
|
|
|
2021-08-10 12:11:00 +00:00
|
|
|
def prevent_sharing_groups_outside_hierarchy
|
|
|
|
return super if namespace.root?
|
|
|
|
|
|
|
|
namespace.root_ancestor.prevent_sharing_groups_outside_hierarchy
|
|
|
|
end
|
|
|
|
|
2020-11-04 15:08:41 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def normalize_default_branch_name
|
2021-08-05 09:08:56 +00:00
|
|
|
self.default_branch_name = if default_branch_name.blank?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
Sanitize.fragment(self.default_branch_name)
|
|
|
|
end
|
2020-11-04 15:08:41 +00:00
|
|
|
end
|
|
|
|
|
2020-10-09 00:08:41 +00:00
|
|
|
def default_branch_name_content
|
|
|
|
return if default_branch_name.nil?
|
|
|
|
|
|
|
|
if default_branch_name.blank?
|
|
|
|
errors.add(:default_branch_name, "can not be an empty string")
|
|
|
|
end
|
|
|
|
end
|
2020-10-13 15:08:53 +00:00
|
|
|
|
|
|
|
def allow_mfa_for_group
|
|
|
|
if namespace&.subgroup? && allow_mfa_for_subgroups == false
|
|
|
|
errors.add(:allow_mfa_for_subgroups, _('is not allowed since the group is not top-level group.'))
|
|
|
|
end
|
|
|
|
end
|
2021-04-06 03:09:02 +00:00
|
|
|
|
|
|
|
def allow_resource_access_token_creation_for_group
|
|
|
|
if namespace&.subgroup? && !resource_access_token_creation_allowed
|
|
|
|
errors.add(:resource_access_token_creation_allowed, _('is not allowed since the group is not top-level group.'))
|
|
|
|
end
|
|
|
|
end
|
2021-08-10 12:11:00 +00:00
|
|
|
|
|
|
|
def set_prevent_sharing_groups_outside_hierarchy
|
|
|
|
self.prevent_sharing_groups_outside_hierarchy = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable_project_sharing!
|
|
|
|
namespace.update_attribute(:share_with_group_lock, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_cap_enabled?
|
|
|
|
new_user_signups_cap.present? && namespace.root?
|
|
|
|
end
|
2020-07-16 15:09:38 +00:00
|
|
|
end
|
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
NamespaceSetting.prepend_mod_with('NamespaceSetting')
|