1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
# frozen_string_literal: true
module Import
module SourceUsers
class ReassignService < BaseService
def initialize(import_source_user, assignee_user, current_user:)
@import_source_user = import_source_user
@current_user = current_user
@assignee_user = assignee_user
end
def execute
return error_invalid_permissions unless current_user.can?(:admin_import_source_user, import_source_user)
return error_invalid_assignee unless valid_assignee?(assignee_user)
invalid_status = false
reassign_successful = false
import_source_user.with_lock do
if import_source_user.reassignable_status?
reassign_successful = reassign_user
else
invalid_status = true
end
end
return error_invalid_status if invalid_status
if reassign_successful
send_user_reassign_email
ServiceResponse.success(payload: import_source_user)
else
ServiceResponse.error(payload: import_source_user, message: import_source_user.errors.full_messages)
end
end
private
attr_reader :assignee_user
def reassign_user
import_source_user.reassign_to_user = assignee_user
import_source_user.reassigned_by_user = current_user
import_source_user.reassign
end
def error_invalid_assignee
ServiceResponse.error(
message: invalid_assignee_message,
reason: :invalid_assignee,
payload: import_source_user
)
end
def invalid_assignee_message
if allow_mapping_to_admins?
s_('UserMapping|You can assign users with regular, auditor, or administrator access only.')
else
s_('UserMapping|You can assign only active users with regular or auditor access. ' \
'To assign users with administrator access, ask your GitLab administrator to ' \
'enable the "Allow contribution mapping to admins" setting.')
end
end
def valid_assignee?(user)
user.present? &&
user.human? &&
user.active? &&
# rubocop:disable Cop/UserAdmin -- This should not be affected by admin mode.
# We just want to know whether the user CAN have admin privileges or not.
(allow_mapping_to_admins? ? true : !user.admin?)
# rubocop:enable Cop/UserAdmin
end
def allow_mapping_to_admins?
::Gitlab::CurrentSettings.allow_contribution_mapping_to_admins?
end
end
end
end
|