summaryrefslogtreecommitdiff
path: root/app/controllers/registrations/welcome_controller.rb
blob: cfb4e939b350dbfb3cd9df17147da4fa410969ff (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true

module Registrations
  class WelcomeController < ApplicationController
    include OneTrustCSP
    include GoogleAnalyticsCSP
    include RegistrationsTracking

    layout 'minimal'
    skip_before_action :authenticate_user!, :required_signup_info, :check_two_factor_requirement, only: [:show, :update]
    before_action :require_current_user

    feature_category :authentication_and_authorization

    def show
      return redirect_to path_for_signed_in_user(current_user) if completed_welcome_step?

      track_event('render')
    end

    def update
      result = ::Users::SignupService.new(current_user, update_params).execute

      if result.success?
        track_event('successfully_submitted_form')

        redirect_to update_success_path
      else
        render :show
      end
    end

    private

    def registering_from_invite?(members)
      members.count == 1 && members.last.source.present?
    end

    def require_current_user
      return redirect_to new_user_registration_path unless current_user
    end

    def completed_welcome_step?
      current_user.role.present? && !current_user.setup_for_company.nil?
    end

    def update_params
      params.require(:user).permit(:role, :setup_for_company)
    end

    def requires_confirmation?(user)
      return false if user.confirmed?
      return false if Feature.enabled?(:soft_email_confirmation)

      true
    end

    def path_for_signed_in_user(user)
      return users_almost_there_path(email: user.email) if requires_confirmation?(user)

      stored_location_for(user) || members_activity_path(user.members)
    end

    def members_activity_path(members)
      return dashboard_projects_path unless members.any?
      return dashboard_projects_path unless members.last.source.present?

      members.last.source.activity_path
    end

    # overridden in EE
    def redirect_to_signup_onboarding?
      false
    end

    def redirect_for_tasks_to_be_done?
      MemberTask.for_members(current_user.members).exists?
    end

    def update_success_path
      return issues_dashboard_path(assignee_username: current_user.username) if redirect_for_tasks_to_be_done?

      return signup_onboarding_path if redirect_to_signup_onboarding?

      members = current_user.members

      if registering_from_invite?(members)
        flash[:notice] = helpers.invite_accepted_notice(members.last)
        members_activity_path(members)
      else
        # subscription registrations goes through here as well
        path_for_signed_in_user(current_user)
      end
    end

    # overridden in EE
    def signup_onboarding_path; end

    # overridden in EE
    def track_event(action); end
  end
end

Registrations::WelcomeController.prepend_mod_with('Registrations::WelcomeController')