@@ -10,6 +10,8 @@ class ApplicationController < ActionController::Base # rubocop:todo Metrics/Clas
1010 before_action :check_platform_setup
1111 before_action :set_locale
1212 before_action :store_user_location! , if : :storable_location?
13+
14+ before_action :set_platform_invitation
1315 before_action :check_platform_privacy
1416 # The callback which stores the current location must be added before you authenticate the user
1517 # as `authenticate_user!` (or whatever your resource is) will halt the filter chain and redirect
@@ -24,7 +26,7 @@ class ApplicationController < ActionController::Base # rubocop:todo Metrics/Clas
2426 rescue_from Pundit ::NotAuthorizedError , with : :user_not_authorized
2527 rescue_from StandardError , with : :handle_error
2628
27- helper_method :default_url_options
29+ helper_method :default_url_options , :valid_platform_invitation_token_present?
2830
2931 def self . default_url_options
3032 super . merge ( locale : I18n . locale )
@@ -56,15 +58,66 @@ def check_platform_setup
5658 redirect_to setup_wizard_path
5759 end
5860
61+ # rubocop:todo Metrics/PerceivedComplexity
62+ # rubocop:todo Metrics/MethodLength
63+ # rubocop:todo Metrics/AbcSize
64+ def set_platform_invitation # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
65+ # Only proceed if there's an invitation token in the URL or already in the session.
66+ return unless params [ :invitation_code ] . present? || session [ :platform_invitation_token ] . present?
67+
68+ # Check if the session token has expired.
69+ if session [ :platform_invitation_expires_at ] . present? && Time . current > session [ :platform_invitation_expires_at ]
70+ session . delete ( :platform_invitation_token )
71+ session . delete ( :platform_invitation_expires_at )
72+ return
73+ end
74+
75+ if params [ :invitation_code ] . present?
76+ # On first visit with the invitation code, update the session with the token and a new expiry.
77+ token = params [ :invitation_code ]
78+ session [ :platform_invitation_token ] = token
79+ session [ :platform_invitation_expires_at ] ||= Time . current + platform_invitation_expiry_time
80+ else
81+ # If no params, simply use the token stored in the session.
82+ token = session [ :platform_invitation_token ]
83+ end
84+
85+ return unless token . present?
86+
87+ @platform_invitation = ::BetterTogether ::PlatformInvitation . pending . find_by ( token : token )
88+
89+ return if @platform_invitation
90+
91+ session . delete ( :platform_invitation_token )
92+ session . delete ( :platform_invitation_expires_at )
93+ end
94+ # rubocop:enable Metrics/AbcSize
95+ # rubocop:enable Metrics/MethodLength
96+ # rubocop:enable Metrics/PerceivedComplexity
97+
5998 def check_platform_privacy
6099 return if helpers . host_platform . privacy_public?
61100 return if current_user
62101 return unless BetterTogether . user_class . any?
102+ return if valid_platform_invitation_token_present?
63103
64104 flash [ :error ] = I18n . t ( 'globals.platform_not_public' )
65105 redirect_to new_user_session_path ( locale : I18n . locale )
66106 end
67107
108+ def valid_platform_invitation_token_present?
109+ token = session [ :platform_invitation_token ]
110+ return false unless token . present?
111+
112+ if session [ :platform_invitation_expires_at ] . present? && Time . current > session [ :platform_invitation_expires_at ]
113+ return false
114+ end
115+
116+ ::BetterTogether ::PlatformInvitation . pending . exists? ( token : token )
117+ end
118+
119+ private
120+
68121 def handle404
69122 render_404
70123 end
@@ -171,8 +224,18 @@ def after_sign_in_path_for(resource)
171224 end
172225 end
173226
227+ def after_inactive_sign_up_path_for ( resource )
228+ new_user_session_path if helpers . host_platform &.private?
229+ super
230+ end
231+
174232 def after_sign_out_path_for ( _resource_or_scope )
175233 BetterTogether . base_path_with_locale
176234 end
235+
236+ # Configurable expiration time (e.g., 30 minutes)
237+ def platform_invitation_expiry_time
238+ 30 . minutes
239+ end
177240 end
178241end
0 commit comments