2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-10 09:22:31 -04:00
|
|
|
class InvitesController < ApplicationController
|
2020-07-07 08:09:16 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2021-04-29 17:10:03 -04:00
|
|
|
prepend_before_action :authenticate_user!, :track_invite_join_click, only: :show
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :member
|
2020-09-22 05:09:43 -04:00
|
|
|
before_action :ensure_member_exists
|
2020-07-28 20:09:37 -04:00
|
|
|
before_action :invite_details
|
2015-04-16 08:03:37 -04:00
|
|
|
skip_before_action :authenticate_user!, only: :decline
|
2015-04-10 09:22:31 -04:00
|
|
|
|
2020-07-07 08:09:16 -04:00
|
|
|
helper_method :member?, :current_user_matches_invite?
|
|
|
|
|
2015-04-10 09:22:31 -04:00
|
|
|
respond_to :html
|
|
|
|
|
2020-10-05 08:08:47 -04:00
|
|
|
feature_category :authentication_and_authorization
|
|
|
|
|
2015-04-10 09:22:31 -04:00
|
|
|
def show
|
2020-07-07 08:09:16 -04:00
|
|
|
accept if skip_invitation_prompt?
|
2015-04-10 09:22:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def accept
|
2021-08-03 17:09:39 -04:00
|
|
|
if current_user_matches_invite? && member.accept_invite!(current_user)
|
2021-04-22 05:09:45 -04:00
|
|
|
redirect_to invite_details[:path], notice: helpers.invite_accepted_notice(member)
|
2015-04-10 09:22:31 -04:00
|
|
|
else
|
2019-04-08 10:17:45 -04:00
|
|
|
redirect_back_or_default(options: { alert: _("The invitation could not be accepted.") })
|
2015-04-10 09:22:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-10 10:37:02 -04:00
|
|
|
def decline
|
|
|
|
if member.decline_invite!
|
2021-02-07 16:09:09 -05:00
|
|
|
return render layout: 'signup_onboarding' if !current_user && member.invite_to_unknown_user? && member.created_by
|
2020-09-30 08:09:53 -04:00
|
|
|
|
2015-04-16 08:03:37 -04:00
|
|
|
path =
|
2015-04-10 10:37:02 -04:00
|
|
|
if current_user
|
2015-09-08 12:14:02 -04:00
|
|
|
dashboard_projects_path
|
2015-04-10 10:37:02 -04:00
|
|
|
else
|
|
|
|
new_user_session_path
|
|
|
|
end
|
|
|
|
|
2020-07-28 20:09:37 -04:00
|
|
|
redirect_to path, notice: _("You have declined the invitation to join %{title} %{name}.") %
|
|
|
|
{ title: invite_details[:title], name: invite_details[:name] }
|
2015-04-10 10:37:02 -04:00
|
|
|
else
|
2019-04-08 10:17:45 -04:00
|
|
|
redirect_back_or_default(options: { alert: _("The invitation could not be declined.") })
|
2015-04-10 10:37:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-10 09:22:31 -04:00
|
|
|
private
|
|
|
|
|
2020-07-07 08:09:16 -04:00
|
|
|
def skip_invitation_prompt?
|
|
|
|
!member? && current_user_matches_invite?
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_user_matches_invite?
|
2021-08-03 17:09:39 -04:00
|
|
|
current_user.verified_emails.include?(@member.invite_email)
|
2020-07-07 08:09:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def member?
|
|
|
|
strong_memoize(:is_member) do
|
|
|
|
@member.source.users.include?(current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-10 09:22:31 -04:00
|
|
|
def member
|
2020-09-22 05:09:43 -04:00
|
|
|
strong_memoize(:member) do
|
|
|
|
@token = params[:id]
|
|
|
|
Member.find_by_invite_token(@token)
|
|
|
|
end
|
|
|
|
end
|
2015-04-14 06:25:47 -04:00
|
|
|
|
2020-09-22 05:09:43 -04:00
|
|
|
def ensure_member_exists
|
|
|
|
return if member
|
2015-04-14 09:16:00 -04:00
|
|
|
|
2021-05-04 14:10:03 -04:00
|
|
|
redirect_back_or_default(options: { alert: _("The invitation can not be found with the provided invite token.") })
|
2015-04-10 09:22:31 -04:00
|
|
|
end
|
|
|
|
|
2021-04-29 17:10:03 -04:00
|
|
|
def track_invite_join_click
|
2021-08-06 14:09:57 -04:00
|
|
|
return unless member && initial_invite_email?
|
|
|
|
|
|
|
|
experiment(:invite_email_preview_text, actor: member).track(:join_clicked) if params[:experiment_name] == 'invite_email_preview_text'
|
|
|
|
Gitlab::Tracking.event(self.class.name, 'join_clicked', label: 'invite_email', property: member.id.to_s)
|
2021-04-29 17:10:03 -04:00
|
|
|
end
|
|
|
|
|
2015-04-10 09:22:31 -04:00
|
|
|
def authenticate_user!
|
|
|
|
return if current_user
|
|
|
|
|
2021-04-22 05:09:45 -04:00
|
|
|
store_location_for(:user, invite_landing_url) if member
|
2015-04-10 09:22:31 -04:00
|
|
|
|
2021-03-30 14:10:47 -04:00
|
|
|
if user_sign_up?
|
2021-04-29 17:10:03 -04:00
|
|
|
set_session_invite_params
|
2021-04-27 23:09:58 -04:00
|
|
|
|
2021-06-18 17:10:06 -04:00
|
|
|
redirect_to new_user_registration_path(invite_email: member.invite_email), notice: _("To accept this invitation, create an account or sign in.")
|
2021-03-30 14:10:47 -04:00
|
|
|
else
|
|
|
|
redirect_to new_user_session_path(sign_in_redirect_params), notice: sign_in_notice
|
|
|
|
end
|
|
|
|
end
|
2020-09-04 17:08:41 -04:00
|
|
|
|
2021-04-29 17:10:03 -04:00
|
|
|
def set_session_invite_params
|
|
|
|
session[:invite_email] = member.invite_email
|
|
|
|
|
2021-07-29 05:08:46 -04:00
|
|
|
session[:originating_member_id] = member.id if initial_invite_email?
|
2021-08-06 14:09:57 -04:00
|
|
|
session[:invite_email_experiment_name] = params[:experiment_name] if initial_invite_email? && params[:experiment_name]
|
2021-07-29 05:08:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initial_invite_email?
|
|
|
|
params[:invite_type] == Emails::Members::INITIAL_INVITE
|
2021-04-29 17:10:03 -04:00
|
|
|
end
|
|
|
|
|
2021-03-30 14:10:47 -04:00
|
|
|
def sign_in_redirect_params
|
|
|
|
member ? { invite_email: member.invite_email } : {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_sign_up?
|
|
|
|
Gitlab::CurrentSettings.allow_signup? && member && !User.find_by_any_email(member.invite_email)
|
|
|
|
end
|
2020-09-04 17:08:41 -04:00
|
|
|
|
2021-03-30 14:10:47 -04:00
|
|
|
def sign_in_notice
|
|
|
|
if Gitlab::CurrentSettings.allow_signup?
|
|
|
|
_("To accept this invitation, sign in or create an account.")
|
|
|
|
else
|
|
|
|
_("To accept this invitation, sign in.")
|
|
|
|
end
|
2015-04-10 09:22:31 -04:00
|
|
|
end
|
2015-04-14 06:25:38 -04:00
|
|
|
|
2021-04-22 05:09:45 -04:00
|
|
|
def invite_landing_url
|
|
|
|
root_url + invite_details[:path]
|
|
|
|
end
|
|
|
|
|
2020-07-28 20:09:37 -04:00
|
|
|
def invite_details
|
2020-09-22 05:09:43 -04:00
|
|
|
@invite_details ||= case member.source
|
2020-07-28 20:09:37 -04:00
|
|
|
when Project
|
|
|
|
{
|
2020-09-22 05:09:43 -04:00
|
|
|
name: member.source.full_name,
|
|
|
|
url: project_url(member.source),
|
2020-07-28 20:09:37 -04:00
|
|
|
title: _("project"),
|
2021-05-05 14:10:31 -04:00
|
|
|
path: member.source.activity_path
|
2020-07-28 20:09:37 -04:00
|
|
|
}
|
|
|
|
when Group
|
|
|
|
{
|
2020-09-22 05:09:43 -04:00
|
|
|
name: member.source.name,
|
|
|
|
url: group_url(member.source),
|
2020-07-28 20:09:37 -04:00
|
|
|
title: _("group"),
|
2021-05-05 14:10:31 -04:00
|
|
|
path: member.source.activity_path
|
2020-07-28 20:09:37 -04:00
|
|
|
}
|
|
|
|
end
|
2015-04-14 06:25:38 -04:00
|
|
|
end
|
2015-04-10 09:22:31 -04:00
|
|
|
end
|