Skip to content

Commit 866fd27

Browse files
committed
Extract user sign-in to dedicated methods
Also, this commit improves the redirect behavior and uses the supplied Sorcery method for final redirect. Having a single approach for user sign-in and redirect allows future changes to the login workflow.
1 parent 31159da commit 866fd27

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

app/controllers/application_controller.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,22 @@ def login_from_authentication_token
9090
token.update(expire_at: Time.zone.now)
9191
session[:study_group_id] = token.study_group_id
9292

93-
# Sorcery Login only works for InternalUsers
94-
return auto_login(token.user) if token.user.is_a? InternalUser
93+
session[:return_to_url] = request.fullpath
94+
authenticate(token.user)
95+
end
96+
end
9597

98+
def authenticate(user)
99+
if user.is_a? InternalUser
100+
# Sorcery Login only works for InternalUsers
101+
auto_login(user)
102+
else
96103
# All external users are logged in "manually"
97-
session[:external_user_id] = token.user.id
98-
token.user
104+
session[:external_user_id] = user.id
99105
end
106+
107+
flash = {notice: session.delete(:return_to_url_notice), alert: session.delete(:return_to_url_alert)}.compact_blank
108+
sorcery_redirect_back_or_to(:root, flash) unless session[:return_to_url] == request.fullpath
100109
end
101110

102111
def set_sentry_context

app/controllers/concerns/lti.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,6 @@ def store_lti_session_data(parameters)
237237

238238
@lti_parameters.lti_parameters = parameters.slice(*SESSION_PARAMETERS).permit!.to_h
239239
@lti_parameters.save!
240-
241-
session[:external_user_id] = current_user.id
242-
session[:pair_programming] = parameters[:custom_pair_programming] || false
243240
rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid
244241
retry
245242
end

app/controllers/sessions_controller.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,24 @@ def create_through_lti
2727
store_lti_session_data(params)
2828
store_nonce(params[:oauth_nonce])
2929
if params[:custom_redirect_target]
30-
redirect_to(URI.parse(params[:custom_redirect_target].to_s).path)
30+
session[:return_to_url] = URI.parse(params[:custom_redirect_target].to_s).path
3131
elsif params[:custom_pair_programming]
32-
redirect_to(new_exercise_programming_group_path(@exercise))
32+
session[:return_to_url] = new_exercise_programming_group_path(@exercise)
3333
else
34-
redirect_to(implement_exercise_path(@exercise),
35-
notice: t("sessions.create_through_lti.session_#{lti_outcome_service?(@exercise, @user) ? 'with' : 'without'}_outcome",
36-
consumer: @consumer))
34+
session[:return_to_url] = implement_exercise_path(@exercise)
35+
session[:return_to_url_notice] =
36+
t("sessions.create_through_lti.session_#{lti_outcome_service?(@exercise, @user) ? 'with' : 'without'}_outcome",
37+
consumer: @consumer)
3738
end
39+
session[:pair_programming] = params[:custom_pair_programming] || false
40+
authenticate(@user)
3841
end
3942

4043
def create
4144
if login(params[:email], params[:password], params[:remember_me])
4245
# We set the user's default study group to the "internal" group (no external id) for the given consumer.
4346
session[:study_group_id] = current_user.study_groups.find_by(external_id: nil)&.id
44-
sorcery_redirect_back_or_to(:root, notice: t('.success'))
47+
session[:return_to_url_notice] = t('.success')
4548
else
4649
flash.now[:danger] = t('.failure')
4750
render(:new)

spec/concerns/lti_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,6 @@ class Controller < AnonymousController
271271
describe '#store_lti_session_data' do
272272
let(:parameters) { ActionController::Parameters.new({}) }
273273

274-
it 'stores data in the session' do
275-
controller.instance_variable_set(:@user, create(:external_user))
276-
controller.instance_variable_set(:@exercise, create(:fibonacci))
277-
expect(controller.session).to receive(:[]=).with(:external_user_id, anything)
278-
expect(controller.session).to receive(:[]=).with(:pair_programming, anything)
279-
controller.send(:store_lti_session_data, parameters)
280-
end
281-
282274
it 'creates an LtiParameter Object' do
283275
expect do
284276
controller.instance_variable_set(:@user, create(:external_user))

spec/controllers/sessions_controller_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,24 @@
144144
expect(assigns(:exercise)).to eq(exercise)
145145
end
146146

147-
it 'stores LTI parameters in the session' do
147+
it 'persists LTI parameters' do
148148
expect(controller).to receive(:store_lti_session_data)
149149
perform_request
150150
end
151151

152+
it 'updates the session' do
153+
expect(controller.session).to receive(:[]=).with(:locale, anything).and_call_original
154+
expect(controller.session).to receive(:[]=).with(:study_group_id, anything).and_call_original
155+
expect(controller.session).to receive(:[]=).with(:embed_options, anything).and_call_original
156+
expect(controller.session).to receive(:[]=).with(:return_to_url, implement_exercise_path(exercise)).and_call_original # Initial redirect by the controller
157+
expect(controller.session).to receive(:[]=).with(:return_to_url, nil).and_call_original # Clearing the URL as done by Sorcery
158+
expect(controller.session).to receive(:[]=).with(:return_to_url_notice, anything).and_call_original
159+
expect(controller.session).to receive(:[]=).with(:external_user_id, anything).and_call_original
160+
expect(controller.session).to receive(:[]=).with(:pair_programming, anything).and_call_original
161+
expect(controller.session).to receive(:[]=).with('flash', anything).twice.and_call_original
162+
perform_request
163+
end
164+
152165
it 'stores the OAuth nonce' do
153166
expect(controller).to receive(:store_nonce).with(nonce)
154167
perform_request

0 commit comments

Comments
 (0)