Skip to content

Commit f6066a1

Browse files
authored
Allow for disabling registration (#502)
* Create registration column in DB * Disable account creation if registration is closed * Test that registration is closed
1 parent 7656393 commit f6066a1

File tree

7 files changed

+39
-13
lines changed

7 files changed

+39
-13
lines changed

app/controllers/registrations_controller.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
class RegistrationsController < Devise::RegistrationsController
44
before_action :load_game, :load_message_count
55
before_action :check_captcha, only: %i[create]
6-
before_action :prevent_action_after_game, only: %i[new create]
6+
before_action :prevent_action_after_game, :prevent_action_if_registration_closed, only: %i[new create]
7+
8+
def prevent_action_if_registration_closed
9+
return if @game.registration_enabled
10+
11+
redirect_back fallback_location: user_root_path, alert: I18n.t('game.registration_closed')
12+
end
713

814
def new
915
super

app/views/home/index.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
= link_to t('home.index.join_team'), join_team_users_path, :class => "btn btn-large btn-primary"
3636
.btn-group
3737
= link_to t('home.index.create_team'), new_team_path, :class => "btn btn-large btn-primary"
38-
- else
38+
- elsif @game.registration_enabled
3939
.btn-toolbar
4040
.pagination-centered
4141
.btn-group

app/views/layouts/_dropdown.html.haml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@
2828
= link_to t('application.edit_account'), edit_user_registration_path, :method => :get
2929
= link_to t('application.log_out'), destroy_user_session_path, :method => :delete
3030
- else
31-
%li.dropdown
32-
%a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#"}
33-
= t('home.index.login_or_register')
34-
%b.caret
35-
%ul.dropdown-menu
36-
%li
37-
=link_to t('application.log_in'), new_user_session_path
38-
%li.divider
39-
%li
40-
=link_to t('home.index.register'), new_user_registration_path
31+
- unless @game.nil?
32+
%li.dropdown
33+
%a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#"}
34+
- if @game.registration_enabled
35+
= t('home.index.login_or_register')
36+
- else
37+
= t('home.index.login')
38+
%b.caret
39+
%ul.dropdown-menu
40+
%li
41+
=link_to t('application.log_in'), new_user_session_path
42+
- if @game.registration_enabled
43+
%li.divider
44+
%li
45+
=link_to t('home.index.register'), new_user_registration_path

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ en:
162162
date_mismatch: 'The start date must be before the end date.'
163163
before_competition: 'This action is not available until the game is open.'
164164
after_competition: 'This action is not available after the game is over.'
165+
registration_closed: 'This action is not available because registration is closed.'
165166
setup: 'Finish setting up the scoreboard by creating a game. Not all fields are required, and you can always change them later! Learn more at %{href}'
166167
setup_href: "https://github.com/mitre-cyber-academy/ctf-scoreboard/wiki/Configuration#game"
167168
attack_defend_challenges: 'Attack & Defend Challenges'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddToggleRegistration < ActiveRecord::Migration[6.0]
2+
def change
3+
add_column :games, :registration_enabled, :boolean, default: true, null: false
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_06_15_163200) do
13+
ActiveRecord::Schema.define(version: 2020_06_23_173255) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -125,6 +125,7 @@
125125
t.text "prizes_text"
126126
t.text "terms_and_conditions"
127127
t.integer "board_layout", default: 0, null: false
128+
t.boolean "registration_enabled", default: true, null: false
128129
end
129130

130131
create_table "messages", id: :serial, force: :cascade do |t|

test/controllers/registrations_controller_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ def setup
5454
assert_equal I18n.t('game.after_competition'), flash[:alert]
5555
end
5656

57+
test 'cannot register for game when registration closed' do
58+
game = create(:active_game, registration_enabled: false)
59+
60+
get :new
61+
assert_redirected_to @controller.user_root_path
62+
assert_equal I18n.t('game.registration_closed'), flash[:alert]
63+
end
64+
5765
# We accidently enabled checking of captcha on naviation to the new page once,
5866
# this is to ensure we don't make that mistake again
5967
test 'new has no errors' do

0 commit comments

Comments
 (0)