From f6066a1eba68e3466d39240680e3968a0b725aa6 Mon Sep 17 00:00:00 2001 From: camdenmoors <66680985+camdenmoors@users.noreply.github.com> Date: Wed, 24 Jun 2020 13:41:55 -0400 Subject: [PATCH] Allow for disabling registration (#502) * Create registration column in DB * Disable account creation if registration is closed * Test that registration is closed --- app/controllers/registrations_controller.rb | 8 +++++- app/views/home/index.html.haml | 2 +- app/views/layouts/_dropdown.html.haml | 25 +++++++++++-------- config/locales/en.yml | 1 + .../20200623173255_add_toggle_registration.rb | 5 ++++ db/schema.rb | 3 ++- .../registrations_controller_test.rb | 8 ++++++ 7 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 db/migrate/20200623173255_add_toggle_registration.rb diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 1bbc3a05..3e023b90 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -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 diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index d88c4b10..8432755e 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -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 diff --git a/app/views/layouts/_dropdown.html.haml b/app/views/layouts/_dropdown.html.haml index 3eeaccb3..b44a5c23 100644 --- a/app/views/layouts/_dropdown.html.haml +++ b/app/views/layouts/_dropdown.html.haml @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index cbaef643..22e2e8f5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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' diff --git a/db/migrate/20200623173255_add_toggle_registration.rb b/db/migrate/20200623173255_add_toggle_registration.rb new file mode 100644 index 00000000..223b8d2b --- /dev/null +++ b/db/migrate/20200623173255_add_toggle_registration.rb @@ -0,0 +1,5 @@ +class AddToggleRegistration < ActiveRecord::Migration[6.0] + def change + add_column :games, :registration_enabled, :boolean, default: true, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index fb5b1c24..d3ba9698 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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| diff --git a/test/controllers/registrations_controller_test.rb b/test/controllers/registrations_controller_test.rb index 51e2eee1..102a2318 100644 --- a/test/controllers/registrations_controller_test.rb +++ b/test/controllers/registrations_controller_test.rb @@ -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