2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
class InvitesController < ApplicationController
|
2020-07-28 23:09:34 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :member
|
2021-01-03 14:25:43 +05:30
|
|
|
before_action :ensure_member_exists
|
2020-10-24 23:57:45 +05:30
|
|
|
before_action :invite_details
|
2015-09-11 14:41:01 +05:30
|
|
|
skip_before_action :authenticate_user!, only: :decline
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
helper_method :member?, :current_user_matches_invite?
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
respond_to :html
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
feature_category :authentication_and_authorization
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def show
|
2020-07-28 23:09:34 +05:30
|
|
|
accept if skip_invitation_prompt?
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def accept
|
|
|
|
if member.accept_invite!(current_user)
|
2021-01-03 14:25:43 +05:30
|
|
|
track_invitation_reminders_experiment('accepted')
|
2020-10-24 23:57:45 +05:30
|
|
|
redirect_to invite_details[:path], notice: _("You have been granted %{member_human_access} access to %{title} %{name}.") %
|
|
|
|
{ member_human_access: member.human_access, title: invite_details[:title], name: invite_details[:name] }
|
2015-04-26 12:48:37 +05:30
|
|
|
else
|
2019-07-31 22:56:46 +05:30
|
|
|
redirect_back_or_default(options: { alert: _("The invitation could not be accepted.") })
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def decline
|
|
|
|
if member.decline_invite!
|
2021-01-03 14:25:43 +05:30
|
|
|
return render layout: 'devise_experimental_onboarding_issues' if !current_user && member.invite_to_unknown_user? && member.created_by
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
path =
|
2015-04-26 12:48:37 +05:30
|
|
|
if current_user
|
2015-09-25 12:07:36 +05:30
|
|
|
dashboard_projects_path
|
2015-04-26 12:48:37 +05:30
|
|
|
else
|
|
|
|
new_user_session_path
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
redirect_to path, notice: _("You have declined the invitation to join %{title} %{name}.") %
|
|
|
|
{ title: invite_details[:title], name: invite_details[:name] }
|
2015-04-26 12:48:37 +05:30
|
|
|
else
|
2019-07-31 22:56:46 +05:30
|
|
|
redirect_back_or_default(options: { alert: _("The invitation could not be declined.") })
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
def skip_invitation_prompt?
|
|
|
|
!member? && current_user_matches_invite?
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_user_matches_invite?
|
|
|
|
@member.invite_email == current_user.email
|
|
|
|
end
|
|
|
|
|
|
|
|
def member?
|
|
|
|
strong_memoize(:is_member) do
|
|
|
|
@member.source.users.include?(current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def member
|
2021-01-03 14:25:43 +05:30
|
|
|
strong_memoize(:member) do
|
|
|
|
@token = params[:id]
|
|
|
|
Member.find_by_invite_token(@token)
|
|
|
|
end
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def ensure_member_exists
|
|
|
|
return if member
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
render_404
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_user!
|
|
|
|
return if current_user
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
notice = ["To accept this invitation, sign in"]
|
|
|
|
notice << "or create an account" if Gitlab::CurrentSettings.allow_signup?
|
|
|
|
notice = notice.join(' ') + "."
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
redirect_params = member ? { invite_email: member.invite_email } : {}
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
store_location_for :user, request.fullpath
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
redirect_to new_user_session_path(redirect_params), notice: notice
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def invite_details
|
2021-01-03 14:25:43 +05:30
|
|
|
@invite_details ||= case member.source
|
2020-10-24 23:57:45 +05:30
|
|
|
when Project
|
|
|
|
{
|
2021-01-03 14:25:43 +05:30
|
|
|
name: member.source.full_name,
|
|
|
|
url: project_url(member.source),
|
2020-10-24 23:57:45 +05:30
|
|
|
title: _("project"),
|
2021-01-03 14:25:43 +05:30
|
|
|
path: project_path(member.source)
|
2020-10-24 23:57:45 +05:30
|
|
|
}
|
|
|
|
when Group
|
|
|
|
{
|
2021-01-03 14:25:43 +05:30
|
|
|
name: member.source.name,
|
|
|
|
url: group_url(member.source),
|
2020-10-24 23:57:45 +05:30
|
|
|
title: _("group"),
|
2021-01-03 14:25:43 +05:30
|
|
|
path: group_path(member.source)
|
2020-10-24 23:57:45 +05:30
|
|
|
}
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def track_invitation_reminders_experiment(action)
|
|
|
|
return unless Gitlab::Experimentation.enabled?(:invitation_reminders)
|
|
|
|
|
|
|
|
property = Gitlab::Experimentation.enabled_for_attribute?(:invitation_reminders, member.invite_email) ? 'experimental_group' : 'control_group'
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
Gitlab::Tracking.event(
|
2021-01-29 00:20:46 +05:30
|
|
|
Gitlab::Experimentation.experiment(:invitation_reminders).tracking_category,
|
2020-11-24 15:15:51 +05:30
|
|
|
action,
|
|
|
|
property: property,
|
|
|
|
label: Digest::MD5.hexdigest(member.to_global_id.to_s)
|
|
|
|
)
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|