debian-mirror-gitlab/app/controllers/invites_controller.rb

140 lines
4 KiB
Ruby
Raw Normal View History

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
2021-06-08 01:23:25 +05:30
prepend_before_action :authenticate_user!, :track_invite_join_click, only: :show
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
2021-08-04 16:29:09 +05:30
if current_user_matches_invite? && member.accept_invite!(current_user)
2021-06-08 01:23:25 +05:30
redirect_to invite_details[:path], notice: helpers.invite_accepted_notice(member)
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-03-11 19:13:27 +05:30
return render layout: 'signup_onboarding' if !current_user && member.invite_to_unknown_user? && member.created_by
2021-01-03 14:25:43 +05:30
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?
2021-08-04 16:29:09 +05:30
current_user.verified_emails.include?(@member.invite_email)
2020-07-28 23:09:34 +05:30
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-06-08 01:23:25 +05:30
redirect_back_or_default(options: { alert: _("The invitation can not be found with the provided invite token.") })
end
def track_invite_join_click
experiment('members/invite_email', actor: member).track(:join_clicked) if member && Members::InviteEmailExperiment.initial_invite_email?(params[:invite_type])
2015-04-26 12:48:37 +05:30
end
def authenticate_user!
return if current_user
2021-06-08 01:23:25 +05:30
store_location_for(:user, invite_landing_url) if member
2015-04-26 12:48:37 +05:30
2021-04-29 21:17:54 +05:30
if user_sign_up?
2021-06-08 01:23:25 +05:30
set_session_invite_params
2021-09-30 23:02:18 +05:30
redirect_to new_user_registration_path(invite_email: member.invite_email), notice: _("To accept this invitation, create an account or sign in.")
2021-04-29 21:17:54 +05:30
else
redirect_to new_user_session_path(sign_in_redirect_params), notice: sign_in_notice
end
end
2020-11-24 15:15:51 +05:30
2021-06-08 01:23:25 +05:30
def set_session_invite_params
session[:invite_email] = member.invite_email
2021-09-30 23:02:18 +05:30
session[:originating_member_id] = member.id if Members::InviteEmailExperiment.initial_invite_email?(params[:invite_type])
2021-06-08 01:23:25 +05:30
end
2021-04-29 21:17:54 +05:30
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-11-24 15:15:51 +05:30
2021-04-29 21:17:54 +05:30
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-26 12:48:37 +05:30
end
2021-06-08 01:23:25 +05:30
def invite_landing_url
root_url + invite_details[:path]
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-06-08 01:23:25 +05:30
path: member.source.activity_path
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-06-08 01:23:25 +05:30
path: member.source.activity_path
2020-10-24 23:57:45 +05:30
}
end
2015-04-26 12:48:37 +05:30
end
end