Skip to content

Commit

Permalink
Allow for disabling registration (#502)
Browse files Browse the repository at this point in the history
* Create registration column in DB

* Disable account creation if registration is closed

* Test that registration is closed
  • Loading branch information
camdenmoors authored Jun 24, 2020
1 parent 7656393 commit f6066a1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 13 deletions.
8 changes: 7 additions & 1 deletion app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
class RegistrationsController < Devise::RegistrationsController
before_action :load_game, :load_message_count
before_action :check_captcha, only: %i[create]
before_action :prevent_action_after_game, only: %i[new create]
before_action :prevent_action_after_game, :prevent_action_if_registration_closed, only: %i[new create]

def prevent_action_if_registration_closed
return if @game.registration_enabled

redirect_back fallback_location: user_root_path, alert: I18n.t('game.registration_closed')
end

def new
super
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
= link_to t('home.index.join_team'), join_team_users_path, :class => "btn btn-large btn-primary"
.btn-group
= link_to t('home.index.create_team'), new_team_path, :class => "btn btn-large btn-primary"
- else
- elsif @game.registration_enabled
.btn-toolbar
.pagination-centered
.btn-group
Expand Down
25 changes: 15 additions & 10 deletions app/views/layouts/_dropdown.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@
= link_to t('application.edit_account'), edit_user_registration_path, :method => :get
= link_to t('application.log_out'), destroy_user_session_path, :method => :delete
- else
%li.dropdown
%a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#"}
= t('home.index.login_or_register')
%b.caret
%ul.dropdown-menu
%li
=link_to t('application.log_in'), new_user_session_path
%li.divider
%li
=link_to t('home.index.register'), new_user_registration_path
- unless @game.nil?
%li.dropdown
%a.dropdown-toggle{"data-toggle" => "dropdown", :href => "#"}
- if @game.registration_enabled
= t('home.index.login_or_register')
- else
= t('home.index.login')
%b.caret
%ul.dropdown-menu
%li
=link_to t('application.log_in'), new_user_session_path
- if @game.registration_enabled
%li.divider
%li
=link_to t('home.index.register'), new_user_registration_path
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ en:
date_mismatch: 'The start date must be before the end date.'
before_competition: 'This action is not available until the game is open.'
after_competition: 'This action is not available after the game is over.'
registration_closed: 'This action is not available because registration is closed.'
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}'
setup_href: "https://github.com/mitre-cyber-academy/ctf-scoreboard/wiki/Configuration#game"
attack_defend_challenges: 'Attack & Defend Challenges'
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20200623173255_add_toggle_registration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddToggleRegistration < ActiveRecord::Migration[6.0]
def change
add_column :games, :registration_enabled, :boolean, default: true, null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_06_15_163200) do
ActiveRecord::Schema.define(version: 2020_06_23_173255) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -125,6 +125,7 @@
t.text "prizes_text"
t.text "terms_and_conditions"
t.integer "board_layout", default: 0, null: false
t.boolean "registration_enabled", default: true, null: false
end

create_table "messages", id: :serial, force: :cascade do |t|
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/registrations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def setup
assert_equal I18n.t('game.after_competition'), flash[:alert]
end

test 'cannot register for game when registration closed' do
game = create(:active_game, registration_enabled: false)

get :new
assert_redirected_to @controller.user_root_path
assert_equal I18n.t('game.registration_closed'), flash[:alert]
end

# We accidently enabled checking of captcha on naviation to the new page once,
# this is to ensure we don't make that mistake again
test 'new has no errors' do
Expand Down

0 comments on commit f6066a1

Please sign in to comment.