debian-mirror-gitlab/app/controllers/invites_controller.rb
2020-08-09 17:41:57 +05:30

99 lines
2.3 KiB
Ruby

# frozen_string_literal: true
class InvitesController < ApplicationController
include Gitlab::Utils::StrongMemoize
before_action :member
skip_before_action :authenticate_user!, only: :decline
helper_method :member?, :current_user_matches_invite?
respond_to :html
def show
accept if skip_invitation_prompt?
end
def accept
if member.accept_invite!(current_user)
label, path = source_info(member.source)
redirect_to path, notice: _("You have been granted %{member_human_access} access to %{label}.") % { member_human_access: member.human_access, label: label }
else
redirect_back_or_default(options: { alert: _("The invitation could not be accepted.") })
end
end
def decline
if member.decline_invite!
label, _ = source_info(member.source)
path =
if current_user
dashboard_projects_path
else
new_user_session_path
end
redirect_to path, notice: _("You have declined the invitation to join %{label}.") % { label: label }
else
redirect_back_or_default(options: { alert: _("The invitation could not be declined.") })
end
end
private
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
def member
return @member if defined?(@member)
@token = params[:id]
@member = Member.find_by_invite_token(@token)
return render_404 unless @member
@member
end
def authenticate_user!
return if current_user
notice = ["To accept this invitation, sign in"]
notice << "or create an account" if Gitlab::CurrentSettings.allow_signup?
notice = notice.join(' ') + "."
store_location_for :user, request.fullpath
redirect_to new_user_session_path, notice: notice
end
def source_info(source)
case source
when Project
project = member.source
label = "project #{project.full_name}"
path = project_path(project)
when Group
group = member.source
label = "group #{group.name}"
path = group_path(group)
else
label = "who knows what"
path = dashboard_projects_path
end
[label, path]
end
end