BigW Consortium Gitlab

invites_controller.rb 1.83 KB
Newer Older
Douwe Maan committed
1
class InvitesController < ApplicationController
2 3
  before_action :member
  skip_before_action :authenticate_user!, only: :decline
Douwe Maan committed
4 5 6 7 8 9 10 11 12

  respond_to :html

  def show

  end

  def accept
    if member.accept_invite!(current_user)
13 14 15
      label, path = source_info(member.source)

      redirect_to path, notice: "You have been granted #{member.human_access} access to #{label}."
Douwe Maan committed
16
    else
17
      redirect_back_or_default(options: { alert: "The invitation could not be accepted." })
Douwe Maan committed
18 19 20
    end
  end

Douwe Maan committed
21 22
  def decline
    if member.decline_invite!
23
      label, _ = source_info(member.source)
Douwe Maan committed
24

25
      path =
Douwe Maan committed
26
        if current_user
Douwe Maan committed
27
          dashboard_projects_path
Douwe Maan committed
28 29 30 31
        else
          new_user_session_path
        end

32
      redirect_to path, notice: "You have declined the invitation to join #{label}."
Douwe Maan committed
33
    else
34
      redirect_back_or_default(options: { alert: "The invitation could not be declined." })
Douwe Maan committed
35 36 37
    end
  end

Douwe Maan committed
38 39 40 41
  private

  def member
    return @member if defined?(@member)
42

Douwe Maan committed
43
    @token = params[:id]
44 45
    @member = Member.find_by_invite_token(@token)

46 47 48 49 50
    unless @member
      render_404 and return
    end

    @member
Douwe Maan committed
51 52 53 54 55 56 57 58 59 60 61 62
  end

  def authenticate_user!
    return if current_user

    notice = "To accept this invitation, sign in"
    notice << " or create an account" if current_application_settings.signup_enabled?
    notice << "."

    store_location_for :user, request.fullpath
    redirect_to new_user_session_path, notice: notice
  end
63 64 65 66 67 68 69 70 71 72 73 74 75

  def source_info(source)
    case source
    when Project
      project = member.source
      label = "project #{project.name_with_namespace}"
      path = namespace_project_path(project.namespace, project)
    when Group
      group = member.source
      label = "group #{group.name}"
      path = group_path(group)
    else
      label = "who knows what"
Douwe Maan committed
76
      path = dashboard_projects_path
77 78 79 80
    end

    [label, path]
  end
Douwe Maan committed
81
end