2018-07-16 16:31:01 +00:00
# frozen_string_literal: true
2016-03-09 00:01:33 +00:00
module Groups
class UpdateService < Groups :: BaseService
2017-09-02 06:59:24 +00:00
include UpdateVisibilityLevel
2016-03-09 00:01:33 +00:00
def execute
2017-03-28 11:09:44 +00:00
reject_parent_id!
2017-09-06 00:10:30 +00:00
return false unless valid_visibility_level_change? ( group , params [ :visibility_level ] )
2016-03-09 00:01:33 +00:00
2017-09-02 01:00:46 +00:00
return false unless valid_share_with_group_lock_change?
2016-03-20 20:03:53 +00:00
group . assign_attributes ( params )
2016-03-09 00:01:33 +00:00
2016-12-20 16:52:27 +00:00
begin
2018-08-02 14:16:58 +00:00
after_update if group . save
true
2016-12-20 16:52:27 +00:00
rescue Gitlab :: UpdatePathError = > e
group . errors . add ( :base , e . message )
false
end
2016-03-09 00:01:33 +00:00
end
2017-03-28 11:09:44 +00:00
private
2018-08-02 14:16:58 +00:00
def after_update
if group . previous_changes . include? ( :visibility_level ) && group . private?
# don't enqueue immediately to prevent todos removal in case of a mistake
TodosDestroyer :: GroupPrivateWorker . perform_in ( 1 . hour , group . id )
end
end
2017-03-28 11:09:44 +00:00
def reject_parent_id!
params . except! ( :parent_id )
end
2017-09-02 01:00:46 +00:00
def valid_share_with_group_lock_change?
return true unless changing_share_with_group_lock?
return true if can? ( current_user , :change_share_with_group_lock , group )
2017-09-06 18:31:45 +00:00
group . errors . add ( :share_with_group_lock , s_ ( 'GroupSettings|cannot be disabled when the parent group "Share with group lock" is enabled, except by the owner of the parent group' ) )
2017-09-02 01:00:46 +00:00
false
end
def changing_share_with_group_lock?
return false if params [ :share_with_group_lock ] . nil?
params [ :share_with_group_lock ] != group . share_with_group_lock
end
2016-03-09 00:01:33 +00:00
end
end