From c130f80a7de15c3357f5563a440b5b659f93fcc5 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 18 Jul 2023 13:46:27 +0200 Subject: [PATCH 1/2] feat(user): add blocket_at and block_reason columns --- app/models/user.rb | 2 ++ .../20230718113720_add_blocked_at_block_reasonto_user.rb | 6 ++++++ db/schema.rb | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20230718113720_add_blocked_at_block_reasonto_user.rb diff --git a/app/models/user.rb b/app/models/user.rb index ef69a6e74..a688ebd44 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,6 +3,8 @@ # Table name: users # # id :integer not null, primary key +# blocked_at :datetime +# blocked_reason :text # confirmation_sent_at :datetime # confirmation_token :string # confirmed_at :datetime diff --git a/db/migrate/20230718113720_add_blocked_at_block_reasonto_user.rb b/db/migrate/20230718113720_add_blocked_at_block_reasonto_user.rb new file mode 100644 index 000000000..2250abb90 --- /dev/null +++ b/db/migrate/20230718113720_add_blocked_at_block_reasonto_user.rb @@ -0,0 +1,6 @@ +class AddBlockedAtBlockReasontoUser < ActiveRecord::Migration[7.0] + def change + add_column :users, :blocked_at, :datetime + add_column :users, :blocked_reason, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index 1ff80dc40..c50569b18 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[7.0].define(version: 2023_06_29_102031) do +ActiveRecord::Schema[7.0].define(version: 2023_07_18_113720) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -935,6 +935,8 @@ ActiveRecord::Schema[7.0].define(version: 2023_06_29_102031) do create_table "users", id: :serial, force: :cascade do |t| t.datetime "confirmation_sent_at", precision: 6 + t.datetime "blocked_at", precision: 6 + t.text "blocked_reason" t.string "confirmation_token" t.datetime "confirmed_at", precision: 6 t.datetime "created_at", precision: 6 From ad7fa39b6257f1376b34bc35240cfeb3eb8f8cc0 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 18 Jul 2023 13:53:06 +0200 Subject: [PATCH 2/2] feat(user): block login when .. blocked --- app/models/user.rb | 4 ++++ spec/system/users/sign_up_spec.rb | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index a688ebd44..85920d288 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -267,6 +267,10 @@ class User < ApplicationRecord devise_mailer.send(notification, self, *args).deliver_later end + def active_for_authentication? + super && blocked_at.nil? + end + private def does_not_merge_on_self diff --git a/spec/system/users/sign_up_spec.rb b/spec/system/users/sign_up_spec.rb index d5629802b..afa8e0651 100644 --- a/spec/system/users/sign_up_spec.rb +++ b/spec/system/users/sign_up_spec.rb @@ -142,4 +142,17 @@ describe 'Signing up:' do expect(page).to have_current_path new_user_session_path end end + + context 'when the user already has a confirmed account but is blocked' do + before do + create(:user, email: user_email, password: user_password, blocked_at: Time.current) + end + + scenario 'they cannot signed in' do + visit new_user_session_path + sign_in_with user_email, user_password + + expect(page).to have_current_path new_user_session_path + end + end end