Skip to content

Commit 73b5fc1

Browse files
authored
Merge pull request #2582 from mroderick/fix/tc-unauthenticated-handling
fix(tc): handle unauthenticated and already-accepted users
2 parents e4b8be8 + 7be548f commit 73b5fc1

4 files changed

Lines changed: 70 additions & 14 deletions

File tree

app/controllers/terms_and_conditions_controller.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
class TermsAndConditionsController < ApplicationController
2-
before_action :logged_in?
2+
# Skip accept_terms (from ApplicationController) to avoid an infinite redirect loop:
3+
# - If user hasn't accepted T&C, ApplicationController already redirects here
4+
# - If user has accepted, there's no reason to redirect away from this page
5+
# The view handles both cases (needs to accept vs already accepted)
36
skip_before_action :accept_terms
47

58
def show
69
@terms_and_conditions_form = TermsAndConditionsForm.new
710
end
811

912
def update
13+
# The show action skips accept_terms, but unauthenticated users can still
14+
# POST directly to update. Redirect to login to avoid NoMethodError on nil.
15+
unless logged_in?
16+
redirect_to '/auth/github'
17+
return
18+
end
19+
1020
@terms_and_conditions_form = TermsAndConditionsForm.new(terms_params)
1121
if @terms_and_conditions_form.valid?
1222
member = current_user

app/views/terms_and_conditions/show.html.haml

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,30 @@
22
.row.justify-content-md-center
33
.col-md-10.col-lg-8
44
%h1= t('terms_and_conditions.title')
5-
%p
6-
= t('terms_and_conditions.body')
7-
%br
8-
= link_to t('terms_and_conditions.link_text'), code_of_conduct_path, target: '_blank'
5+
- if logged_in?
6+
- if current_user.accepted_toc_at?
7+
%p
8+
= t('terms_and_conditions.already_accepted_html', date: current_user.accepted_toc_at.strftime('%d %B %Y'))
9+
- else
10+
%p
11+
= t('terms_and_conditions.body')
12+
%br
13+
= link_to t('terms_and_conditions.link_text'), code_of_conduct_path, target: '_blank', rel: 'noopener'
14+
- else
15+
%p
16+
= t('terms_and_conditions.login_required_html')
17+
%br
18+
= link_to t('terms_and_conditions.login_link_text'), '/auth/github'
919

10-
= simple_form_for @terms_and_conditions_form, url: terms_and_conditions_path, method: :patch do |f|
11-
.row.justify-content-md-center
12-
.col-md-10.col-lg-8
13-
= f.input :terms, as: :boolean
14-
= f.button :button, t('terms_and_conditions.accept'), class: 'btn btn-primary mb-0'
20+
- if logged_in? && !current_user.accepted_toc_at?
21+
= simple_form_for @terms_and_conditions_form, url: terms_and_conditions_path, method: :patch do |f|
22+
.row.justify-content-md-center
23+
.col-md-10.col-lg-8
24+
= f.input :terms, as: :boolean
25+
= f.button :button, t('terms_and_conditions.accept'), class: 'btn btn-primary mb-0'
26+
- elsif !logged_in?
27+
= simple_form_for @terms_and_conditions_form, url: terms_and_conditions_path, method: :patch, html: { style: 'opacity: 0.6; pointer-events: none;' } do |f|
28+
.row.justify-content-md-center
29+
.col-md-10.col-lg-8
30+
= f.input :terms, as: :boolean, disabled: true
31+
= f.button :button, t('terms_and_conditions.accept'), class: 'btn btn-primary mb-0', disabled: true

config/locales/en.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ en:
350350
body: "Before you can proceed you must first read our Code of Conduct and agree to abide by it."
351351
link_text: "Read codebar's Code of Conduct"
352352
accept: "Accept"
353+
already_accepted_html: "You have already accepted our Code of Conduct on %{date}."
354+
login_required_html: "Please log in to accept our Code of Conduct."
355+
login_link_text: "Log in with GitHub"
353356
messages:
354357
notice: "You have to accept the Terms and Conditions before you are able to proceed."
355358
footer:

spec/features/accepting_terms_and_conditions_spec.rb

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
mock_github_auth
55
end
66

7-
scenario 'they can''t proceed unless they accept the ToCs' do
7+
scenario "they can't proceed unless they accept the ToCs" do
88
visit root_path
99
click_on 'Join us as a student'
1010
click_on 'Join us as a student'
@@ -16,7 +16,7 @@
1616
expect(page).to have_content('You have to accept the Terms and Conditions before you are able to proceed.')
1717
end
1818

19-
scenario 'they can read the Code of Conduct before accepting the ToCs', js: true do
19+
scenario 'they can read the Code of Conduct before accepting the ToCs', :js do
2020
visit root_path
2121
click_on 'Join us as a student'
2222
click_on 'Join us as a student'
@@ -47,7 +47,7 @@
4747
end
4848

4949
context 'When an existing member logs in' do
50-
context 'and they have not yet accepted codebar''s ToCs' do
50+
context "and they have not yet accepted codebar's ToCs" do
5151
scenario 'they have to accept before continuing to the page they want to get' do
5252
member = Fabricate(:member_without_toc)
5353
login(member)
@@ -59,14 +59,40 @@
5959
end
6060
end
6161

62-
context 'and they have already accepted codebar''s ToCs' do
62+
context "and they have already accepted codebar's ToCs" do
6363
scenario 'they will be redirected to the link they were trying to access' do
6464
member = Fabricate(:member)
6565
login(member)
6666

6767
visit dashboard_path
6868
expect(page).to have_current_path(dashboard_path)
6969
end
70+
71+
scenario 'they see a message that they have already accepted on the T&C page' do
72+
member = Fabricate(:member)
73+
login(member)
74+
75+
visit terms_and_conditions_path
76+
expect(page).to have_current_path(terms_and_conditions_path)
77+
expect(page).to have_content(/already accepted.*#{member.accepted_toc_at.strftime('%d %B %Y')}/)
78+
expect(page).not_to have_button('Accept')
79+
end
80+
end
81+
end
82+
83+
context 'When a guest user (not logged in) visits the page' do
84+
scenario 'they see a login prompt and cannot accept the ToCs' do
85+
visit terms_and_conditions_path
86+
87+
expect(page).to have_content('Please log in to accept our Code of Conduct.')
88+
expect(page).to have_link('Log in with GitHub')
89+
end
90+
91+
scenario 'they see a disabled form' do
92+
visit terms_and_conditions_path
93+
94+
expect(page).to have_field('terms_and_conditions_form_terms', disabled: true)
95+
expect(page).to have_button('Accept', disabled: true)
7096
end
7197
end
7298
end

0 commit comments

Comments
 (0)