Commit 966edcc7 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Merge branch 'kerrizor/create-foss-namespace_settings_update_service' into 'master'

Create basic non-EE NamespaceSettings::UpdateService

See merge request gitlab-org/gitlab!43308
parents f6ba3d41 e490f69b
# 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
if group.namespace_settings
group.namespace_settings.attributes = settings_params
else
group.build_namespace_settings(settings_params)
end
end
end
end
NamespaceSettings::UpdateService.prepend_if_ee('EE::NamespaceSettings::UpdateService')
...@@ -116,7 +116,7 @@ module EE ...@@ -116,7 +116,7 @@ module EE
settings_params = params.slice(:prevent_forking_outside_group) settings_params = params.slice(:prevent_forking_outside_group)
params.delete(:prevent_forking_outside_group) params.delete(:prevent_forking_outside_group)
NamespaceSettings::UpdateService.new(current_user, group, settings_params).execute ::NamespaceSettings::UpdateService.new(current_user, group, settings_params).execute
end end
def log_audit_event def log_audit_event
......
...@@ -2,44 +2,37 @@ ...@@ -2,44 +2,37 @@
module EE module EE
# This class is responsible for updating the namespace settings of a specific group. # This class is responsible for updating the namespace settings of a specific group.
#
module NamespaceSettings module NamespaceSettings
class UpdateService module UpdateService
include ::Gitlab::Allowable extend ::Gitlab::Utils::Override
def initialize(current_user, group, settings)
@current_user = current_user
@group = group
@settings_params = settings
end
override :execute
def execute def execute
unless valid? unless can_update_prevent_forking?
group.errors.add(:prevent_forking_outside_group, s_('GroupSettings|Prevent forking setting was not saved')) group.errors.add(
:prevent_forking_outside_group,
s_('GroupSettings|Prevent forking setting was not saved')
)
return return
end end
if group.namespace_settings super
group.namespace_settings.attributes = settings_params
else
group.build_namespace_settings(settings_params)
end
end end
private private
attr_reader :current_user, :group, :settings_params def can_update_prevent_forking?
return true unless settings_params.key?(:prevent_forking_outside_group)
def valid? if can?(current_user, :change_prevent_group_forking, group)
if settings_params.key?(:prevent_forking_outside_group)
can_update_prevent_forking?
else
true true
end else
end settings_params.delete(:prevent_forking_outside_group)
def can_update_prevent_forking? false
can?(current_user, :change_prevent_group_forking, group) end
end end
end end
end end
......
...@@ -6,7 +6,7 @@ RSpec.describe EE::NamespaceSettings::UpdateService do ...@@ -6,7 +6,7 @@ RSpec.describe EE::NamespaceSettings::UpdateService do
let(:group) { create(:group) } let(:group) { create(:group) }
let(:user) { create(:user) } let(:user) { create(:user) }
subject { described_class.new(user, group, params).execute } subject { NamespaceSettings::UpdateService.new(user, group, params).execute }
describe '#execute' do describe '#execute' do
before do before do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe NamespaceSettings::UpdateService do
let(:user) { create(:user) }
let(:group) { create(:group) }
let(:settings) { {} }
subject(:service) { described_class.new(user, group, settings) }
describe "#execute" do
context "group has no namespace_settings" do
it "builds out a new namespace_settings record" do
expect do
service.execute
end.to change { NamespaceSetting.count }.by(1)
end
end
context "group has a namespace_settings" do
before do
create(:namespace_settings, namespace: group)
service.execute
end
it "doesn't create a new namespace_setting record" do
expect do
service.execute
end.not_to change { NamespaceSetting.count }
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment