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
|
|
|
|
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :member
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
if member.accept_invite!(current_user)
|
2020-07-28 20:09:37 -04:00
|
|
|
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-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!
|
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?
|
|
|
|
@member.invite_email == current_user.email
|
|
|
|
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
|
|
|
|
return @member if defined?(@member)
|
2015-04-16 08:03:37 -04:00
|
|
|
|
2015-04-10 09:22:31 -04:00
|
|
|
@token = params[:id]
|
2015-04-14 06:25:47 -04:00
|
|
|
@member = Member.find_by_invite_token(@token)
|
|
|
|
|
2017-02-21 17:31:14 -05:00
|
|
|
return render_404 unless @member
|
2015-04-14 09:16:00 -04:00
|
|
|
|
|
|
|
@member
|
2015-04-10 09:22:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def authenticate_user!
|
|
|
|
return if current_user
|
|
|
|
|
2018-09-14 01:42:05 -04:00
|
|
|
notice = ["To accept this invitation, sign in"]
|
|
|
|
notice << "or create an account" if Gitlab::CurrentSettings.allow_signup?
|
|
|
|
notice = notice.join(' ') + "."
|
2015-04-10 09:22:31 -04:00
|
|
|
|
|
|
|
store_location_for :user, request.fullpath
|
2020-08-10 14:09:54 -04:00
|
|
|
redirect_to new_user_session_path(invite_email: member.invite_email), notice: notice
|
2015-04-10 09:22:31 -04:00
|
|
|
end
|
2015-04-14 06:25:38 -04:00
|
|
|
|
2020-07-28 20:09:37 -04:00
|
|
|
def invite_details
|
|
|
|
@invite_details ||= case @member.source
|
|
|
|
when Project
|
|
|
|
{
|
|
|
|
name: @member.source.full_name,
|
|
|
|
url: project_url(@member.source),
|
|
|
|
title: _("project"),
|
|
|
|
path: project_path(@member.source)
|
|
|
|
}
|
|
|
|
when Group
|
|
|
|
{
|
|
|
|
name: @member.source.name,
|
|
|
|
url: group_url(@member.source),
|
|
|
|
title: _("group"),
|
|
|
|
path: group_path(@member.source)
|
|
|
|
}
|
|
|
|
end
|
2015-04-14 06:25:38 -04:00
|
|
|
end
|
2015-04-10 09:22:31 -04:00
|
|
|
end
|