summaryrefslogtreecommitdiff
path: root/app/controllers/invites_controller.rb
blob: 8b1f6eeba1bb54df6b12971d662e05c4c2a6d0f9 (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
class InvitesController < ApplicationController
  before_filter :member

  respond_to :html

  layout 'navless'

  def show

  end

  def accept
    if member.accept_invite!(current_user)
      case member.source
      when Project
        project = member.source
        source = "project #{project.name_with_namespace}"
        path = namespace_project_path(project.namespace, project)
      when Group
        group = member.source
        source = "group #{group.name}"
        path = group_path(group)
      else
        source = "who knows what"
        path = dashboard_path
      end

      redirect_to path, notice: "You have been granted #{member.human_access} access to #{source}."
    else
      redirect_to :back, alert: "The invite could not be accepted."
    end
  end

  private

  def member
    return @member if defined?(@member)
    
    @token = params[:id]
    if member = Member.find_by_invite_token(@token)
      @member = member
    else
      render_404
    end
  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
end