debian-mirror-gitlab/app/services/members/create_service.rb

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

220 lines
6.3 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Members
2018-03-27 19:54:05 +05:30
class CreateService < Members::BaseService
2021-04-29 21:17:54 +05:30
BlankInvitesError = Class.new(StandardError)
TooManyInvitesError = Class.new(StandardError)
2021-11-11 11:23:49 +05:30
MembershipLockedError = Class.new(StandardError)
2021-02-11 23:33:58 +05:30
2021-04-29 21:17:54 +05:30
DEFAULT_INVITE_LIMIT = 100
2017-09-10 17:25:29 +05:30
2021-11-11 11:23:49 +05:30
attr_reader :membership_locked
2021-04-29 21:17:54 +05:30
def initialize(*args)
super
2017-09-10 17:25:29 +05:30
2021-04-29 21:17:54 +05:30
@errors = []
2022-06-21 17:19:12 +05:30
@invites = invites_from_params
2021-04-29 21:17:54 +05:30
@source = params[:source]
2022-06-21 17:19:12 +05:30
@tasks_to_be_done_members = []
2021-04-29 21:17:54 +05:30
end
def execute
2022-02-27 12:50:16 +05:30
raise Gitlab::Access::AccessDeniedError unless can?(current_user, create_member_permission(source), source)
2022-07-23 23:45:48 +05:30
# rubocop:disable Layout/EmptyLineAfterGuardClause
raise Gitlab::Access::AccessDeniedError if adding_at_least_one_owner &&
cannot_assign_owner_responsibilities_to_member_in_project?
# rubocop:enable Layout/EmptyLineAfterGuardClause
2021-09-04 01:27:46 +05:30
validate_invite_source!
2021-11-11 11:23:49 +05:30
validate_invitable!
2021-04-29 21:17:54 +05:30
add_members
2022-06-21 17:19:12 +05:30
create_tasks_to_be_done
2021-04-29 21:17:54 +05:30
enqueue_onboarding_progress_action
2022-04-04 11:22:00 +05:30
publish_event!
2021-04-29 21:17:54 +05:30
result
2021-11-11 11:23:49 +05:30
rescue BlankInvitesError, TooManyInvitesError, MembershipLockedError => e
2021-04-29 21:17:54 +05:30
error(e.message)
end
2021-11-11 11:23:49 +05:30
def single_member
members.last
end
2021-04-29 21:17:54 +05:30
private
2022-06-21 17:19:12 +05:30
attr_reader :source, :errors, :invites, :member_created_namespace_id, :members,
:tasks_to_be_done_members, :member_created_member_task_id
2021-04-29 21:17:54 +05:30
2022-07-23 23:45:48 +05:30
def adding_at_least_one_owner
params[:access_level] == Gitlab::Access::OWNER
end
def cannot_assign_owner_responsibilities_to_member_in_project?
source.is_a?(Project) && !current_user.can?(:manage_owners, source)
end
2021-04-29 21:17:54 +05:30
def invites_from_params
2022-07-16 23:28:13 +05:30
# String, Nil, Array, Integer
return params[:user_id] if params[:user_id].is_a?(Array)
return [] unless params[:user_id]
2022-06-21 17:19:12 +05:30
2022-07-16 23:28:13 +05:30
params[:user_id].to_s.split(',').uniq
2021-04-29 21:17:54 +05:30
end
2021-09-04 01:27:46 +05:30
def validate_invite_source!
raise ArgumentError, s_('AddMember|No invite source provided.') unless invite_source.present?
end
2021-11-11 11:23:49 +05:30
def validate_invitable!
2021-04-29 21:17:54 +05:30
raise BlankInvitesError, blank_invites_message if invites.blank?
return unless user_limit && invites.size > user_limit
raise TooManyInvitesError,
format(s_("AddMember|Too many users specified (limit is %{user_limit})"), user_limit: user_limit)
end
def blank_invites_message
s_('AddMember|No users specified.')
end
2017-08-17 22:00:37 +05:30
2021-04-29 21:17:54 +05:30
def add_members
2021-11-11 11:23:49 +05:30
@members = source.add_users(
2021-04-29 21:17:54 +05:30
invites,
2017-08-17 22:00:37 +05:30
params[:access_level],
expires_at: params[:expires_at],
2021-12-11 22:18:48 +05:30
current_user: current_user,
tasks_to_be_done: params[:tasks_to_be_done],
tasks_project_id: params[:tasks_project_id]
2017-08-17 22:00:37 +05:30
)
2021-04-29 21:17:54 +05:30
members.each { |member| process_result(member) }
end
2019-03-02 22:35:43 +05:30
2021-04-29 21:17:54 +05:30
def process_result(member)
2022-06-21 17:19:12 +05:30
existing_errors = member.errors.full_messages
# calling invalid? clears any errors that were added outside of the
# rails validation process
if member.invalid? || existing_errors.present?
add_error_for_member(member, existing_errors)
2021-04-29 21:17:54 +05:30
else
after_execute(member: member)
@member_created_namespace_id ||= member.namespace_id
end
2017-09-10 17:25:29 +05:30
end
2022-06-21 17:19:12 +05:30
# overridden
def add_error_for_member(member, existing_errors)
2021-04-29 21:17:54 +05:30
prefix = "#{member.user.username}: " if member.user.present?
2017-09-10 17:25:29 +05:30
2022-06-21 17:19:12 +05:30
errors << "#{prefix}#{all_member_errors(member, existing_errors).to_sentence}"
end
def all_member_errors(member, existing_errors)
existing_errors.concat(member.errors.full_messages).uniq
2021-02-11 23:33:58 +05:30
end
2021-09-04 01:27:46 +05:30
def after_execute(member:)
super
2022-06-21 17:19:12 +05:30
build_tasks_to_be_done_members(member)
2021-10-27 15:23:28 +05:30
track_invite_source(member)
end
def track_invite_source(member)
2022-06-21 17:19:12 +05:30
Gitlab::Tracking.event(self.class.name,
'create_member',
label: invite_source,
property: tracking_property(member),
user: current_user)
2021-09-04 01:27:46 +05:30
end
def invite_source
params[:invite_source]
end
def tracking_property(member)
# ideally invites go down the invite service class instead, but there is nothing that limits an invite
# from being used in this class and if you send emails as a comma separated list to the api/members
# endpoint, it will support invites
member.invite? ? 'net_new_user' : 'existing_user'
end
2022-06-21 17:19:12 +05:30
def build_tasks_to_be_done_members(member)
return unless tasks_to_be_done?(member)
2021-12-11 22:18:48 +05:30
2022-06-21 17:19:12 +05:30
@tasks_to_be_done_members << member
2021-12-11 22:18:48 +05:30
# We can take the first `member_task` here, since all tasks will have the same attributes needed
# for the `TasksToBeDone::CreateWorker`, ie. `project` and `tasks_to_be_done`.
2022-06-21 17:19:12 +05:30
@member_created_member_task_id ||= member.member_task.id
end
def tasks_to_be_done?(member)
return false if params[:tasks_to_be_done].blank? || params[:tasks_project_id].blank?
# Only create task issues for existing users. Tasks for new users are created when they signup.
member.member_task&.valid? && member.user.present?
end
def create_tasks_to_be_done
return unless member_created_member_task_id # signal if there is any work to be done here
TasksToBeDone::CreateWorker.perform_async(member_created_member_task_id,
current_user.id,
tasks_to_be_done_members.map(&:user_id))
2021-12-11 22:18:48 +05:30
end
2017-09-10 17:25:29 +05:30
def user_limit
2021-04-29 21:17:54 +05:30
limit = params.fetch(:limit, DEFAULT_INVITE_LIMIT)
2017-09-10 17:25:29 +05:30
limit && limit < 0 ? nil : limit
2017-08-17 22:00:37 +05:30
end
2021-02-22 17:27:13 +05:30
2021-04-29 21:17:54 +05:30
def enqueue_onboarding_progress_action
return unless member_created_namespace_id
Namespaces::OnboardingUserAddedWorker.perform_async(member_created_namespace_id)
end
def result
if errors.any?
error(formatted_errors)
else
success
end
end
def formatted_errors
errors.to_sentence
2021-02-22 17:27:13 +05:30
end
2022-02-27 12:50:16 +05:30
2022-04-04 11:22:00 +05:30
def publish_event!
Gitlab::EventStore.publish(
Members::MembersAddedEvent.new(data: {
source_id: source.id,
source_type: source.class.name
})
)
end
2022-02-27 12:50:16 +05:30
def create_member_permission(source)
case source
when Group
:admin_group_member
when Project
:admin_project_member
else
raise "Unknown source type: #{source.class}!"
end
end
2017-08-17 22:00:37 +05:30
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Members::CreateService.prepend_mod_with('Members::CreateService')