Commit b94c2151 authored by Thong Kuah's avatar Thong Kuah

Merge branch...

Merge branch '350999-deprecate-use-of-members-api-for-invite-modal-in-favor-of-invitations-api-7' into 'master'

Update Invitations Service to accept users

See merge request gitlab-org/gitlab!83534
parents 1165c5f5 07422cb2
...@@ -14,7 +14,7 @@ module Members ...@@ -14,7 +14,7 @@ module Members
super super
@errors = [] @errors = []
@invites = invites_from_params&.split(',')&.uniq&.flatten @invites = invites_from_params
@source = params[:source] @source = params[:source]
end end
...@@ -43,7 +43,9 @@ module Members ...@@ -43,7 +43,9 @@ module Members
attr_reader :source, :errors, :invites, :member_created_namespace_id, :members attr_reader :source, :errors, :invites, :member_created_namespace_id, :members
def invites_from_params def invites_from_params
params[:user_ids] return params[:user_ids] if params[:user_ids].is_a?(Array)
params[:user_ids]&.to_s&.split(',')&.uniq&.flatten || []
end end
def validate_invite_source! def validate_invite_source!
...@@ -87,6 +89,7 @@ module Members ...@@ -87,6 +89,7 @@ module Members
end end
end end
# overridden
def add_error_for_member(member) def add_error_for_member(member)
prefix = "#{member.user.username}: " if member.user.present? prefix = "#{member.user.username}: " if member.user.present?
...@@ -117,7 +120,8 @@ module Members ...@@ -117,7 +120,8 @@ module Members
def create_tasks_to_be_done def create_tasks_to_be_done
return if params[:tasks_to_be_done].blank? || params[:tasks_project_id].blank? return if params[:tasks_to_be_done].blank? || params[:tasks_project_id].blank?
valid_members = members.select { |member| member.valid? && member.member_task.valid? } # Only create task issues for existing users. Tasks for new users are created when they signup.
valid_members = members.select { |member| member.valid? && member.member_task.valid? && member.user.present? }
return unless valid_members.present? return unless valid_members.present?
# We can take the first `member_task` here, since all tasks will have the same attributes needed # We can take the first `member_task` here, since all tasks will have the same attributes needed
......
...@@ -7,6 +7,8 @@ module Members ...@@ -7,6 +7,8 @@ module Members
def initialize(*args) def initialize(*args)
super super
@invites += parsed_emails
@errors = {} @errors = {}
end end
...@@ -14,38 +16,63 @@ module Members ...@@ -14,38 +16,63 @@ module Members
alias_method :formatted_errors, :errors alias_method :formatted_errors, :errors
def invites_from_params def parsed_emails
params[:email] # can't put this in the initializer since `invites_from_params` is called in super class
# and needs it
@parsed_emails ||= (formatted_param(params[:email]) || [])
end
def formatted_param(parameter)
parameter&.split(',')&.uniq&.flatten
end end
def validate_invitable! def validate_invitable!
super super
return if params[:email].blank?
# we need the below due to add_users hitting Members::CreatorService.parse_users_list and ignoring invalid emails # we need the below due to add_users hitting Members::CreatorService.parse_users_list and ignoring invalid emails
# ideally we wouldn't need this, but we can't really change the add_users method # ideally we wouldn't need this, but we can't really change the add_users method
valid, invalid = invites.partition { |email| Member.valid_email?(email) } invalid_emails.each { |email| errors[email] = s_('AddMember|Invite email is invalid') }
@invites = valid end
def invalid_emails
parsed_emails.each_with_object([]) do |email, invalid|
next if Member.valid_email?(email)
invalid.each { |email| errors[email] = s_('AddMember|Invite email is invalid') } invalid << email
@invites.delete(email)
end
end end
override :blank_invites_message override :blank_invites_message
def blank_invites_message def blank_invites_message
s_('AddMember|Emails cannot be blank') s_('AddMember|Invites cannot be blank')
end end
override :add_error_for_member override :add_error_for_member
def add_error_for_member(member) def add_error_for_member(member)
errors[invite_email(member)] = member.errors.full_messages.to_sentence errors[invited_object(member)] = member.errors.full_messages.to_sentence
end end
override :create_tasks_to_be_done def invited_object(member)
def create_tasks_to_be_done return member.invite_email if member.invite_email
# Only create task issues for existing users. Tasks for new users are created when they signup.
end
def invite_email(member) # There is a case where someone was invited by email, but the `user` record exists.
member.invite_email || member.user.email # The member record returned will not have an invite_email attribute defined since
# the CreatorService finds `user` record sometimes by email.
# At that point we lose the info of whether this invite was done by `user` or by email.
# Here we will give preference to check invites by user_id first.
# There is also a case where a user could be invited by their email and
# at the same time via the API in the same request.
# This would would mean the same user is invited as user_id and email.
# However, that isn't as likely from the UI at least since the token generator checks
# for that case and doesn't allow email being used if the user exists as a record already.
if member.user_id.to_s.in?(invites)
member.user.username
else
member.user.all_emails.detect { |email| email.in?(invites) }
end
end end
end end
end end
...@@ -2334,15 +2334,15 @@ msgstr "" ...@@ -2334,15 +2334,15 @@ msgstr ""
msgid "AddContextCommits|Add/remove" msgid "AddContextCommits|Add/remove"
msgstr "" msgstr ""
msgid "AddMember|Emails cannot be blank"
msgstr ""
msgid "AddMember|Invite email is invalid" msgid "AddMember|Invite email is invalid"
msgstr "" msgstr ""
msgid "AddMember|Invite limit of %{daily_invites} per day exceeded" msgid "AddMember|Invite limit of %{daily_invites} per day exceeded"
msgstr "" msgstr ""
msgid "AddMember|Invites cannot be blank"
msgstr ""
msgid "AddMember|No invite source provided." msgid "AddMember|No invite source provided."
msgstr "" msgstr ""
......
...@@ -240,7 +240,7 @@ RSpec.describe API::Invitations do ...@@ -240,7 +240,7 @@ RSpec.describe API::Invitations do
params: { email: '', access_level: Member::MAINTAINER } params: { email: '', access_level: Member::MAINTAINER }
expect(response).to have_gitlab_http_status(:created) expect(response).to have_gitlab_http_status(:created)
expect(json_response['message']).to eq('Emails cannot be blank') expect(json_response['message']).to eq('Invites cannot be blank')
end end
it 'returns 404 when the email list is not a valid format' do it 'returns 404 when the email list is not a valid format' do
......
...@@ -6,6 +6,7 @@ RSpec.describe Members::CreateService, :aggregate_failures, :clean_gitlab_redis_ ...@@ -6,6 +6,7 @@ RSpec.describe Members::CreateService, :aggregate_failures, :clean_gitlab_redis_
let_it_be(:source, reload: true) { create(:project) } let_it_be(:source, reload: true) { create(:project) }
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:member) { create(:user) } let_it_be(:member) { create(:user) }
let_it_be(:user_invited_by_id) { create(:user) }
let_it_be(:user_ids) { member.id.to_s } let_it_be(:user_ids) { member.id.to_s }
let_it_be(:access_level) { Gitlab::Access::GUEST } let_it_be(:access_level) { Gitlab::Access::GUEST }
...@@ -49,6 +50,36 @@ RSpec.describe Members::CreateService, :aggregate_failures, :clean_gitlab_redis_ ...@@ -49,6 +50,36 @@ RSpec.describe Members::CreateService, :aggregate_failures, :clean_gitlab_redis_
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(true) expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(true)
end end
context 'when user_id is passed as an integer' do
let(:user_ids) { member.id }
it 'successfully creates member' do
expect(execute_service[:status]).to eq(:success)
expect(source.users).to include member
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(true)
end
end
context 'with user_ids as an array of integers' do
let(:user_ids) { [member.id, user_invited_by_id.id] }
it 'successfully creates members' do
expect(execute_service[:status]).to eq(:success)
expect(source.users).to include(member, user_invited_by_id)
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(true)
end
end
context 'with user_ids as an array of strings' do
let(:user_ids) { [member.id.to_s, user_invited_by_id.id.to_s] }
it 'successfully creates members' do
expect(execute_service[:status]).to eq(:success)
expect(source.users).to include(member, user_invited_by_id)
expect(OnboardingProgress.completed?(source.namespace, :user_added)).to be(true)
end
end
context 'when executing on a group' do context 'when executing on a group' do
let_it_be(:source) { create(:group) } let_it_be(:source) { create(:group) }
...@@ -191,6 +222,15 @@ RSpec.describe Members::CreateService, :aggregate_failures, :clean_gitlab_redis_ ...@@ -191,6 +222,15 @@ RSpec.describe Members::CreateService, :aggregate_failures, :clean_gitlab_redis_
) )
end end
context 'when it is an invite by email passed to user_ids' do
let(:user_ids) { 'email@example.org' }
it 'does not create task issues' do
expect(TasksToBeDone::CreateWorker).not_to receive(:perform_async)
execute_service
end
end
context 'when passing many user ids' do context 'when passing many user ids' do
before do before do
stub_licensed_features(multiple_issue_assignees: false) stub_licensed_features(multiple_issue_assignees: false)
......
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