debian-mirror-gitlab/app/services/namespace_settings/update_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.9 KiB
Ruby
Raw Normal View History

2021-01-03 14:25:43 +05:30
# frozen_string_literal: true
module NamespaceSettings
class UpdateService
include ::Gitlab::Allowable
attr_reader :current_user, :group, :settings_params
def initialize(current_user, group, settings)
@current_user = current_user
@group = group
@settings_params = settings
end
def execute
2021-04-29 21:17:54 +05:30
validate_resource_access_token_creation_allowed_param
2021-09-30 23:02:18 +05:30
validate_settings_param_for_root_group(
param_key: :prevent_sharing_groups_outside_hierarchy,
user_policy: :change_prevent_sharing_groups_outside_hierarchy
)
validate_settings_param_for_root_group(
param_key: :new_user_signups_cap,
user_policy: :change_new_user_signups_cap
)
2021-04-29 21:17:54 +05:30
2021-01-03 14:25:43 +05:30
if group.namespace_settings
group.namespace_settings.attributes = settings_params
else
group.build_namespace_settings(settings_params)
end
end
2021-04-29 21:17:54 +05:30
private
def validate_resource_access_token_creation_allowed_param
return if settings_params[:resource_access_token_creation_allowed].nil?
unless can?(current_user, :admin_group, group)
settings_params.delete(:resource_access_token_creation_allowed)
group.namespace_settings.errors.add(:resource_access_token_creation_allowed, _('can only be changed by a group admin.'))
end
end
2021-09-04 01:27:46 +05:30
2021-09-30 23:02:18 +05:30
def validate_settings_param_for_root_group(param_key:, user_policy:)
return if settings_params[param_key].nil?
unless can?(current_user, user_policy, group)
settings_params.delete(param_key)
group.namespace_settings.errors.add(param_key, _('can only be changed by a group admin.'))
end
2021-09-04 01:27:46 +05:30
2021-09-30 23:02:18 +05:30
unless group.root?
settings_params.delete(param_key)
group.namespace_settings.errors.add(param_key, _('only available on top-level groups.'))
2021-09-04 01:27:46 +05:30
end
end
2021-01-03 14:25:43 +05:30
end
end
2021-06-08 01:23:25 +05:30
NamespaceSettings::UpdateService.prepend_mod_with('NamespaceSettings::UpdateService')