summaryrefslogtreecommitdiff
path: root/app/controllers/invites_controller.rb
blob: 52430ea771fecbaba9a7694d923207408de425ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class InvitesController < ApplicationController
  before_action :member
  skip_before_action :authenticate_user!, only: :decline

  respond_to :html

  def show
  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}."
    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}."
    else
      redirect_back_or_default(options: { alert: "The invitation could not be declined." })
    end
  end

  private

  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 << "."

    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.name_with_namespace}"
      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